# Line Chart with 2 lines

**URL:** <https://discourse.nodered.org/t/line-chart-with-2-lines/28234>\
**Category:** Dashboard\
**Created:** [12 June 2020 14:51 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234 "2020-06-12T14:51:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmgp](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hmgp](https://discourse.nodered.org/u/hmgp)\
**Post date:** [12 June 2020 14:51 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/1 "2020-06-12T14:51:52Z")

</div>

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:

```auto
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:

```auto
{
   "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!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 June 2020 14:54 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/2 "2020-06-12T14:54:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hmgp](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hmgp](https://discourse.nodered.org/u/hmgp)\
**Post date:** [12 June 2020 14:56 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/3 "2020-06-12T14:56:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 June 2020 14:58 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/4 "2020-06-12T14:58:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hmgp](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hmgp](https://discourse.nodered.org/u/hmgp)\
**Post date:** [12 June 2020 15:22 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/5 "2020-06-12T15:22:32Z")

</div>

Yes, much more! 😅  
Thank you

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 June 2020 15:24 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/6 "2020-06-12T15:24:11Z")

</div>

If you look at the example in [https://github.com/node-red/node-red-dashboard/blob/master/Charts.md](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

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

```

---

<div class="post-metadata">

**Author:** ![hmgp](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hmgp](https://discourse.nodered.org/u/hmgp)\
**Post date:** [12 June 2020 15:30 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/7 "2020-06-12T15:30:36Z")

</div>

OMG!!! It was that simple... 🤪 🤪 Shame on me!!! 😀  
I tried so many variants...  
Thanks!!!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 June 2020 15:44 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/8 "2020-06-12T15:44:04Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![hmgp](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hmgp](https://discourse.nodered.org/u/hmgp)\
**Post date:** [12 June 2020 18:50 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/9 "2020-06-12T18:50:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 June 2020 18:50 UTC](https://discourse.nodered.org/t/line-chart-with-2-lines/28234/10 "2020-06-26T18:50:48Z")

</div>

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