I have a problem with parsing data to my chart. For sure, this should be included in an automatic process I use getJSON and don't want to use ajax, so I have:
$.getJSON('file.json').done( function (results) {
var labels = [];
var data = [];
var labels = results.map(function (item) {
return item.updatedLaels
});
var data = results.map(function (item) {
return item.updatedData;
});
console.log(labels)
console.log(data)
});
So of course labels and data are logged to console in a proper way e.g.:
(5) [1,2,3,4,5]
But how to use it in this part:
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Example',
data: data,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
}]
},
});
Because index.html generates an empty chart without data. How to map labels and data in the proper way?
$.getJSONis anasynccall andlabelsanddataare local variables. Those variables areundefinedout of the scope of yourcallback function.