// get current time and context array
var now = Date.now();
var off = new Date(70, 0, 1) - new Date(0);
var arr = context.get("data") || [];
// reset the data if empty array is received
if (Array.isArray(msg.payload)) {
if (msg.payload.length === 0) {
context.set("data", msg.payload);
return msg; // to clear the chart
}
else {
arr = msg.payload;
}
}
else {
// get/set the initial (absolute) timestamp
var dt0 = arr.length ? arr[0][0] : now;
if (arr.length === 0) {
arr.push([ dt0, 0 ]);
}
// calculate time since last msg
var dt1 = msg.timestamp || now;
var dlt = (dt1 - dt0) + off;
// add new relative data point to array
arr.push([ dlt, msg.payload ]);
}
context.set("data", arr);
// build the chart msg with the array
var key = msg.topic || "Test Data";
msg.payload = [{ "key": key, "values": arr.slice(1) }];
return msg;