【问题标题】:How to add datas to chart js from javascript array itself?如何从javascript数组本身向图表js添加数据?
【发布时间】:2013-11-28 12:57:05
【问题描述】:

我想在数据结构数据中使用chart 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?

【问题讨论】:

    标签: javascript jquery jquery-plugins charts chart.js


    【解决方案1】:

    您必须创建Array's,填充它们,并且只在对象内键入数组名称,而无需用[] 包围它

    示例:

    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
                    }
                ]
    
            }
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2021-02-01
      • 1970-01-01
      • 2021-10-17
      • 2012-10-11
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 2015-07-15
      相关资源
      最近更新 更多