Multiple lines in graph from JSONata

I try to get historical data in a graph. I have 3 values, huidig (current), max and min. All are temperatures.
I get the data from a MariaDB and feed it through a JSONata node. I have the following script in a change node:

(
  $series := [
    { "field1": "d_waarde", "field2": "d_max", "field3": "d_min", "label": "TuinTemp" }
  ];
  $xaxis := "UNIX_TIMESTAMP(d_ts)";
  [
    {
      "series": ["Huidig","Max","Min"],
      "labels": [$series.label],
      "data": $series.[[
        (
          $yaxis := $.field1;
          $$.payload.{
            "x": $lookup($, $xaxis)*1000,
            "y": $number($lookup($, $yaxis))
          }
        )
      ][
        (
          $yaxis := $.field2;
          $$.payload.{
            "x": $lookup($, $xaxis)*1000,
            "y": $number($lookup($, $yaxis))
          }
        )
      ][
        (
          $yaxis := $.field3;
          $$.payload.{
            "x": $lookup($, $xaxis)*1000,
            "y": $number($lookup($, $yaxis))
          }
        )
      ]]
    }
  ]
)

the resulting array does only have 1 x and 1 y value and displays only 1 line:

[{"series":["Huidig","Max","Min"],"labels":["TuinTemp"],"data":[[{"x":1667388506000,"y":14.4},{"x":1667388549000,"y":14.4},{"x":1667388592000,"y":14.4},{"x":1667388635000,"y":14.4},{"x":1667388678000,"y":14.5},{"x":1667388721000,"y":14.5},{"x":1667388764000,"y":14.5},{"x":1667388807000,"y":14.4},{"x":1667388850000,"y":14.5},{"x":1667388893000,"y":14.4},{"x":1667388936000,"y":14.5},{"x":1667388979000,"y":14.5},{"x":1667389065000,"y":14.5}]]}]

I think I'm doing something wrong with the square bracket's. Do I need more?

You are missing 2 commas between the 3 arrays.

where do I need to place them? I started with this and that works:

(
  $series := [
    { "field": "rsd_neerslag", "label": "mm" }
  ];
  $xaxis := "UNIX_TIMESTAMP(rsd_ts)";
  [
    {
      "series": [$series.label],
      "labels": [$series.label],
      "data": $series.[[
        (
          $yaxis := $.field;
          $$.payload.{
            "x": $lookup($, $xaxis)*1000,
            "y": $number($substringAfter($lookup($, $yaxis)," "))
          }
        )
      ]]
    }
  ]
)

As said between the 3 arrays, lines 18 and 26 ],[

ok, thanks. But worked only once. Now the graph does not appear anymore. I'm puzzled.
The array that is presented to the graph contains 1 object that contains series array[3], labels array[1] and data array[3] which contains 0 array[80], 1 array[80] and 2 array[80].
Each of these arrays contain object containing an x and an y both with a value.
And the starnge thing is that the first time I tried it I got an nice graph.

Editing a post that said it work is not a good idea, I receive no notification unless you reply to me.

As to issue I can not say a lot as you give no relevant info, what the input is, what the output is. I know the code is fine so it would most likely be the data not being correct.

More dynamic version, add $lines object for each line.

(
$lines := [
    {
        "field_y": "d_waarde",
        "field_x": "UNIX_TIMESTAMP(rsd_ts)",
        "label": "TuinTemp",
        "series": "Huidig"
    },
    {
        "field_y": "d_max",
        "field_x": "UNIX_TIMESTAMP(rsd_ts)",
        "label": "TuinTemp",
        "series": "Max"
    },
    {
        "field_y": "d_min",
        "field_x": "UNIX_TIMESTAMP(rsd_ts)",
        "label": "TuinTemp",
        "series": "Min"
    }
];
[
    {
        "series": [$lines.series],
        "labels": [$lines.label],
        "data": [$lines.[(
                $yaxis := $.field_y;
                $xaxis := $.field_x;
                $$.payload.{
                    "x": $lookup($, $xaxis)*1000,
                    "y": $number($lookup($, $yaxis))           
                }        
            )]
        ]
    }   
] 
)

Thanks, I will try the lines proposal. The problem that the graph was not displayed was the browser (Firefox): when I selected a day where I had no data from the graph disappered. Then when I choose a day with data I did not get my graph. When I then pressed CTRL-F5 the graph reappeared. Something to do with caching I presume.
This is what I'm working on:

[{"id":"4f1b7cf7b9b34133","type":"ui_date_picker","z":"3759b07b.f88938","name":"datum_mwm","label":"grafiek op","group":"f2a17a62170c2e69","order":2,"width":0,"height":0,"passthru":false,"topic":"","topicType":"str","className":"","x":90,"y":1880,"wires":[["24d6003533fb19ea","a41cf2f784aed7a7"]]},{"id":"24d6003533fb19ea","type":"function","z":"3759b07b.f88938","name":"make whole day","func":"if ( !msg.timestamp ) msg.timestamp = Math.round(+new Date());\n\nvar dt = new Date(msg.payload);\nvar Y = dt.getFullYear().toString();\nvar M = (dt.getMonth() + 1).toString();\nif((dt.getMonth() + 1)<=9){M='0'+M}\nvar D = dt.getDate().toString();\nif(dt.getDate()<=9){D='0'+D}\nvar H = dt.getHours().toString();\nif(dt.getHours()<=9){H='0'+H}\nvar m = dt.getMinutes().toString();\nif(dt.getMinutes()<=9){m='0'+m}\n//var S = dt.getMilliseconds().toString();\nmsg.payload = Y+'-'+M+'-'+D;\nmsg.datum = Y+'-'+M+'-'+D;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1880,"wires":[["522bacc6f08d0343"]]},{"id":"a41cf2f784aed7a7","type":"debug","z":"3759b07b.f88938","name":"datum","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":260,"y":1820,"wires":[]},{"id":"db5f7aab9d038ab7","type":"inject","z":"3759b07b.f88938","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1667347200000","payloadType":"num","x":110,"y":1960,"wires":[["24d6003533fb19ea","a41cf2f784aed7a7"]]},{"id":"522bacc6f08d0343","type":"template","z":"3759b07b.f88938","name":"format query","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"SELECT UNIX_TIMESTAMP(d_ts), d_waarde, d_max, d_min\nFROM data\nWHERE d_ts >= \"{{payload}}:00:00:00\"\n    AND d_ts <= \"{{payload}}:23:59:59\"\nORDER  BY d_ts ASC;","output":"str","x":470,"y":1880,"wires":[["cd71db604bcef6f2"]]},{"id":"cd71db604bcef6f2","type":"mysql","z":"3759b07b.f88938","mydb":"ed59d6734411cfdd","name":"MariaDB","x":620,"y":1880,"wires":[["9f74654ab95b7932"]]},{"id":"9f74654ab95b7932","type":"change","z":"3759b07b.f88938","name":"Format data WK","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t  $series := [\t    { \"field1\": \"d_waarde\", \"field2\": \"d_max\", \"field3\": \"d_min\", \"label\": \"TuinTemp\" }\t  ];\t  $xaxis := \"UNIX_TIMESTAMP(d_ts)\";\t  [\t    {\t      \"series\": [\"Huidig\",\"Max\",\"Min\"],\t      \"labels\": [$series.label],\t      \"data\": $series.[[\t        (\t          $yaxis := $.field1;\t          $$.payload.{\t            \"x\": $lookup($, $xaxis)*1000,\t            \"y\": $number($lookup($, $yaxis))\t          }\t        )\t      ],[\t        (\t          $yaxis := $.field2;\t          $$.payload.{\t            \"x\": $lookup($, $xaxis)*1000,\t            \"y\": $number($lookup($, $yaxis))\t          }\t        )\t      ],[\t        (\t          $yaxis := $.field3;\t          $$.payload.{\t            \"x\": $lookup($, $xaxis)*1000,\t            \"y\": $number($lookup($, $yaxis))\t          }\t        )\t      ]]\t    }\t  ]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":1880,"wires":[["ceec58959c7461f2","7fed709d69e8509e"]]},{"id":"ceec58959c7461f2","type":"ui_chart","z":"3759b07b.f88938","name":"","group":"f2a17a62170c2e69","order":3,"width":0,"height":0,"label":"TuinTemperatuurHistorie","chartType":"line","legend":"true","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"604800","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1010,"y":1880,"wires":[["bbb23d92844e8f9f"]]},{"id":"7fed709d69e8509e","type":"debug","z":"3759b07b.f88938","name":"data","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":1940,"wires":[]},{"id":"bbb23d92844e8f9f","type":"debug","z":"3759b07b.f88938","name":"message","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":2040,"wires":[]},{"id":"f2a17a62170c2e69","type":"ui_group","name":"MinWaardeMax","tab":"bf4b8d34.813ae8","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ed59d6734411cfdd","type":"MySQLdatabase","name":"","host":"192.168.17.67","port":"3306","db":"huismanager","tz":"","charset":"UTF8"},{"id":"bf4b8d34.813ae8","type":"ui_tab","name":"Huismanager","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

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