score:4
i'm sorry if I didn't answer before.
Here a working fiddle https://jsfiddle.net/7L0fu4er/10/
Let's explain my solution:
With Charts.js you can use only one set of labels for both two x axes, the workaround is to populate the labels array with 2 dates every one value of the array, after that, you have to call a callback in the "ticks" method of every xAxe that will split dates with a delimiter ("#" in my case).
In our case, we want to show two periods (like the ecommerce revenue of every day, this week and the last week), your labels array will be like:
{labels: ["09/01/2018#02/01/2018","10/01/2018#03/01/2018"] ...}
If you see the first value "09/01/2018#02/01/2018"
, the date before the "#" character is for the period 1, the second one is for the period 2.
So
First. You have to build the labels array, the days for every period must be the same number for every period values
Give an unique Id for every xAxe
Call "ticks" callback for every xAxe that will elaborate every labels array value, so you can split and draw your correct date for every day
I hope I have helped you
score:0
I Know its an old question but I found out a great solution for this, m using .net framework.
1- make a store procedure to get all the data from database for this year GroupBy the data and first 3 letter of the month:
SELECT sum(anything) AS something , MAX ( Convert(char(3), tb.date, 0)) AS ALL_MONTHS
FROM table tb WHERE YEAR(tb.date) = YEAR(CURRENT_TIMESTAMP)
GROUP BY YEAR(tb.date), MONTH(tb.date)
ORDER BY YEAR(tb.date), MONTH(tb.date)
2- make a store procedure to get all the data from database for last year, similar to step 1 but the where clause would be something like this:
.......... WHERE YEAR(tb.date) = YEAR(GETDATE()) - 1 ..........
means minus the current year.
3- after sending the data from controller to view, in the View page use JS to manipulate the data.
note: there are 12 month so make an array for 12 month so that way there will be only one label but the data will figure out there place of month according to the index that they will receive from the js loop function.
/////-- chart-----------------------------------------------------
var salesChartCanvas = $('#salesChart').get(0).getContext('2d')
var months = @Html.Raw(Json.Encode(Model.expChartMonths));
var total = @Html.Raw(Json.Encode(Model.expChartTotal));
var Lastmonths = @Html.Raw(Json.Encode(Model.expChartMonthsLast));
var Lasttotal = @Html.Raw(Json.Encode(Model.expChartTotalLast));
var arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var arr2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
function getMonthFromString(mon) {
return new Date(Date.parse(mon + " 1, 2021")).getMonth()
}
function getMonth(mon) {
return new Date(Date.parse(mon + " 1, 2021")).getMonth()
}
//this year
for (var x = 0; x < months.length; x++) { arr2[getMonth(months[x])] = total[x] }
//last year
for (var i = 0; i < Lastmonths.length; i++) { arr[getMonthFromString(Lastmonths[i])] = Lasttotal[i] }
var salesChartData = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [
{
label: 'This Year Expenses',
fill: false,
backgroundColor: 'rgba(60,141,188,0.9)',
borderColor: 'rgba(60,141,188,0.8)',
pointRadius: 5,
pointColor: '#3b8bba',
pointStrokeColor: 'rgba(60,141,188,1)',
pointHighlightFill: '#fff',
pointHighlightStroke: 'rgba(60,141,188,1)',
data: arr2
}
,
{
label: 'Last Year Expenses',
fill: false,
backgroundColor: 'rgba(210, 214, 222, 1)',
borderColor: 'rgba(210, 214, 222, 1)',
pointRadius: 5,
pointColor: 'rgba(210, 214, 222, 1)',
pointStrokeColor: '#c1c7d1',
pointHighlightFill: '#fff',
pointHighlightStroke: 'rgba(220,220,220,1)',
data: arr
}
]
}
Source: stackoverflow.com
Related Query
- Chart.js: compare two periods like Google Analytics with a line chart
- Chart.js - line chart with two yAxis: "TypeError: yScale is undefined"
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- How we embed google analytics chart from google analytic with angular 4?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- line chart with {x, y} point data displays only 2 values
- Display line chart with connected dots using chartJS
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to display Line Chart dataset point labels with Chart.js?
- Chart.js Mixed Bar and Line chart with different scales
- chart.js Line chart with different background colors for each section
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js: Line chart with partial dashed line
- Obtain max value of y axis of line chart rendered with Chart.js
- ChartJS - Line Chart with different size datasets
- Line chart with large number of labels on X axis
- ChartJS - Line chart issue with only 1 point
- ChartJs line chart - display permanent icon above some data points with text on hover
- Is it possible to revert x-axe values in line chart with chart.js
- Drawing line chart in chart.js with json response
- Chart.js line chart with area range
- Extending Line Chart with custom line
- Can we draw a Line Chart with both solid and dotted line in it?
- Chart js: Update line chart having two data sets
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Display two line graphs with data starting at different points
- Chart.js plotting two json datasets on a line chart
- Line Chart x-Axis datapoints with Strings
More Query from same tag
- "<%=value%>" in javascript not ASP.net
- Is it possible with Chart js or which chart Library can provide this type of chart?
- ChartJS 3.0 - Number format
- How to sum array value in chart.js inside loop function?
- Chart.js: Force display of a certain label with autoSkip: true
- Charts.js candlestick (Financial Charts) Timeformat Question
- Configuring a Radar Chart w/Chart.js
- Stacked Bar Chart, Two on Same Line w/ Same Color
- How to make rectangle in chart.js
- Multi Level Color in Doughnut Chart in ng2-charts
- Angular 2 chart - change point radius
- Trying to load a chart with chart.js, through ajax and php
- How to make a child component of a sibling component refresh when a button is pressed in ReactJS?
- Chart.js - Timeline
- How to change the Y-Axis to show $ in graph
- Chart.js - make background text responsive
- Chart.js, set a max value
- Printing a list by clicking chart Chart js + react
- Rendering Chart.js Bubble Chart Using Array Data
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- How to hide row in chartJS when filtering?
- How show data label in the graph on Chart.js?
- Why does the horizontal bar size jump when resizing the chart?
- How to inject "Chart.js" in my module?
- chart.js one of the chart from mixed chart is not plotting
- Chart isn't updating with Response data (Chart.js 3.2.1, ng2-charts 3.0.0-beta.9)
- chart.js radar pointLabel options not working
- Polar Area Chart
- Draw vertical line on horizontalBar at value 2.0
- Align data label right. Horizontal bar chart Vue JS