# Problem with line chart

**URL:** <https://discourse.nodered.org/t/problem-with-line-chart/30064>\
**Category:** Dashboard\
**Created:** [14 July 2020 14:17 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064 "2020-07-14T14:17:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orion\_12](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@orion\_12](https://discourse.nodered.org/u/orion_12)\
**Post date:** [14 July 2020 14:17 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/1 "2020-07-14T14:17:01Z")

</div>

Good day!  
I try to visualize data from Cloudant with Line Chart.  
Function code is:

```auto
var time = [];
var temp = [];
var deviceid = [];
var time_float = 0;
var temp_float = 0;

var i = 0;
var dataarray = [[]];

for (i=0; i < msg.payload.length; i++) {
time = msg.payload[i].data.d.timestamp;
temp = msg.payload[i].data.d.value3;
time_float = Date.parse(time);
temp_float = temp;
dataarray[0][i] = {"x":time_float,"y":temp_float};
}
var chart = [{
    "series":["Temperature ="],
    "data":dataarray,
    "labels":[""]
}];
msg.payload = chart;
return msg;

```

Into UI my chart's dots connected chaotically  
 ![chart](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/3258ac19e53e97f4493956f1da43e42aa12412c5.jpeg)

Where is my mistake?

Thanks a lot!

---

<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:** [14 July 2020 16:21 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/3 "2020-07-14T16:21:24Z")

</div>

The points will be plotted in the order that they are in the array. The chart looks as if the samples are not in timestamp order, so the line flicks up and down. So for example the first sample might be 13:13, then the next at 14:00, then the next at 13:30 and so on. I don't know about Cloudant, but perhaps you have to ask for the data to be sorted by the timestamp column when you fetch it.

---

<div class="post-metadata">

**Author:** ![orion\_12](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@orion\_12](https://discourse.nodered.org/u/orion_12)\
**Post date:** [15 July 2020 06:29 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/4 "2020-07-15T06:29:38Z")

</div>

Thanks a lot! I understand this.  
How can I sort array of objects? I cant find information about this.  
Field "X" is my timestamp, field "Y" - data for visualize.

 ![array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f65a84380bb40190e57836554cbc79d4ae3770a3.jpeg)

---

<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:** [15 July 2020 07:59 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/5 "2020-07-15T07:59:44Z")

</div>

It would be much more efficient to sort it when you fetch it from Cloudant rather than sorting it in node-red. A quick google search gave many hits including [https://stackoverflow.com/questions/40624834/how-to-sort-date-in-cloudant-query](https://stackoverflow.com/questions/40624834/how-to-sort-date-in-cloudant-query)

If you really need to sort it in node red then you can do it in javascript using the javascript sort() function. This link has an example of how to do it in the section Sorting Object Arrays. [https://www.w3schools.com/js/js\_array\_sort.asp](https://www.w3schools.com/js/js_array_sort.asp)

---

<div class="post-metadata">

**Author:** ![orion\_12](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@orion\_12](https://discourse.nodered.org/u/orion_12)\
**Post date:** [15 July 2020 11:02 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/6 "2020-07-15T11:02:14Z")

</div>

Thanks a lot!

---

<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:** [29 July 2020 11:02 UTC](https://discourse.nodered.org/t/problem-with-line-chart/30064/7 "2020-07-29T11:02:14Z")

</div>

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