I would like to create a simple node that will read data from API and build a graph. Now I am stuck on pushing the data to the chart array. Can't find what is the problem here.
I'm quite new in programming, sorry if this topic is quite simple
Here is my code:
var test_obj = [{ "Received": 1667342689, "OperationState": 3, "BatterySoC": 36 }, { "Received": 1667342749, "OperationState": 3, "BatterySoC": 20 }];
var all_keys = Object.keys(test_obj[0]);
/*
CHART VARIABLE SHOULD BE LIKE THIS:
var chart = [{
"series": ["OperationState", "BatterySoC"],
"data": [[{"x":1667342689, "y":3 },{"x":1667342749, "y":3}], //Operation State
[{"x":1667342689, "y":36},{"x":1667342749, "y":20}]], //BatterySoC
"labels": ["OperationState", "BatterySoC"]
}];
*/
var chart = [{
"series": [],
"data": [],
"labels": []
}];
for (var i = 1; i < all_keys.length; i++) {
chart[0].series.push(all_keys[i]);
chart[0].data.push([]);
chart[0].labels.push(all_keys[i]);
}
for (var j=0; j < test_obj.length; j++) {
var temp_array = Object.entries(test_obj[j]);
for (var k=1; k < temp_array.length; k++) {
var example = { "x": temp_array[0][1], "y": temp_array[k][1] };
chart[0].data[k].push(example); //Can't push data to the chart variable here
}
}
msg.payload = chart;
return msg;
Does your api give you a batch of historical data or are you trying to draw the chart as it happens?
You have not shown us what the incoming data look like. See this canned text for how you can display the data in the debug window and then copy/paste a selection of it here for us to look at:
There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.
Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.
Since your data is nicely typed { "Received": 1667342689, "OperationState": 3, "BatterySoC": 36 } you can map them almost directly into the chart data...
@E1cid your solution works fine, thank you! Your code looks much better, but a bit complicated for me, at least for now Will try to figure out how it works!