score:10

Accepted answer

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;
}

Related Query

More Query from same tag