# Data from influxdb to a multiline chart

**URL:** https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816
**Category:** General
**Tags:** database, node-red-dashboard, chart
**Created:** [27 October 2021 09:34 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816 "2021-10-27T09:34:04Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [27 October 2021 09:34 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/1 "2021-10-27T09:34:04Z")

</div>

I am trying to get data from influxdb to a multiline chart. I found many examples and tried them. But I don't seem to get it just right. My problem is with the (curly) brackets. Hope anyone can help me solve the problem.

```auto
> // Format the InfluxDB results to match the charts JSON format
> var series = ["A", "B"];
> var labels = [""];
> var data = "[[";
> var thetime, selfuse, totalsolar;
>  
> for (var i=0; i < msg.payload.length; i++) {
> thetime = Number(msg.payload[i].time); // Some manipulation of the time may be required
> selfuse = msg.payload[i].selfuse;
> totalsolar = msg.payload[i].totalsolar;
> data += ' {"x":' + thetime + ', "y":' + selfuse + '},';      
> data += ' {"x":' + thetime + ', "y":' + totalsolar +'} ';
> if (i < (msg.payload.length - 1)) {
> data += ","
> } else {
> data += "]]"
> }
> }
> 
> var jsondata = JSON.parse(data);
> msg.payload = [{"series": series, "data": jsondata, "labels": labels}];
> 
> return msg;

```

---

<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: [27 October 2021 10:11 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/2 "2021-10-27T10:11:33Z")

</div>

Don't build the data as a string and then convert it to an object, build it as a javascript object, it is much simpler.

---

<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: [27 October 2021 11:18 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/3 "2021-10-27T11:18:45Z")

</div>

To clarify a bit, start with declaring data to be an empty array  
`let data = []`  
(nowadays you should be using let not var)  
Then in the loop create push each item into the array, something like  
`data.push({x: thetime, y: selfuse})`  
then at the end, in the payload use `data: [data]`  
Another problem I now see with the code is that you are trying to create two series, so, I think, but I haven't checked the docs, you will need to create arrays dataA and dataB and then in the payload returned use `data:[dataA,dataB]`

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [27 October 2021 12:08 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/4 "2021-10-27T12:08:12Z")

</div>

Colin, thanks for your reaction, Indeed I am trying for a multi line chart. I have tried to convert this (working) example for a single line chart, but got stuck. Now it is only 2 line chart, but I want even more lines.

---

<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: [27 October 2021 12:33 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/5 "2021-10-27T12:33:00Z")

</div>

Does not the method I suggested work? Your original method, even if it works, is not a good way to do it.

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [27 October 2021 12:45 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/6 "2021-10-27T12:45:05Z")

</div>

Yes, I think You are right. I will try your suggestion. I will let you know the result. 🙂

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [27 October 2021 13:15 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/7 "2021-10-27T13:15:44Z")

</div>

> [@Colin](#):
>
> data.push({x: thetime, y: selfuse})

Colin, it works like a charme. You were completly right!! Thanks very much. 🙂  
This is the working function node now.

```auto
// Format the InfluxDB results to match the charts JSON format
let series = ["Eigen gebruik","Totaal Opgewekt"];
let labels = ["Watt"];
let data0 = [];
let data1 = [];

for (var i=0; i < msg.payload.length; i++) {
    let thetime = Number(msg.payload[i].time);
    let selfuse = msg.payload[i].selfuse;
    let totalsolar = msg.payload[i].totalsolar;

    data0.push({x: thetime, y: selfuse});     
    data1.push({x: thetime, y: totalsolar});     
}

let data = [data0,data1];

// node.warn(data);

msg.payload = [{"series": series, "data": data, "labels": labels}];

return msg;

```

---

<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: [27 October 2021 14:45 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/8 "2021-10-27T14:45:56Z")

</div>

Excellent. Glad to be of help.

---

<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: [10 November 2021 14:46 UTC](https://discourse.nodered.org/t/data-from-influxdb-to-a-multiline-chart/52816/9 "2021-11-10T14:46:09Z")

</div>

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