Chartjs-Chart.js – Multiple generateLegend() functions on same page

0👍

You are replacing your variable on each iteration, so naturally the variable refers to the last thing assigned (i.e. the last chart created):

while (chartCounter <= chartCount) {
  ...
  window.statsChart = new Chart(ctx, {...});
  ...
}

Clicking a legend item calls updateDataset wherein you reference back to window.statsChart via the line:

var ci = e.view.statsChart;

Since statsChart is assigned to the last chart iterated your code affects only that chart.

Either make statsChart an array and pass the relevant index via onclick or simply pass the chart object itself to updateDataset (although that won’t work via the inline onclick; you’d need to bind to the element).

Leave a comment