I am struggling to get the formatting correct for my bar chart. Sometimes, I do not have data for all 7 machines. I am having problems figuring out my loop in the function node. Here is what I have (no loop).
var arrayLength = msg.payload.length;
for (var i = 0; i < arrayLength; i++) {
var m = {};
m.labels = ["Available Time",
msg.payload[i].Machine];
m.data = [[480,
(msg.payload[i].TotalTime/60).toFixed(2)]];
m.series = ["Total Time in 8 hours by Machine"];
return {payload:[m],topic:msg.topic};
}
That is because you are returning at the end of the first pass so it doesn't get to do the rest. You need to build the arrays up. So declare the arrays just with the first (fixed) element
m.labels = ["Available Time"];
m.data = [[480]];
Then have your loop and use the Array.push() to add new items onto the array. So you will have something like
var m = {};
m.labels = ["Available Time"];
m.data = [[480]];
m.series = ["Total Time in 8 hours by Machine"];
var arrayLength = msg.payload.length;
for (var i = 0; i < arrayLength; i++) {
m.labels.push((msg.payload[i].Machine));
m.data[0].push((msg.payload[i].TotalTime/60).toFixed(2));
}
return {payload:[m],topic:msg.topic};