[Chartjs]-How to update a variable after button click

1๐Ÿ‘

I think this is what you need:

function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
    dataset.data.push(data);
});
chart.update();}

function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
    dataset.data.pop();
});
chart.update();}

Source: Updating Charts

0๐Ÿ‘

You need to call the object method .update(), every time you change the data, in order to update the graph visualisation.
Take a look at the documentation of the Chartjs library: https://www.chartjs.org/docs/latest/developers/updates.html.

0๐Ÿ‘

Original Docs: https://www.chartjs.org/docs/latest/developers/api.html#updateconfig

Sample:

 $('#1').click(function () {
    spins15 = [15, 25, 35];
    someChart.data.datasets[0] = spin;
    someChart.update();
});

Leave a comment