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

2022-01-22 00:00:00 arrays javascript ajax chart.js chart.js2

我希望将数组的值传递给 chart.js 数据集的数据和标签字段.

I wish to pass values of an array to the data and label fields of the chart.js dataset.

这里是 ajax 调用成功获取 json 数据的代码.我获取 json 数据并将其存储到一个数组中.

Here the code from success of ajax call made to fetch json data. I fetch the json data and store it into an array.

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是一个数组,改变

LabelResult is an array, change

labels: [LabelResult]

labels: LabelResult

还有:

data: [DataResult]

data: DataResult

喜欢:

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

相关文章