# Single line chart displaying MySql-data

**URL:** <https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620>\
**Category:** General\
**Created:** [23 July 2019 19:24 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620 "2019-07-23T19:24:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [23 July 2019 19:24 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/1 "2019-07-23T19:24:34Z")

</div>

I have this output from a MySQL query:

```auto
[{"time":"2019-07-23T18:13:24.000Z","voltage":3.26},{"time":"2019-07-23T18:13:04.000Z","voltage":3.26},{"time":"2019-07-23T18:12:44.000Z","voltage":3.26},{"time":"2019-07-23T18:12:04.000Z","voltage":3.26},{"time":"2019-07-23T18:11:44.000Z","voltage":3.26},{"time":"2019-07-23T18:11:23.000Z","voltage":3.26},{"time":"2019-07-23T18:11:03.000Z","voltage":3.26},{"time":"2019-07-23T18:10:43.000Z","voltage":3.26},{"time":"2019-07-23T18:10:22.000Z","voltage":3.26},{"time":"2019-07-23T18:10:02.000Z","voltage":3.26}]

```

I try to display this with the dashboard ui\_chart node, but get "bad data inject" from debug window.  
What am I missing here?

Example flow (array put into inject node):

```auto
[{"id":"cce7a5b3.8ffaf8","type":"tab","label":"chart test","disabled":false,"info":""},{"id":"11e50b1e.086525","type":"inject","z":"cce7a5b3.8ffaf8","name":"inject array","topic":"","payload":"[{\"time\":\"2019-07-23T18:13:24.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:13:04.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:12:44.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:12:04.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:11:44.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:11:23.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:11:03.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:10:43.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:10:22.000Z\",\"voltage\":3.26},{\"time\":\"2019-07-23T18:10:02.000Z\",\"voltage\":3.26}]","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":214,"y":155,"wires":[["d6be3d5e.2fe3d","fbefbe0.4cf9d4"]]},{"id":"d6be3d5e.2fe3d","type":"ui_chart","z":"cce7a5b3.8ffaf8","name":"line chart","group":"f02a6ce.5981f9","order":1,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":426,"y":158,"wires":[["f88aaee7.8455"]]},{"id":"fbefbe0.4cf9d4","type":"debug","z":"cce7a5b3.8ffaf8","name":"complete msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":447,"y":121,"wires":[]},{"id":"f88aaee7.8455","type":"debug","z":"cce7a5b3.8ffaf8","name":"complete msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":674,"y":119,"wires":[]},{"id":"f02a6ce.5981f9","type":"ui_group","z":"","name":"TEST","tab":"96eb0793.053038","order":2,"disp":true,"width":"12","collapse":false},{"id":"96eb0793.053038","type":"ui_tab","z":"b317d85f.b86b38","name":"Home2","icon":"dashboard"}]

```

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [24 July 2019 20:33 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/2 "2019-07-24T20:33:07Z")

</div>

I have manually edited the data to fit the requirements for chart\_ui.

```auto
{"_msgid":"d06e93.606b617","topic":"","payload":[{"series":["y"],"data":[[{"x":1563998854561,"y":3.26},{"x":1563998855571,"y":3.26},{"x":1563998856581,"y":3.26},{"x":1563998857591,"y":3.26},{"x":1563998858601,"y":3.26},{"x":1563998859611,"y":3.26},{"x":1563998856021,"y":3.26},{"x":1563998856131,"y":3.26},{"x":1563998856241,"y":3.26},{"x":1563998856351,"y":3.26}]],"labels":["y"]}]}

```

So now it shows up as a chart as expected. Now I just have to figure out how to get there via a function (or other) node...

Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 July 2019 21:21 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/3 "2019-07-24T21:21:50Z")

</div>

Your second flow in not importing  
what does the data coming from the database look like?

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [25 July 2019 11:38 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/4 "2019-07-25T11:38:03Z")

</div>

Thanks for your response!  
The code in the second post is not a flow, but a modified copy of the payload coming from the MySQL node, meant to be placed into an injection node, feeding a chart node and debug node(s) for testing.

Waiting for response I tried a few more times to get the payload formatted as needed, and now I have this function:

```auto
var i;
var x = [];
var y = [];
var pl_old = msg.payload;
var pl_new = [];

for (i = 0; i<pl_old.length; i++) {
    // get data info x and y, and convert to unix time format:
    x[i] = new Date(pl_old[i].time).getTime();
    y[i] = pl_old[i].voltage;
    
    // generate key-value pair:
    pl_new[i] = {"x":x[i], "y":y[i]};
}

// Redefine msg.payload and return:
msg.payload = [{"series":["voltage"], "labels":["voltage data"], "data":[pl_new]}];

return msg;

```

which seems to work very well !

It may contain errors, be more clever written, etc, but i am a newbie so please give feedback if there are better ways to do this so I can learn from it.

Regards,

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [25 July 2019 12:12 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/5 "2019-07-25T12:12:29Z")

</div>

> [@solano8](#):
>
> The code in the second post is not a flow, but a modified copy of the payload coming from the MySQL node, meant to be placed into an injection node, feeding a chart node and debug node(s) for testing.

But it is not importable. It looks like you missed something in the copy/paste of the node.

As for the function, there are many ways to solve problems. If this is working then good for you 🌟

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [26 July 2019 14:31 UTC](https://discourse.nodered.org/t/single-line-chart-displaying-mysql-data/13620/6 "2019-07-26T14:31:45Z")

</div>

> [@zenofmud](#):
>
> But it is not importable. It looks like you missed something in the copy/paste of the node.

Hi, again!

The code supplied was not meant for import as such, but if I start with a new flow, add the inject node and paste the given code into the payload file as json, it can then be injected into other nodes. In my case chart and debug nodes for testing. But of course I could (and perhaps should) have given you the complete node or maybe flow for import, not only the json, to make it easier and more safe to submit the case. But hopefully a lesson learned, and thank you for your time with my challenge!.
