Creating multiple charts and selecting them later

0👍

But that seems like it’s just setting ONE object called dayChart in
the window scope.

That’s because you are assigning the chart instance to the same variable each time. What you probably want is

To initialize (once)

window.piieCharts = [];

and then each time you create a chart

window.piieCharts.push(new Chart(ctx).Pie(pieData,pieOptions));

and you can get the instances by index

window.piieCharts[0].update();

Or you can use an object. To initialize

window.piieCharts = {};

and then each time you create a chart

window.piieCharts['canvas#'+idDate] = new Chart(ctx).Pie(pieData,pieOptions);

and you can get the instances by the key

window.piieCharts['canvas#'+idDate].update();

Alternatively since Chart.js stores each instance it creates (till they are destroyed) in Chart.instances, you can also do

var myInstance;
Chart.helpers.each(Chart.instances, function(instance) { 
    if (instance.chart.canvas === document.getElementById('canvas#'+idDate))
         myInstance = instance;
});
myInstance.update();

Leave a comment