I am attempting to develop a dynamic chart by utilizing data retrieved from a function housed inside a JSON object.
The JSON object is fetched through the Relayr Javascript API, and looks like this:
relayr.devices().getDeviceData({
token: toke,
deviceId: Candle1_deviceId,
incomingData: function (data) {
console.log(data.readings[0].meaning);
console.log(data.readings[0].value);
return data.readings[0].value;
}
});
My objective is to incorporate data.readings[0].value into the chart as shown below:
window.onload = function () {
var dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Westminster Cathedral Building Movement Data"
},
data: [{
type: "line",
dataPoints: dps
}]
});
var xVal = 0;
var yVal;
var updateInterval = 100;
var dataLength = 500; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1;
for (var j = 0; j < count; j++) {
yVal = yVal + relayr.devices().getDeviceData.incomingData;
dps.push({
x: xVal,
y: yVal
});
xVal++;
};
if (dps.length > dataLength) {
dps.shift();
}
chart.render();
};
updateChart(dataLength);
setInterval(function () {
updateChart()
}, updateInterval);
}
The code above is adapted from an example on CanvasJS.