Updating Charts

It's pretty common to want to update charts after they've been created. When the chart data is changed, Chart.js will animate to the new data values.

Adding or Removing Data

Adding and removing data is supported by changing the data array. To add data, just add data into the data array as seen in this example.

  1. function addData(chart, label, data) {
  2. chart.data.labels.push(label);
  3. chart.data.datasets.forEach((dataset) => {
  4. dataset.data.push(data);
  5. });
  6. chart.update();
  7. }
  1. function removeData(chart) {
  2. chart.data.labels.pop();
  3. chart.data.datasets.forEach((dataset) => {
  4. dataset.data.pop();
  5. });
  6. chart.update();
  7. }

Preventing Animations

Sometimes when a chart updates, you may not want an animation. To achieve this you can call update with a duration of 0. This will render the chart synchronously and without an animation.