score:0

Accepted answer

change:

window.mychart = new chart(ctx, chartdata[curindex]);

to

window.mychart = new chart(ctx, json.parse(json.stringify(chartdata[curindex])));

the reason being you need to clone the object otherwise mychart is the same as chartdata[0] and when you click on 1 or 2, it overwrites 0.

this part of the code clones the object json.parse(json.stringify(chartdata[curindex]))

score:0

the update is not working. you should rerender the chart

 function resetchart() {
   const ctx = document.getelementbyid("mychart").getcontext("2d");
   window.mychart = new chart(ctx, chartdata[curindex]);
 }

 window.onload = function() {
  this.resetchart();
 };

function updatechart(index) {
  curindex = index;
  mychart.data.labels = chartdata[curindex].data.labels;
  mychart.data.datasets[0].data = chartdata[curindex].data.datasets[0].data;
  mychart.data.datasets[0].label = chartdata[curindex].data.datasets[0].label;
  mychart.data.datasets[1].data = chartdata[curindex].data.datasets[1].data;
  mychart.data.datasets[1].label = chartdata[curindex].data.datasets[1].label;
  this.resetchart();
}

Related Query

More Query from same tag