# Bar Chart programming help

**URL:** <https://discourse.nodered.org/t/bar-chart-programming-help/4259>\
**Category:** Dashboard\
**Created:** [24 October 2018 13:30 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259 "2018-10-24T13:30:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arneym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arneym/32/54_2.png) [@arneym](https://discourse.nodered.org/u/arneym)\
**Post date:** [24 October 2018 13:30 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259/1 "2018-10-24T13:30:42Z")

</div>

I am struggling to get the formatting correct for my bar chart. Sometimes, I do not have data for all 7 machines. I am having problems figuring out my loop in the function node. Here is what I have (no loop).

```auto
m.labels = ["Available Time",
            msg.payload[0].Machine,
            msg.payload[1].Machine,
            msg.payload[2].Machine,
            msg.payload[3].Machine,
            msg.payload[4].Machine,
            msg.payload[5].Machine,
            msg.payload[6].Machine];
m.data = [[480,
           (msg.payload[0].TotalTime/60).toFixed(2),
           (msg.payload[1].TotalTime/60).toFixed(2),
           (msg.payload[2].TotalTime/60).toFixed(2),
           (msg.payload[3].TotalTime/60).toFixed(2),
           (msg.payload[4].TotalTime/60).toFixed(2),
           (msg.payload[5].TotalTime/60).toFixed(2),
           (msg.payload[6].TotalTime/60).toFixed(2)]];
m.series = ["Total Time in 8 hours by Machine"];
return {payload:[m],topic:msg.topic};

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4a09eced898be64442bb4c2f30ebc78453d53fa7.png)

This only works if all 7 of these have some data from the last 8 hours. Sometimes, there might only be 1 machine running.

How can I get this to loop properly?

Thanks,  
Mike

---

<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:** [24 October 2018 14:01 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259/2 "2018-10-24T14:01:41Z")

</div>

You can use

```auto
var arrayLength = msg.payload.length;
for (var i = 0; i < arrayLength; i++) {
   ...
}

```

---

<div class="post-metadata">

**Author:** ![arneym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arneym/32/54_2.png) [@arneym](https://discourse.nodered.org/u/arneym)\
**Post date:** [24 October 2018 14:10 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259/3 "2018-10-24T14:10:32Z")

</div>

Thank you @Colin,

I should have posted my attempt of my `for` loop.  
I had used a similar line in my attempt.

```auto
 for(var i = 0; i < msg.payload.length; i++) {
    ...
}

```

I think it is my placement, but I haven't had any luck. Here is my attempt with your suggestion, but I only get 1 machine.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bfbf8318eaa14bf3e0d175d6cb3e7527fd10470b.png)

```auto
var arrayLength = msg.payload.length;
for (var i = 0; i < arrayLength; i++) {
var m = {};
m.labels = ["Available Time",
            msg.payload[i].Machine];
m.data = [[480,
           (msg.payload[i].TotalTime/60).toFixed(2)]];
m.series = ["Total Time in 8 hours by Machine"];
return {payload:[m],topic:msg.topic};
}

```

---

<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:** [24 October 2018 14:41 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259/4 "2018-10-24T14:41:13Z")

</div>

That is because you are returning at the end of the first pass so it doesn't get to do the rest. You need to build the arrays up. So declare the arrays just with the first (fixed) element

```auto
m.labels = ["Available Time"];
m.data = [[480]];

```

Then have your loop and use the `Array.push()` to add new items onto the array. So you will have something like

```auto
m.labels.push(msg.payload[i].Machine));
m.data[0].push((msg.payload[i].TotalTime/60).toFixed(2));

```

---

<div class="post-metadata">

**Author:** ![arneym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arneym/32/54_2.png) [@arneym](https://discourse.nodered.org/u/arneym)\
**Post date:** [24 October 2018 15:03 UTC](https://discourse.nodered.org/t/bar-chart-programming-help/4259/5 "2018-10-24T15:03:05Z")

</div>

Thank you @Colin!

```auto
var m = {};
m.labels = ["Available Time"];
m.data = [[480]];
m.series = ["Total Time in 8 hours by Machine"];
var arrayLength = msg.payload.length;
for (var i = 0; i < arrayLength; i++) {
    m.labels.push((msg.payload[i].Machine));
    m.data[0].push((msg.payload[i].TotalTime/60).toFixed(2));
}
return {payload:[m],topic:msg.topic};

```
