# EasyChart: Working with Datasets (Python)

**URL:** <https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668>\
**Category:** Ignition\
**Created:** [February 5, 2015, 11:57pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668 "2015-02-05T23:57:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisGray](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@ChrisGray](https://forum.inductiveautomation.com/u/ChrisGray)\
**Post date:** [February 5, 2015, 11:57pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/1 "2015-02-05T23:57:25Z")

</div>

Objective: To add Tag Pens to Easy Chart through scripting.

EasyChart seems to use DataSets to store the Tag Pens that are displayed on the chart. The DataSet includes a number of columns that store the configuration of the Tag being charted (i.e. Name, Tag\_Path, Color…).

Question: How do you add a new row through event scripts to this DataSet to add a new Tag Pen to the EasyChart?

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/0/0731d820fe59a2c7df7e473c96dc027645247643.gif)

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 6, 2015, 12:24am UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/2 "2015-02-06T00:24:36Z")

</div>

Use the [system.dataset.addRow()](https://support.inductiveautomation.com/usermanuals/ignition/index.html?system_dataset_addrow.htm) function.

Best,

---

<div class="post-metadata">

**Author:** ![ChrisGray](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@ChrisGray](https://forum.inductiveautomation.com/u/ChrisGray)\
**Post date:** [February 6, 2015, 12:55am UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/3 "2015-02-06T00:55:54Z")

</div>

Thanks for the response.

I have used this method, but it increasingly got more complicated as I tried to implement. Building a well formed and type specific row to pass into the dataset does not seem easy (unless you hardcode which is painful and not automated), so I thought there must be a better way. Is there an easy method for creating the row of data (with correct type specific data) that I am missing?

BTW: I did try extracting an existing row from the dataset one element at a time (using the getValueAt()) to see if I could automatically reconstruct a valid row to pass into the dataset,but I ran into issues with coercion (datatyping) as I tried to create the row as a string and feed this back into the dataset.

Advice?

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 6, 2015, 2:23am UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/4 "2015-02-06T02:23:17Z")

</div>

Maybe you should paste in the code you have tried to use. It is easy for me to construct rows of data for a dataset.

Maybe you can show me how it can be complicated and difficult and then I can show you how to make it easy. I don’t know where or how you are getting your data that you need to add to the dataset so I can’t show you how to prepare the data and add the row.

---

<div class="post-metadata">

**Author:** ![ChrisGray](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@ChrisGray](https://forum.inductiveautomation.com/u/ChrisGray)\
**Post date:** [February 6, 2015, 5:41am UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/5 "2015-02-06T05:41:47Z")

</div>

OK. It’s not pretty, but here is what I have tried so far. This was my last attempt at building the row string correctly.

#Display Event properties for testing purposes  
print "Button code: " , event.button, " Click Count: ", event.clickCount

#Work with DataSets to get Easy Chart Tag Pens  
tagPenDataSet = event.source.parent.getComponent(“Easy Chart”).tagPens  
print "Row Count = “, tagPenDataSet.rowCount, " Column Count = “, tagPenDataSet.columnCount,”\n”,  
“Original Data Set : \n”,event.source.displayRows(tagPenDataSet) , “\n”

#Build Easy Chart Dataset Row from scratch in Dictionary format (there is no Row Copy method for Datasets)  
easyChartRow = “”  
headerTypes = [“str”,“str”,“str”,“str”,“I”,“B”,“clr”,“str”,“I”,“F”,“I”,“B”,“str”,“B”,“B”,“B”,“B”,“I”,“B”]  
for column in range(tagPenDataSet.columnCount):  
if column == 0:  
easyChartRow = “{’” + tagPenDataSet.getColumnName(column) + "’ : "  
if headerTypes[column] == “str” or headerTypes[column] == “clr” :  
easyChartRow = easyChartRow + “’” + str(tagPenDataSet.getValueAt(0, column)) + “’”  
print column, " " ,easyChartRow  
else:  
easyChartRow = easyChartRow + str(tagPenDataSet.getValueAt(0, column))  
print column, " " ,easyChartRow   
else:  
if headerTypes[column] == “str” or headerTypes[column] == “clr”:  
easyChartRow = easyChartRow + “,’” + tagPenDataSet.getColumnName(column) + "’ : " + “’” + str(tagPenDataSet.getValueAt(0, column)) + “’”  
print column, " " ,easyChartRow  
else:  
easyChartRow = easyChartRow + “,’” + tagPenDataSet.getColumnName(column) + "’ : " + str(tagPenDataSet.getValueAt(0, column))  
print column, " " ,easyChartRow   
easyChartRow = easyChartRow + “}”

print easyChartRow,"\n\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\n\n"

tagPenDataSet = system.dataset.addRow(tagPenDataSet,easyChartRow)

event.source.parent.getComponent(“Easy Chart”).tagPens = tagPenDataSet

Let me know if you have any tricks that can help? I apologies for the poor coding in advance. I am a beginner at best. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 6, 2015, 3:10pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/6 "2015-02-06T15:10:45Z")

</div>

Hi Chris,  
I see that you are attempting to create the new row as a string that looks like a dictionary. The addRow() function needs a python list, in the same order as the existing columns. The following routine will duplicate a specified row and add it to the dataset, which you can then assign to the target property:

`def duplicateRow(dataset, rownum):
  newRow = [dataset.getValueAt(rownum, colnum) for colnum in range(dataset.columnCount)]
  return system.dataset.addRow(dataset, newRow)`  
Note the use of [] to make a python list, not a string. This form is called a [List Comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) in the python documentation.  
Generally, you’ll want to open-code this kind of manipulation, so you can customize the values in newRow before you add it to the dataset.

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 6, 2015, 4:07pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/7 "2015-02-06T16:07:51Z")

</div>

Okay, you want to copy a row from a dataset and then add that row to the dataset. Here’s my implementation:

```auto
chart = event.source.parent.getComponent("Easy Chart")
tagPenDataSet = system.dataset.toPyDataSet(chart.tagPens)
chart.tagPens = system.dataset.addRow(chart.tagPens,list(tagPenDataSet[0]))
```

Here’s what happens:

1. We get the chart.

2. We convert the Dataset into a PyDataSet. This is done so we can easily access individual rows of the Dataset with a row index like tagPenDataSet[0], tagPenDataSet[1] etc.

3. We convert the first row into a Python list with the “list” function and use the system.dataset.addRow function to add the row to the dataset.

Best,

---

<div class="post-metadata">

**Author:** ![ChrisGray](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@ChrisGray](https://forum.inductiveautomation.com/u/ChrisGray)\
**Post date:** [February 6, 2015, 11:57pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/8 "2015-02-06T23:57:08Z")

</div>

Thanks everyone, I will give this a shot. Much appreciated.

---

<div class="post-metadata">

**Author:** ![lrhyrne](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrhyrne/32/12097_2.png) [@lrhyrne](https://forum.inductiveautomation.com/u/lrhyrne)\
**Post date:** [September 20, 2019, 1:24pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/9 "2019-09-20T13:24:08Z")

</div>

This is the closest topic and answer thread I have found to what I’m looking for. I’m trying to access the start and end dates on the Easy Chart from python. I have not found any reference to support this programmatic interface.

Thanks for any help,

---

<div class="post-metadata">

**Author:** ![lrhyrne](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrhyrne/32/12097_2.png) [@lrhyrne](https://forum.inductiveautomation.com/u/lrhyrne)\
**Post date:** [September 20, 2019, 2:21pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/10 "2019-09-20T14:21:19Z")

</div>

Is there a way to get the start and end timestamps for the chart, in python?

---

<div class="post-metadata">

**Author:** ![lrhyrne](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrhyrne/32/12097_2.png) [@lrhyrne](https://forum.inductiveautomation.com/u/lrhyrne)\
**Post date:** [September 20, 2019, 2:58pm UTC](https://forum.inductiveautomation.com/t/easychart-working-with-datasets-python/9668/11 "2019-09-20T14:58:01Z")

</div>

working code below (Ignition 7.9.8):

code triggered from button in the same Window as EasyChart

eChart = event.source.parent.getComponent(“Easy Chart”)

start\_ts = eChart.getZoomedStartDate()  
end\_ts = eChart.getZoomedEndDate()

strMessage = "Start TS: " + str(start\_ts) + “\n” + "End TS: " + str(end\_ts)

system.gui.messageBox(strMessage)
