Line Chart - Overlay 2 different line charts from datasets that don't have the same # of elements

I need to display a line chart containing 2 different sets of data (series) that i receive from my epics nodes .

[The first data-set is 2 Arrays for Current Signal containing 197 elements - one array for x-co-ordinates and one for y co-ordinates
The second data-set is 2 Arrays for Golden Standard containing 30 elements - one for x co-ordinates and one for y co-ordinates]
Both datasets have values that fall within the same range on the X-axis chart

I have an issue when i plot this data to a line graph causing the chart to show incorrectly

I've illustrated the problem I'm having by writing some simple code to plot 2 data-sets (Array 1 - ten element, Array 2 - five elements) and hard-coding the info in the arrays into my code to simulate my problem - Looking at the graph and comparing the data we see that the chart is not rendering the lines correctly. Basically When the chart is plotted Array1 is plotted correctly but the Array2 Y-coordinates which has 5 elements are plotted on the first 5 X co-ordinates of Array1 graph no matter what the Array2 X co-ordinates are. I've tried various ways of fixing this issue but no joy.

Some of the fixes I've tried are are to pass the second data-set to another variable n, and then plotting both series on the same graph by a return {payload:[m,n]}; but also no luck.

I need to be able to plot this data correctly. Any help would be appreciated

Also if any one has any ideas on how to only enlarge one set of pts for a particular line in a line chart instead of all the lines in the chart like the option the line chart gives you your answer would be appreciated. The example code is provided below

[{"id":"81b58820.dd8428","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"f3d4b953.e2b008","type":"function","z":"81b58820.dd8428","name":"Normalize&MergeXY-Array","func":"context.X = context.X || 0.00;    \ncontext.X = [0,10,20,30,40,50,60,70,80,90] //Array 1 X-CoOrdinates\n\ncontext.Y = context.Y || 0.00;    \ncontext.Y = [5,15.5,12.5,23,17,99,45,11,27,5]   //Array 1 Y-CoOrdinates\n\ncontext.GX = context.GX || 0.00;      \ncontext.GX = [0,15,35,55,90]         //Array 2 X-CoOrdinates\n\ncontext.GY = context.GY || 0.00;      \ncontext.GY = [2,45,10,30,89]        //Array 2 Y-CoOrdinates\n\n\n\n\nvar m={};\nm.labels = context.X,context.GX;\nm.series = ['Current Signal','Golden Standard'];\nm.data = [\n    context.Y,\n    context.GY,\n    ];\n\nreturn {payload:[m]};\n\n","outputs":1,"noerr":0,"x":440,"y":300,"wires":[["d3cd2613.1882f","1ec950b6.a3e73f"]]},{"id":"d3cd2613.1882f","type":"ui_chart","z":"81b58820.dd8428","name":"","group":"34e8b5e8.fdddc2","order":2,"width":20,"height":7,"label":"Normalized","chartType":"line","legend":"true","xformat":"Energy","interpolate":"linear","nodata":"NO DATA","dot":true,"ymin":"-5","ymax":"105","removeOlder":"1100","removeOlderPoints":"10500","removeOlderUnit":"1","cutout":0,"useOneColor":true,"colors":["#2ca02c","#f97a2c","#5ce0cc","#2c3aa0","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":710,"y":320,"wires":[[]]},{"id":"1ec950b6.a3e73f","type":"debug","z":"81b58820.dd8428","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":710,"y":280,"wires":[]},{"id":"c1716b5b.e13338","type":"inject","z":"81b58820.dd8428","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":300,"wires":[["f3d4b953.e2b008"]]},{"id":"34e8b5e8.fdddc2","type":"ui_group","z":"","name":"Chart","tab":"f3f2f651.2f6dd","order":1,"disp":false,"width":"26","collapse":false},{"id":"f3f2f651.2f6dd","type":"ui_tab","z":"","name":"Question - How to display 2 different dimension arrays on Line Chart correctly","icon":"dashboard","disabled":false,"hidden":false}]

question.json (2.1 KB)

Hi. Similar problems have been here many times. This is not straightforward as it takes deeper modification for chart. By default the x-axis is in "time" mode. This must be changed to be "linear". Luckily there is option to do so. Chart options can be changed by using msg.ui_control. And there is way more things you can modify to get chart render in the way you like. Pointing out directly the axes part of documentation but take the time to explore more. https://www.chartjs.org/docs/latest/axes/

Now as starting point for you, I modified your flow to render lines correctly. I left your data as it was so you can understand better the way it works.
In addition, the options should be sent only when chart needs to be changed, not with every data change. (definitely you'll need to send options every time after deploy because of chart initializes to defaults) So the ui_control part should be separated from the function where you send the data. (or left out from outgoing message when you know that options are good for that moment and for data you are going to feed.)

[{"id":"dddd8dbb.45cec","type":"function","z":"2eb6025c.98cc3e","name":"Normalize&MergeXY-Array","func":"context.X = context.X || 0.00;    \ncontext.X = [0,10,20,30,40,50,60,70,80,90] //Array 1 X-CoOrdinates\n\ncontext.Y = context.Y || 0.00;    \ncontext.Y = [5,15.5,12.5,23,17,99,45,11,27,5]   //Array 1 Y-CoOrdinates\n\ncontext.GX = context.GX || 0.00;      \ncontext.GX = [0,15,35,55,90]         //Array 2 X-CoOrdinates\n\ncontext.GY = context.GY || 0.00;      \ncontext.GY = [2,45,10,30,89]        //Array 2 Y-CoOrdinates\n\n\nvar line_1 = []\nvar i\nfor(i = 0;i< context.X.length;i++){\n    line_1.push({x:context.X[i],y:context.Y[i]})\n}\nvar line_2 = []\nfor( i = 0;i< context.GX.length;i++){\n    line_2.push({x:context.GX[i],y:context.GY[i]})\n}\n\n\nvar m={};\nm.labels = context.X,context.GX;\nm.series = ['Current Signal','Golden Standard'];\nm.data = [\n   line_1,\n    line_2,\n];\n    \n    \n\nvar opt= {\n    legend: {\n        display: true\n    },\n   scales: {\n        xAxes: [{\n            gridLines:{\n                color:'rgba(0, 0, 0, 0.2)',\n                zeroLineColor:'rgba(0, 0, 0, 0.2)'\n            },\n            type: 'linear',\n            position:'bottom',\n            ticks: {\n                fontColor:\"#ccc\",\n                max: 100,\n                min: 0,\n            }\n        }],\n        yAxes: [{\n            gridLines:{\n                color:'rgba(0, 0, 0, 0.2)',\n                zeroLineColor:'rgba(0, 0, 0, 0.2)'\n            },\n            ticks: {\n                fontColor:\"#ccc\",\n            }\n        }]\n   }\n}\n \n\nreturn {payload:[m],ui_control:{options:opt}};\n\n","outputs":1,"noerr":0,"x":440,"y":300,"wires":[["4d6440d2.72bf4","56fc838d.3310cc"]]},{"id":"4d6440d2.72bf4","type":"ui_chart","z":"2eb6025c.98cc3e","name":"","group":"f81595d1.755b48","order":2,"width":20,"height":7,"label":"Normalized","chartType":"line","legend":"true","xformat":"Energy","interpolate":"linear","nodata":"NO DATA","dot":true,"ymin":"-5","ymax":"105","removeOlder":"1100","removeOlderPoints":"10500","removeOlderUnit":"1","cutout":0,"useOneColor":true,"colors":["#2ca02c","#f97a2c","#5ce0cc","#2c3aa0","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":700,"y":320,"wires":[[]]},{"id":"56fc838d.3310cc","type":"debug","z":"2eb6025c.98cc3e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":280,"wires":[]},{"id":"468de8ba.e3be88","type":"inject","z":"2eb6025c.98cc3e","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":300,"wires":[["dddd8dbb.45cec"]]},{"id":"f81595d1.755b48","type":"ui_group","z":"","name":"Chart","tab":"f859ce5d.fc0ba","order":1,"disp":false,"width":"26","collapse":false},{"id":"f859ce5d.fc0ba","type":"ui_tab","z":"","name":"Question - How to display 2 different dimension arrays on Line Chart correctly","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Thank you. This helps a lot. I've modified my code accordingly and am able to plot the Curves correctly. Your help was much appreciated as i spent a few days trying to solve this issue on my own with no luck

1 Like