Plotting arrays

Hi, I'm new to JS having come from a LabVIEW background. I have an instrument that is returning a single column array of pressures which is connected to node-red using OPC UA client. The size of this array varies.

I'm trying to plot all values on to a dash chart but i have no idea how to convert this into the right JSON format that the chart node needs inorder to plot?

I woudl like subsequent data to be appended to the graph every second. This is probably very trivial for someone who codes in JS but i've been stuck on this for days!

Hi, try following this thread.

I put a lot of effort into explaining how it all fits together. It might be of some use to get you started.

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?

@om327 You may of noticed that your last post did not display correctly, for example your brackets [] looked like
Any code that you post in the forum must be added in a certain format - see How to share code or flow json

I have edited the post above and corrected it for you, but please see the link above for future posts containing code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.