在 chart.js 数据和标签字段中使用数组值 [英] using array values in chart.js data and label field

查看:11
本文介绍了在 chart.js 数据和标签字段中使用数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将数组的值传递给 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
        }]
    }    
});

这篇关于在 chart.js 数据和标签字段中使用数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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