I want to plot with chart.js using json data I query with getjson.
The code below gives me and empty plot. However if I uncomment the line with alert(ddata.length); not only it pops up with a 0 but the plot comes up correctly this time - confusing behaviour. Using chart.js 2.6.0 jquery 3.3.1.
var ddata = [];
//var labels = [];
$.getJSON("http://192.168.1.143/json_request", function(jdata){
$.each(jdata, function(i, d){
ddata.push({"x":d.timestamp, "y": d.x0});
//labels.push(String(d.timestamp));
});
});
//alert(ddata.length); // <-- Works if uncommented !!!
//console.table(ddata);
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
//labels: labels,
datasets: [{
label: 'h1px2',
data: ddata,
backgroundColor: 'rgba(0, 0, 255, 0.2)',
borderColor: 'rgba(0,0,255,1)',
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
type: 'linear'
}]
}
}
});
A sample of the json:
[{"timestamp": 1528828783,"x0": 246.21,"x1": 1.16,"x2": 11.47,"x3": 8.62,"x4": -127.00},
{"timestamp": 1528828793,"x0": 245.75,"x1": 1.37,"x2": 15.41,"x3": 4.93,"x4": -127.00}, etc...
Would you be able to tell how I can get this plotted correctly?