Javascript 在chart.js 数据和标签字段中使用数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38869815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using array values in chart.js data and label field
提问by Salil Lambay
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. I fetch the json data and store it into an array.
这里是获取 json 数据的 ajax 调用成功的代码。我获取 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
但似乎有一些问题,数据没有在图表上呈现
回答by tata.leona
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
}]
}
});
回答by Emil Pausz
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.
我希望这会奏效。