admin管理员组文章数量:1401134
I've been looking all over chart.js
-related questions, but not two developers seem to be giving the same answer on how to display a chart using chart.js
+ JSON.
I am trying to display a chart using a JSON file - specifically a list of "amounts" with their relative labels ("January 2017",...).
The chart canva display just fine, no console log error, but no chart itself. What am I missing?
Thanks!
Here my chart.js code:-
var labels = [];
var data = [];
$.getJSON(""), function (data) {
$.each(data.customers.amounts, function(key, value){
var labels = json.map(function(item) {
labels.push(item.key);
});
var data = json.map(function(item) {
data.push(item.value);
});
});
}
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
labels: labels,
backgroundColor: 'rgb(129, 198, 2228)',
borderColor: 'rgb(0, 150, 215)',
data: data
}]
},
options: {
responsive: 'true',
}
});
and here's my JSON file:-
{
"customers": [
{
"first_name": "John",
"last_name": "Doe",
"account": "123456",
"period": "13th July - 13th August",
"due_date": "14th September",
"amounts": [
["January 2017", 121.23],
["February 2017", 145.23],
["March 2017", 55.12],
["April 2017", 78.58],
["May 2017", 89.13],
["June 2017", 45.78],
["July 2017", 90.22]
]
}
]
}
I've been looking all over chart.js
-related questions, but not two developers seem to be giving the same answer on how to display a chart using chart.js
+ JSON.
I am trying to display a chart using a JSON file - specifically a list of "amounts" with their relative labels ("January 2017",...).
The chart canva display just fine, no console log error, but no chart itself. What am I missing?
Thanks!
Here my chart.js code:-
var labels = [];
var data = [];
$.getJSON("https://jsonblob./api/jsonBlob/26078b70-6b6f-11e7-a38a-bf689f57642c"), function (data) {
$.each(data.customers.amounts, function(key, value){
var labels = json.map(function(item) {
labels.push(item.key);
});
var data = json.map(function(item) {
data.push(item.value);
});
});
}
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
labels: labels,
backgroundColor: 'rgb(129, 198, 2228)',
borderColor: 'rgb(0, 150, 215)',
data: data
}]
},
options: {
responsive: 'true',
}
});
and here's my JSON file:-
{
"customers": [
{
"first_name": "John",
"last_name": "Doe",
"account": "123456",
"period": "13th July - 13th August",
"due_date": "14th September",
"amounts": [
["January 2017", 121.23],
["February 2017", 145.23],
["March 2017", 55.12],
["April 2017", 78.58],
["May 2017", 89.13],
["June 2017", 45.78],
["July 2017", 90.22]
]
}
]
}
Share
Improve this question
edited Jul 24, 2017 at 8:09
Peter Haddad
81k26 gold badges145 silver badges148 bronze badges
asked Jul 24, 2017 at 5:40
StefanStefan
831 gold badge2 silver badges7 bronze badges
1
-
you are accessing amounts using
data.customers.amounts
, butcustomers
itself is another, shouldn't you access it usingdata.customers[0].amounts
– Akash Shinde Commented Jul 24, 2017 at 5:44
1 Answer
Reset to default 4Couple of Issues :
- since
$.getJSON()
method is asynchronous, you should construct the chart inside it's callback function. - you are looping through the response data incorrectly. could be as simple as :
var labels = data.customers[0].amounts.map(function(e) {
return e[0];
});
var data = data.customers[0].amounts.map(function(e) {
return e[1];
});
- you are adding
labels
array to your dataset, while it belogns to thedata
object.
Here is the revised version of your code :
$.getJSON("https://jsonblob./api/jsonBlob/26078b70-6b6f-11e7-a38a-bf689f57642c", function(data) {
var labels = data.customers[0].amounts.map(function(e) {
return e[0];
});
var data = data.customers[0].amounts.map(function(e) {
return e[1];
});
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
backgroundColor: 'rgb(129, 198, 2228)',
borderColor: 'rgb(0, 150, 215)',
data: data
}]
},
options: {
responsive: 'true',
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>
本文标签: javascriptUse JSON file with chartjsStack Overflow
版权声明:本文标题:javascript - Use JSON file with chart.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744283785a2598782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论