I am using node-red to populate a chart using data from PLC. The data comes in as a object, then i convert it to a 2D array 36 items long (the object has 36 elements):
for(var i in pressCurve){
curveArray.push([i, pressCurve [i]]);
then to get this data onto a chart, I create a new msg.payload object in the format:
msg.payload =[{
"series": ["title 1", "title2"...],
"data": [[{Array of objects}]],
"labels": ["label1", "label2"...];
populating the "data" value as my array of objects via hardcoding is easy:
msg.payload = [{
"series": ["Curve"],
"data": [
[{"x": 10, "y": curveArray[0][1]},
{"x": 20, "y": curveArray[1][1]},
{"x": 30, "y": curveArray[2][1]},
{"x": 40, "y": curveArray[3][1]},
{"x": 50, "y": curveArray[4][1]},
{"x": 60, "y": curveArray[5][1]},
{"x": 70, "y": curveArray[6][1]},
{"x": 80, "y": curveArray[7][1]},
{"x": 90, "y": curveArray[8][1]},
{"x": 100, "y": curveArray[9][1]},
{"x": 110, "y": curveArray[10][1]},
{"x": 120, "y": curveArray[11][1]},
{"x": 130, "y": curveArray[12][1]},
{"x": 140, "y": curveArray[13][1]},
{"x": 150, "y": curveArray[14][1]},
{"x": 160, "y": curveArray[15][1]},
{"x": 170, "y": curveArray[16][1]},
{"x": 180, "y": curveArray[17][1]},
{"x": 190, "y": curveArray[18][1]},
{"x": 200, "y": curveArray[19][1]},
{"x": 210, "y": curveArray[20][1]},
{"x": 220, "y": curveArray[21][1]},
{"x": 230, "y": curveArray[22][1]},
{"x": 240, "y": curveArray[23][1]},
{"x": 250, "y": curveArray[24][1]},
{"x": 260, "y": curveArray[25][1]},
{"x": 270, "y": curveArray[26][1]},
{"x": 280, "y": curveArray[27][1]},
{"x": 290, "y": curveArray[28][1]},
{"x": 300, "y": curveArray[29][1]},
{"x": 310, "y": curveArray[30][1]},
{"x": 320, "y": curveArray[31][1]},
{"x": 330, "y": curveArray[32][1]},
{"x": 340, "y": curveArray[33][1]},
{"x": 350, "y": curveArray[34][1]},
{"x": 360, "y": curveArray[35][1]}]],
"labels": ["Curve"]
}]
However, i need to do this for 9 different channels of data, so that is a huge amount of redundant code.
What i'd like to do is something like,
msg.payload = [{
"series": ["Curve"],
"data": [for(var j = 0; j < 36; j++)){
var c = 10;
[{"x" : c, "y": curveArray[i][1]},
c = c + 10;
]],
to loop through each element in the array, and populate the "data" object property in the format above.
I can't seem to be able to use var inside the object property. Is this possible to do?
You can try this :
msg.payload =[{
"series": ["Cruve"],
"data": pressCurve.map((element, i) => ({ x: i, y: element }) ),
"labels": ["Curbe Label"]
}];
Can you share the array of data that you use to fill "series" and "labels" I think my code can be completed ;)