Line Chart with 2 lines

Hello! Newbie here, starting to learn Node-Red.
I've been wrestling with this for more than 2 weeks, being on and off, when time allows.
I have a database, in which I make a query, and get results for temperature and humidity.
These came from sensors with MQTT.
Example of my query results:
{"timestamp":"2020-06-08T21:32:59.000Z","data":"22.60","topic":"abrigo/temp"},{"timestamp":"2020-06-08T21:32:59.000Z","data":"63.00","topic":"abrigo/humi"}

I tried several ways to make this data good for a 2 line char, one for "abrigo/temp", one for "abrigo/humi".

I feel I'm close with my new approach but I cannot advance so I am asking for help.

This is my actual setup in a function to get the data prepared:

var msg1 = {};
let dataH =[];
let dataT =[];
msg1.topic = "Temp/Humi";
for (let X in msg.payload) 
{
    if (msg.payload[X].topic == "abrigo/humi")
    {
        dataH.push({"x":msg.payload[X].timestamp, "y":msg.payload[X].data});
    }
    else
    {
        dataT.push({"x":msg.payload[X].timestamp, "y":msg.payload[X].data});
    }
}
msg1.payload = [{"series":["Humidade", "Temperatura"], "data":[[dataH],[dataT]],"labels": ["Humidade","Temperatura"]}];
return (msg1);

The result from this function is this:

{
   "series":[
      "Humidade",
      "Temperatura"
   ],
   "data":[
      [
         [
            {
               "x":"2020-06-08T21:32:59.000Z",
               "y":"63.00"
            },
            {
               "x":"2020-06-08T21:33:59.000Z",
               "y":"56.00"
            },
            {
               "x":"2020-06-08T21:47:08.000Z",
               "y":"49.00"
            },
            {
               "x":"2020-06-08T21:57:08.000Z",
               "y":"53.00"
            },
            {
               "x":"2020-06-08T22:07:08.000Z",
               "y":"53.00"
            }
         ]
      ],
      [
         [
            {
               "x":"2020-06-08T21:32:59.000Z",
               "y":"22.60"
            },
            {
               "x":"2020-06-08T21:33:59.000Z",
               "y":"24.70"
            },
            {
               "x":"2020-06-08T21:47:08.000Z",
               "y":"25.20"
            },
            {
               "x":"2020-06-08T21:57:08.000Z",
               "y":"23.20"
            },
            {
               "x":"2020-06-08T22:07:08.000Z",
               "y":"23.20"
            },
            {
               "x":"2020-06-08T22:17:08.000Z",
               "y":"23.10"
            }
         ]
      ]
   ],
   "labels":[
      "Humidade",
      "Temperatura"
   ]
}

But his does not get me a 2 line graphic.

Can someone help me to understand what is wrong here?

Thank you in advance!

hi and welcome to the forum, I'm certain someone will be able to help you out.

Firstly though, please always wrap code in three back ticks

```
like this
```

otherwise the forum messes up your code and it is both unreadable and actually unusable.

Please edit your 1st post.

I'm sorry for the mistake, and thank you.

no worries - I edited the 2nd code block for you - much more readable for someone to assist right?

Yes, much more! :sweat_smile:
Thank you

If you look at the example in https://github.com/node-red/node-red-dashboard/blob/master/Charts.md you will see you have too many levels of array nesting in data.
dataH and dataT are already arrays, so try

msg1.payload = [{"series":["Humidade", "Temperatura"], "data":[dataH,dataT],"labels": ["Humidade","Temperatura"]}];
1 Like

OMG!!! It was that simple... :crazy_face: :crazy_face: Shame on me!!! :grinning:
I tried so many variants...
Thanks!!!

By the way, it is generally good practice to modify the existing message and pass that on rather than making a new one. You could do that by removing the creation of msg1 and the setting of its topic at the top and replace the last lines with

msg.topic = "Temp/Humi"
msg.payload = [{"series":["Humidade", "Temperatura"], "data":[[dataH],[dataT]],"labels": ["Humidade","Temperatura"]}];
return (msg);

Thank you for sharing. I'm starting and I will take that into account next time, also I will change my actual project with this tip.

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