Chart.js how to resend animation command to a chart?

0👍

Easyest way to do this is by removing all the data updating the chart, readding the data and the updating the chart again

const chart = new Chart(ctx, options);

document.getElementById("tt").addEventListener("click", () => {
  const dataSets = chart.data.datasets;
  chart.data.datasets = [];
  chart.update();
  chart.data.datasets = dataSets;
  chart.update();
})

Live example:

var options = {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderWidth: 1,
      backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
    }]
  },
  options: {
    scales: {}
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);

document.getElementById("tt").addEventListener("click", () => {
  const dataSets = chart.data.datasets;
  chart.data.datasets = [];
  chart.update();
  chart.data.datasets = dataSets;
  chart.update();
})
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <button id="tt">
    Reanimate
    </button>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.0/chart.js"></script>
</body>

Leave a comment