addData()从最新的chart.js 2.1.3中删除 - 最新消息?

时间:2016-05-18 11:33:14

标签: javascript chart.js

我一直在阅读文档,有些方法可以替换数据然后更新图表:

myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's value of 'March' to be 50
myLineChart.update(); // Calling update now animates the position of March from 90 to 50.

https://github.com/chartjs/Chart.js/blob/master/docs/07-Advanced.md

addData()似乎已经消失了,我是否坚持为本地数据创建自己的addData然后更新整个图表?或者我错过了什么。

1 个答案:

答案 0 :(得分:2)

update()也处理添加数据。只需将新数据/标签推送到创建图表时传递的配置对象,然后调用update()

例如,

var config = {
  type: 'line',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First dataset",
      data: [65, 0, 80, 81, 56, 85, 40],
      fill: false
    }]
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, config);

setTimeout(function(){
    config.data.labels.push('Test');
    config.data.datasets[0].data.push(3);
    myChart.update();
}, 1000);

小提琴 - http://jsfiddle.net/zpnx8ppb/