[Chartjs]-ChartJS, add new dataset

2👍

First set every array except the first one to blank. Then use onAnimationComplete to progressively load them back.

Adapted from https://stackoverflow.com/a/36010562/360067

...

var dataArrays = [];
for (var i = 1; i < data.datasets.length; i++) {
    dataArrays.push(data.datasets[i].data);
    data.datasets[i].data = data.datasets[i].data.map(function() {
        return 0
    });
}    

var myLineChart = new Chart(ctx).Line(data, {
  animationEasing: 'linear',
  onAnimationComplete: function () {
    if (dataArrays.length !== 0) {
      var index = myLineChart.datasets.length - dataArrays.length;
      myLineChart.datasets[index].points.forEach(function (point, i) {
        point.value = dataArrays[0][i];
      });
      myLineChart.update();
      dataArrays.shift();
    }
  }
});

Fiddle – http://jsfiddle.net/ngf79suk/

Leave a comment