Javascript 从fetchforjson获取数据
我有一个JSON,如下所示:Javascript 从fetchforjson获取数据,javascript,chart.js,fetch,Javascript,Chart.js,Fetch,我有一个JSON,如下所示: { "item":{ "Labels": [ "Jan", "March", "July" ], "Data": [ 1, 20, 30 ] } } 我需要使用fetchjs将数据值传递到ChartJS 我有下面的代码 fetch(path).then(function (response) { return response.json(); }).then(function (data) { console.log(data.item
{
"item":{
"Labels": [ "Jan", "March", "July" ],
"Data": [ 1, 20, 30 ]
}
}
我需要使用fetchjs将数据值传递到ChartJS
我有下面的代码
fetch(path).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data.item.Data);
// Chart Goes here
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: data.Labels,
datasets: [{
label: '# of Votes',
data: data.item.Data,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
});
但是这不适用于ChartJS->data.item.data看起来您忘记了
标签:data.labels
:)中的某些内容,也应该是标签:data.item.labels