# Dynamic bar chart

**URL:** <https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143>\
**Category:** Ignition\
**Created:** [January 12, 2021, 1:54am UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143 "2021-01-12T01:54:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![felipe2010silva](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@felipe2010silva](https://forum.inductiveautomation.com/u/felipe2010silva)\
**Post date:** [January 12, 2021, 1:54am UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/1 "2021-01-12T01:54:14Z")

</div>

Good evening. My name is Felipe. I received today from the manager of the company that I work on the task of creating a dynamic bar graph (or something similar to a bar graph), whose goal is to increment a new bar hourly and delete the last bar from the graph. in the image below I used the cylinder tank components to exemplify what I mean. I used the MES Analysis Controller component to generate hourly production information and record the hourly efficiency value in each tank based on the following equation:

`({Root Container.RCEfficiency.ACDeco22.data}[6,1]/(60*2000))*100`

In the example below, the MES Analysis Controller component generated values ​​until 12:00 and the idea would be that as soon as the hourly production information from 1 pm appears, the 7:00 information from the graph disappears, as well as the time at 2 pm appears. 8 am bar disappeared and so on. I exhausted my attempts and the forum has helped me a lot and I appreciate the help and each one. thank you so much.

I really don’t know if it’s possible, but I appreciate the help of each one

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/b/db5655461b2429eed3ee6821832c6a7e5b05b7c7.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/4/84da2019f8b61c6015b9eeee27a9989d481f967a.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 12, 2021, 1:42pm UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/2 "2021-01-12T13:42:44Z")

</div>

I suggest to make your bar chart a category chart and use the times as the individual categories.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 12, 2021, 1:44pm UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/3 "2021-01-12T13:44:58Z")

</div>

I may have to backtrack a bit. Does the chart itself work, and you need help in populating the dataset?

---

<div class="post-metadata">

**Author:** ![felipe2010silva](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@felipe2010silva](https://forum.inductiveautomation.com/u/felipe2010silva)\
**Post date:** [January 12, 2021, 2:10pm UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/4 "2021-01-12T14:10:00Z")

</div>

to fill the data set I can do it, because the startDate I stayed at 7:00 in the morning and the endDate I put `now(600)`, so that it would provide me with the values ​​in real time since the start of the shift. I set Group By “Hour interval” to provide me with hourly information. The difficulty is that I wanted that when the difference between startDate and endDate was greater than 10 hours, for example 7:00 am and 5:00 pm, when it was 6:00 pm the 7:00 am time of the bar graph would disappear, giving way to bar at 6 pm. when it was 7 at night the 8:00 am time would disappear and so on. I don’t know if I managed to be clear

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 12, 2021, 8:56pm UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/5 "2021-01-12T20:56:32Z")

</div>

This function will return the last n rows in a dataset.

```python
def lastNRows(datasetIn, numRows):
	# Check to see if processing is required
	if datasetIn.getRowCount() <= numRows:
		return datasetIn
	else:
		# Convert to pyDataSet
		pyDataSet = system.dataset.toPyDataSet(datasetIn)
		return system.dataset.toDataSet(pyDataSet[-numRows:])

```

Sample:

```python
# Make sample dataset
headers = ['letter', 'number']
data = [['A', 1],
        ['B', 2],
        ['C', 3],
        ['D', 4],
        ['E', 5],
        ['F', 6],
        ['G', 7],
        ['H', 8],
        ['I', 9],
        ['J', 10],
        ['K', 11],
        ['L', 12],
        ['M', 13],
        ['N', 14],
        ['O', 15],
        ['P', 16]
       ]       
dataSet = system.dataset.toDataSet(headers, data)

# Get last 5 rows
newDataSet = lastNRows(dataSet, 5)

```

dataSet:

```nohighlight
row | letter number
-------------------
 0 | A 1     
 1 | B 2     
 2 | C 3     
 3 | D 4     
 4 | E 5     
 5 | F 6     
 6 | G 7     
 7 | H 8     
 8 | I 9     
 9 | J 10    
 10 | K 11    
 11 | L 12    
 12 | M 13    
 13 | N 14    
 14 | O 15    
 15 | P 16    

```

newDataSet:

```nohighlight
row | letter number
-------------------
 0 | L 12    
 1 | M 13    
 2 | N 14    
 3 | O 15    
 4 | P 16    

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 12, 2021, 9:00pm UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/6 "2021-01-12T21:00:06Z")

</div>

Sample when there are less rows than the threshold:

```python
headers = ['letter', 'number']
data = [['A', 1],
        ['B', 2],
        ['C', 3]
       ]       
dataSet = system.dataset.toDataSet(headers, data)

# Get last 5 rows
newDataSet = lastNRows(dataSet, 5)

```

dataSet:

```nohighlight
row | letter number
-------------------
 0 | A 1     
 1 | B 2     
 2 | C 3   

```

newDataSet:

```nohighlight
row | letter number
-------------------
 0 | A 1     
 1 | B 2     
 2 | C 3   

```

---

<div class="post-metadata">

**Author:** ![felipe2010silva](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@felipe2010silva](https://forum.inductiveautomation.com/u/felipe2010silva)\
**Post date:** [January 13, 2021, 10:32am UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/7 "2021-01-13T10:32:20Z")

</div>

what a spectacular job @JordanCClark. Thank you very much for all your help.

---

<div class="post-metadata">

**Author:** ![felipe2010silva](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@felipe2010silva](https://forum.inductiveautomation.com/u/felipe2010silva)\
**Post date:** [January 13, 2021, 10:33am UTC](https://forum.inductiveautomation.com/t/dynamic-bar-chart/42143/8 "2021-01-13T10:33:15Z")

</div>

It Worked Perfectly
