Random Number Chart

I want to put my array of random number into chart. And I only got this. Maybe someone can help me. And here is the flow

[{"id":"60f6e9da.252998","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"5742a57a.17f17c","type":"inject","z":"60f6e9da.252998","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":70,"y":100,"wires":[["a4fdb5a5.dd1ee8"]]},{"id":"a4fdb5a5.dd1ee8","type":"function","z":"60f6e9da.252998","name":"generator","func":"var ran1 = [];\nvar ran2 = [];\nvar chartdata = context.get(\"chartdata\") || []\nvar i;\n\nfor (i = 0; i < 9; i++) {\n  ran1[i] = parseInt(Math.random() *10);\n  ran2[i] = parseInt(Math.random() *12);\n}\nvar ob = {\"x\":ran1, \"y\":ran2};\n\nchartdata.push(ob);\nmsg.topic = \"Random Number\";\nmsg.payload = [{\n\"series\": [\"A\"],\n\"data\": [chartdata],\n\"labels\": [\"\"]\n}]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":100,"wires":[["ce2ca0f3.24533","e74cb9aa.a5b628"]]},{"id":"ce2ca0f3.24533","type":"debug","z":"60f6e9da.252998","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":40,"wires":[]},{"id":"e74cb9aa.a5b628","type":"ui_chart","z":"60f6e9da.252998","name":"","group":"5e594810.e174b8","order":1,"width":0,"height":0,"label":"Coba","chartType":"line","legend":"true","xformat":"x","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":550,"y":200,"wires":[[]]},{"id":"5e594810.e174b8","type":"ui_group","name":"Chart 1","tab":"c3caac70.41ce4","order":1,"disp":true,"width":"6","collapse":false},{"id":"c3caac70.41ce4","type":"ui_tab","name":"Chart","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Screenshot from 2020-10-27 13-43-19

The problem is in the data array.

each x & y should be an array element:

See image, correct vs incorrect

Modify your code to push the element in the array in the loop:

var chartdata = context.get("chartdata") || []
var i;

for (i = 0; i < 9; i++) {
  x = parseInt(Math.random() *10);
  y = parseInt(Math.random() *12);
  chartdata.push({"x":x, "y":y});
}

msg.topic = "Random Number";
msg.payload = [{
"series": ["A"],
"data": [chartdata],
"labels": [""]
}]
return msg;

Would recommend something sensible for the x-axis.

I want to update it per sec. i mean in one second the chart will show the number in sequence.
1st sec x=2, y=3
2nd sec x=7, y=4

and it will show to the chart sequencely. thx

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