简体   繁体   English

在chart.js 数据和标签字段中使用数组值

[英]using array values in chart.js data and label field

I wish to pass values of an array to the data and label fields of the chart.js dataset.我希望将数组的值传递给 chart.js 数据集的数据和标签字段。

Here the code from success of ajax call made to fetch json data.这里是获取 json 数据的 ajax 调用成功的代码。 I fetch the json data and store it into an array.我获取 json 数据并将其存储到一个数组中。

Data = jQuery.parseJSON(result);
var count = Data.length;
var counter = 0;
while(count > 0) {
    LabelResult[counter] =[Data[counter].TIME];
    counter++;
    count --;
}

Now i wish to use this label values into the labels filed.现在我希望将此标签值用于归档的标签中。

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [LabelResult],
        datasets: [{
            label: '# of Votes',
            data: [DataResult],
            borderWidth: 1
        }]
    }    
});

But there seems some issue and the data is not getting rendered on the chart但似乎有一些问题,数据没有在图表上呈现

LabelResult is an array, change LabelResult 是一个数组,更改

labels: [LabelResult]

to

labels: LabelResult

Also:还:

data: [DataResult]

to

data: DataResult

Like:喜欢:

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: LabelResult,
        datasets: [{
            label: '# of Votes',
            data: DataResult,
            borderWidth: 1
        }]
    }    
});

I think you could try to remove some brackets.我认为您可以尝试删除一些括号。

while(count > 0){
     LabelResult[counter] = Data[counter].TIME; // here removed brackets
      counter++;
      count --;
}    

and

data: {
    labels: LabelResult, // here removed brackets
    datasets: [{
        label: '# of Votes',
        data: DataResult, // here removed brackets
        borderWidth: 1
    }]
},  

I hope that will works.我希望这会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM