[Chartjs]-Animate datasets separately

2👍

SOLUTION for ChartJS v2.6

var done = false;
var data2 = [28, 48, 40, 19, 86, 27, 90];

var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
         label: "My First dataset",
         backgroundColor: "rgba(220,220,220,0.2)",
         borderColor: "rgba(220,220,220,1)",
         pointBackgroundColor: "rgba(220,220,220,1)",
         pointBorderColor: "#fff",
         pointHoverBackgroundColor: "#fff",
         pointHoverBorderColor: "rgba(220,220,220,1)",
         data: [65, 0, 80, 81, 56, 85, 40]
      }, {
         label: "My Second dataset",
         backgroundColor: "rgba(151,187,205,0.2)",
         borderColor: "rgba(151,187,205,1)",
         pointBackgroundColor: "rgba(151,187,205,1)",
         pointBorderColor: "#fff",
         pointHoverBackgroundColor: "#fff",
         pointHoverBorderColor: "rgba(151,187,205,1)",
         data: [0, 0, 0, 0, 0, 0, 0]
      }]
   },
   options: {
      animation: {
         easing: 'linear',
         onComplete: function(e, i) {
            if (!done) {
               this.data.datasets[1].data = data2;
               /* we need to update chart within setTimeout,
               	as there seems to be an issue when updating the chart 
               	at first animation onComplete function call */
               setTimeout(function() {
                  this.update();
               }.bind(this), 100);
               done = true;
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart" height="300" width="800"></canvas>

Leave a comment