如何从javascript数组本身向图表js添加数据? [英] How to add datas to chart js from javascript array itself?

查看:134
本文介绍了如何从javascript数组本身向图表js添加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据结构数据中使用 chart js ,如果我给出数字为

I would like to use chart js in there at data structure data, if i give number as

数据:[40,80,5,190,56,55,40] 工作正常。如果我给出一个数组变量或字符串变量,其中包含该数字,如

data : [40,80,5,190,56,55,40] working fine. If i give a array variable or string variable which holds that number like

 var myvalues = my_array.toString(); 
   alert(myvalues);

变量和数组得到5,10,14,18。现在,当我使用数组或字符串与图表数据时,如果我尝试下面的话,我就无法获得图表

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

data : [myvalues]

包含barChartData的完整代码

with full code for 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 是另一个变量,其中包含有关标签的信息,这也无法正常工作。

Here description is the another variable holding information about the label this too not working.

如何将javascript数组或字符串中的数据和标签分配给图表js?

How to assign data and labels from javascript array or string to the chart js?

推荐答案

你必须创建数组,填充它们,只在对象中键入数组名称,而不需要用 []

You have to create the Array's, populate them, and only type the array name inside object without needing to surround it with [ and ]

示例:

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

        }

这篇关于如何从javascript数组本身向图表js添加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆