将json数据传递给chartjs

时间:2018-03-28 23:09:21

标签: json charts chart.js

JSON.js

    var Jsondata = {
            "label1": 0,
            "label2": 0,
            "label3": 2,
            "label4": 0,
            "label5": 0
        }
var labels = //?? have to get the labels from the Jsondata
var data = //?? have to get the numbers from the Jsondata

我正在使用它来制作使用chartjs的折线图,代码如下。

 type: 'line', 
          data: { 

              labels: labels  //["label1","label2","label3","label4", "label5""], 

 datasets: [{ 
              label:'',                   
              fill: 'false', 
              data: data  //[0, 3, 6, 4, 5,3,2], 

1 个答案:

答案 0 :(得分:1)

您可以使用内置的Object助手keys()values()

var Jsondata = {
            "label1": 0,
            "label2": 0,
            "label3": 2,
            "label4": 0,
            "label5": 0
    }
var labels = Object.keys(Jsondata)
var data = Object.values(Jsondata)

标签: ["label1", "label2", "label3", "label4", "label5"]

数据: [0, 0, 2, 0, 0]