Chartjs-How do I display two datasets on a single chart with chartjs

0👍

To start, you don’t need a foreach here otherwise you will push 4 data in the dataset while you are declaring 2 :

dataPlot.data.datasets.forEach((dataset)=> {
      dataset.data.push(data);
      dataset.data.push(data2);
 });

Try one of those 2 options :

Doing things manually (without foreach):

dataPlot.data.datasets.push(data);
dataPlot.data.datasets.push(data2);

OR, create an array of data, and make the forEach on that array :

dataArray.forEach( (item) => {
    dataPlot.data.datasets.push(item);
});

PS : for the second option, you need to make sure that dataArray.length == dataPlot.data.datasets.length

Leave a comment