简体   繁体   English

在 Chart.js 2.0 中添加数据

[英]AddData in Chart.js 2.0

I am trying to create a double-Y-axis with Chart.js 2.0 .我正在尝试使用Chart.js 2.0创建一个双 Y 轴。 I saw in documentation that AddData works as in Chart.js 1.0 .我在文档中看到AddDataChart.js 1.0 But when I run on browser I get the message " xxxxxxxx.AddData is not a function ".但是当我在浏览器上运行时,我收到消息“ xxxxxxxx.AddData 不是函数”。

If can help this is the cose I am using https://jsfiddle.net/96u30Lht/ (I cannot find an url to chart.js 2 lib in my fiddle as a resource file this should be the reason why the canvas stays black)如果可以提供帮助,这就是我使用https://jsfiddle.net/96u30Lht/ 的原因(我在我的小提琴中找不到指向 chart.js 2 lib 的 url 作为资源文件,这应该是画布保持黑色的原因)

I'm using v2.0.0 from the dev branch (the file header reads 2.0.0-alpha, and the file I'm looking at is https://github.com/nnnick/Chart.js/blob/f3eb6f4a433b4f34a582842dcf7b42f710861a7d/Chart.js )我正在使用 dev 分支中的 v2.0.0(文件头读取 2.0.0-alpha,我正在查看的文件是https://github.com/nnnick/Chart.js/blob/f3eb6f4a433b4f34a582842dcf7b42f710861a7d/Chart .js )

You need to make a few changes to get your fiddle to work你需要做一些改变才能让你的小提琴工作


Make the Chart Type an Option使图表类型成为一个选项

I think v2.0 has only one constructor for all chart types.我认为 v2.0 对所有图表类型只有一个构造函数。 So所以

chartdata = new Chart(ctx).Line({图表数据 = 新图表(ctx)。线({

should be应该

chartdata = new Chart(ctx, {
    type: "line",

Add a Type for the 2nd y-axis为第二个 y 轴添加类型

scaleType: "linear"比例类型:“线性”

to

type: "linear",

type is the property name. type 是属性名称。 It doesn't cause any problem for the first y axis since it's optional there (your scaleType: "linear" on the first instance doesn't actually do anything and you can drop it)它不会对第一个 y 轴造成任何问题,因为它在那里是可选的(第一个实例上的scaleType: "linear"实际上并没有做任何事情,您可以删除它)


Remove the window.onload删除window.onload

Your fiddle is already set to execute onLoad您的小提琴已设置为执行 onLoad


Change the Method Signature更改方法签名

As far as I could see from the code, the method signature has changed for addData.从代码中可以看出,addData 的方法签名已更改。 It is now as follows (From the code) - note that this could change anytime现在如下(来自代码) -请注意,这可能随时更改

// Add data to the given dataset // 添加数据到给定的数据集
// @param data: the data to add // @param data: 要添加的数据
// @param {Number} datasetIndex : the index of the dataset to add to // @param {Number} datasetIndex : 要添加到的数据集的索引
// @param {Number} index : the index of the data // @param {Number} index : 数据的索引
addData: function addData(data, datasetIndex, index) addData: 函数 addData(data, datasetIndex, index)

So a better version of your setInterval would be所以你的 setInterval 的更好版本是

setInterval(function () {
    var latestlabel = chartlabel[6];

    chartdata.addData(12, 0, latestlabel);
    chartdata.addData(5, 1, latestlabel);
    chartdata.removeData(0, 0);
    chartdata.removeData(1, 0);

}, 7000);

https://jsfiddle.net/bsgc9tu7/ https://jsfiddle.net/bsgc9tu7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM