Hi Steve,
Thanks for getting back to me - I'm trying to follow your code but I was hoping node-red would have functions to do this rather tha needing to write JS as i have very limited coding experiance.
Looking at your code I need to declare the data type which is an array []
so
var chartData = [];
Is it best pracise to call let or var for these types of declarations or does it only matter if you want to use these declaed variables inside other functions?
After declaring the array I then need to declare an object to hold everything inside
var element = {};
Looking at the chart documentation it requires the json string to have a series[]
, data[]
and a labels[]
so we need to build this by adding the name of these to the object{}
element.series = []
element.data = [];
element.label = [];
Then to add something to each of these we use the .push tag followed by the the data so
element.series.prush('system pressure');
element.label.push('');
To add dynamic data we need to loop through the incomming array from the OPC UA client to determine how many items are there. So we need to create a variable to hold the number of items in the array inorder for us to iterate over the correct number of times.
var x = 0;
Since the data is coming into the function as a payload from the msg tag so.
var data = msg.payload;
To determine the length of the array we use the .length() function
var len = data.length;
Then to cycle through all the data in the incoming message (which should be an array not a string other wise we would return the total number of characters?) then.
for(x = 0; x < len; x++){
element.data.push({
x : x,
y : data[x]
})
}
We then take this new element.data and place it inside the chartData which has the series and label elements
chartData.push(element);
To send it to the chart we then have to add this to the msg.payload and return it so the chart fucntion can read it
msg.paylod = chartData;
return msg;
Thats as far as i get - i seem to get the correct json string in the debugger but the plot doesn't seem to update or show any data am i doing something daft?