如何为chart.js线图动态创建数据集? [英] How to create datasets dynamically for chart.js Line chart?
问题描述
我想在chart.js库中动态创建包含多个数据集的折线图。
I want to create a Line chart with multiple datasets dynamically in the chart.js library.
我能够动态分配数据。但我想动态创建数据集本身。我看到下面的链接:
I am able to assign the data dynamically. But I want to create the datasets itself dynamically. I saw the link below:
并尝试过这个:
var datasetValue = [];
for (var j = 0; j < count; j++) {
datasetValue[j] = [
{
fillColor: 'rgba(220,220,220,0.5)',
strokeColor :'rgba(220,220,220,1)' ,
title :'2013',
data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
}]
}
var mydata = {
datasets : datasetValue
}
这里的计数值是3,我想在图表中显示3行,计数值会有所不同。即使图表线颜色和标题相同,我想先显示该行,并在解决后更改其余部分。
Here the count value is 3 and I want to display the 3 lines in the chart and the count value will vary. Even though the chart line color and title will be the same, I want to display the line first and will change the rest once this is solved.
我试图访问数据如下:
alert("Datasets:"+mydata.datasets[0].data);
它应显示第一个数据集的数据,但显示 undefined
。
It should show the data of the first dataset but it's showing undefined
.
但在以下情况中:
var mydata1 = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
data : [95,53,99,73,27,82,40],
title : "2014"
},
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
data : [35,43,59,31,50,66,55],
title : "2013"
}
]
}
alert("Datasets:"+mydata1.datasets[0].data);
我能够获得第一个数据集的数据。有谁能请给我解决方案?
I am able to get the data of first dataset. Could anyone please give me the solution?
推荐答案
我认为你正在寻找类似以下解决方案的东西。
http://jsfiddle.net/5m63232a/
I think you are looking for something like the following solution. http://jsfiddle.net/5m63232a/
var datasetValue = [];
var count = 10;
for (var j=0; j<count; j++) {
datasetValue[j] = {
fillColor: 'rgba(220,220,220,0.5)',
strokeColor :'rgba(220,220,220,1)',
title :'2013',
data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
}
}
var mydata = {
datasets : datasetValue
}
alert("Datasets: "+mydata.datasets[0].data);
这篇关于如何为chart.js线图动态创建数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!