javascript 如何从javascript数组本身向图表js添加数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20266849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add datas to chart js from javascript array itself?
提问by sun
I would like to use chart jsin there at data structure data, if i give number as
我想在数据结构数据中使用图表 js,如果我给数字作为
data : [40,80,5,190,56,55,40]
working fine. If i give a array variable or string variable which holds that number like
data : [40,80,5,190,56,55,40]
工作正常。如果我给出一个数组变量或字符串变量来保存该数字,例如
var myvalues = my_array.toString();
alert(myvalues);
am getting 5,10,14,18 for the variable as well as for array. Now when i use the array or string with the chart data i can't able to get the chart if i try like the below
对于变量和数组,我得到了 5,10,14,18。现在,当我将数组或字符串与图表数据一起使用时,如果我尝试如下所示,我将无法获得图表
data : [myvalues]
with full code for barChartData
带有 barChartData 的完整代码
var barChartData = {
labels: [description],
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
scaleOverride: true,
scaleSteps: 100,
stepValue: 1,
barShowStroke: false,
data: [myvalues]
},
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,1)",
scaleOverride: true,
scaleSteps: 100,
barShowStroke: false,
stepValue: 1,
data: [myvalues]
}
]
}
Here description
is the another variable holding information about the label this too not working.
这description
是另一个保存有关标签信息的变量,这也不起作用。
How to assign data and labels from javascript array or string to the chart js?
如何将 javascript 数组或字符串中的数据和标签分配给图表 js?
回答by Paulo Roberto Rosa
You have to create the Array
's, populate them, and only type the array name inside object without needing to surround it with [
and ]
您必须创建Array
's,填充它们,然后只在 object 内键入数组名称,而无需用[
和]
Example:
例子:
var description = new Array();
description.push('a');
description.push('b');
var myvalues = new Array();
myvalues.push('c');
myvalues.push('d');
var barChartData = {
labels: description,
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
scaleOverride: true,
scaleSteps: 100,
stepValue: 1,
barShowStroke: false,
data: myvalues
},
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,1)",
scaleOverride: true,
scaleSteps: 100,
barShowStroke: false,
stepValue: 1,
data: myvalues
}
]
}