score:10
What you are describing is actually possible with chart.js as long as at least 1 of your datasets can be expressed using the alternate scatter chart dataset. Let me walk you through an example where I show the daily avg as bars and the hourly avg as a line (aka, a mixed chart type).
First, configure a bar chart with labels for each day of the week and provide the daily average values for the data as your first dataset.
Next, create another dataset, but set it's type
property to 'line'
. Since we are wanting to use a different X axis and a different set of labels, you must express your data using the {x: 0, y: 2}
notation. This will allow you to circumvent the scale from using the labels defined above.
Finally, in your options.scales
config, define 2 xAxes
scales and associate your 2nd dataset to the 2nd X axis scale (also set it's display
property to false
so you don't see any of the 2nd scale).
You end up with a chart configuration that looks like this.
var ctx = document.getElementById("canvas").getContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
datasets: [{
type: 'bar',
label: 'Daily Avg',
backgroundColor: chartColors.red,
data: dailyAvgData,
borderColor: 'white',
borderWidth: 2
}, {
type: 'line',
label: 'Hourly Avg',
borderColor: chartColors.green,
backgroundColor: chartColors.green,
borderWidth: 1,
fill: false,
pointRadius: 0,
xAxisID: 'x-axis-2',
data: hourlyAvgData
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js - Combo Chart With Multiple Scales (X Axis)'
},
tooltips: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{}, {
id: 'x-axis-2',
type: 'linear',
position: 'bottom',
display: false,
}],
yAxes: [{
ticks: {
min: 0,
max: 50
}
}]
}
}
});
You can see it in action at this codepen.
Here is another example but instead of using a combo chart (daily is bars, hourly is line), both plots are lines. The approach is exactly the same except the chart type is changed from bar
to line
.
Note, in this version I have adjusted the daily avg line over by one so that the points are plotted at the "end of the day". The previous example was showing the daily avg at the start of each day (which is technically not correct).
Here is an example codepen that demonstrates the line version of the chart.
score:0
Landing here after searching how to implement a moving average in Chart.js, I've struggled to fully understand the answer of @jordanwillis, so maybe this can be useful to someone else too.
The central idea is that you can assign a format of data (for each dataset in datasets
) such that the y
component represents the value of the series and the x
- this is the crucial point IMHO - matches the label (in the unique, common labels
array, that will be the shared x axis for all the series, i.e. the datasets) where you want to plot that specific point.
So my example for a moving average of N (say 7 day moving average) and a generic asymmetric shift of S between 0 and N-1 (e.g. 3) would be
var valuesMean7 = movingAverage(7, 3, ydata, xlabels);
where I have defined:
function movingAverage(N, shift, values, labels) {
var NN = values.length;
var valuesMean = [];
rightShift = N - shift - 1;
for (let i = shift; i < NN - rightShift; i++) {
var mean = 0;
for (let j = i - shift; j < i - shift + N; j++) {
mean += values[j];
}
mean = mean / N;
valuesMean.push({
x: labels[i],
y: mean
});
}
return valuesMean;
}
Source: stackoverflow.com
Related Query
- Updating chart.js chart with dataset of different size
- Can I specify a different font size for each row of text in Chart Title?
- ChartJS - Line Chart with different size datasets
- Chart.js: different dataset size
- Updating chart.js with a dataset of different size not working
- How to assign values to different lables(legends) of my dataset of stacked bar chart
- Chart.js - How to set a line chart dataset as disabled on load
- Set minimum step size in chart js
- Chart.js Line-Chart with different Labels for each Dataset
- How to display Line Chart dataset point labels with Chart.js?
- Add DataSet Bar Chart - Chart.JS
- How to add an offset to a dataset in Chart js
- Chart.js setting maximum bar size of bar chart
- Chart.js Mixed Bar and Line chart with different scales
- chart.js Line chart with different background colors for each section
- Different color for each column in angular-chartjs bar chart
- Chart js different background for y axis
- Chart.js dataset controller 'null' when chart drawn
- chart js - Apply different color for each x-axes label
- Chart JS, Choose different Y-axis for different datasets
- How to dynamically set ChartJs line chart width based on dataset size?
- Can't size doughnut chart from chart.js
- Chart.js Radar chart legend label font size doesn't work
- Different gridline steps on chart js line chart
- ChartJS V3 Radar chart Label Font Size
- Chart.js different scaleLine color of radar chart (angular)
- Chart JS dataset disabled by default
- Change size of a specific point on a line chart in Chart.js
- Chart.js - bind data in dataset to specific chart labels
- Customize different tooltips of bar chart
More Query from same tag
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- Updating in chart.js
- Angularjs - Can't call the value from form to graph
- Chart js cut the title and the legends
- chart.js modify style from dataset label
- Graph made with chart js wont change size?
- highlightFill for radar charts in chartJS
- I want to add label on only specific vue chart
- after div display set to block nothing shows
- ChartJs (ver: 2.8.0): Custom tooltip does not hide if clicked (or mouse pointer is moved) outside the chart canvas area
- Angular Chart.js not displaying Chart (Legend is Displaying)
- charts are not being show with wicked_pdf
- chart.js control space between/size of gridlines
- How to adding data in loop and display chart dynamically like in live (chart.js & typescript & angular)?
- Chart.js Bubble chart with custome X-axis labels
- How do I filter & update dates using react-chartjs-2
- Problem adding name to X-axis for a chart
- Chartjs Datasets overlapping and z-index
- Chart.js how to use scriptable options
- Laravel DB select statement. Query returns column name as result rather than values
- ChartJS remove previous chart when making new one
- Horizontal gradient for every single bar in group chart js
- How to move the x-axis values and line points to the middle of two x-axis lines using chartjs?
- How to add ChartJS code in Html2Pdf to view image
- set my chart min yaxis to 0
- ChartJS:align zeros on chart with multi axes
- How to sum/divide array values in chart.js?
- How to add new data point and remove leftmost data point dynamically in Chartjs
- Chart.js with plotting durations against dates
- How to remove old chart and append new chart to div using chartjs