如何从javascript数组本身向图表js添加数据?
我想在数据结构数据中使用图表js ,如果我给出数字为
data : [40,80,5,190,56,55,40]
工作正常。 如果我给出一个数组变量或字符串变量,其中包含该数字
var myvalues = my_array.toString(); alert(myvalues);
我得到5,10,14,18变量以及数组。 现在当我使用数组或字符串与图表数据时,如果我尝试如下,我无法获得图表
data : [myvalues]
使用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] } ] }
这里的description
是另一个变量,它保存有关标签的信息,这也无法正常工作。
如何将javascript数组或字符串中的数据和标签分配给图表js?
你必须创建Array
,填充它们,只在对象内键入数组名称,而不需要用[
和]
包围它
例:
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 } ] }