EasyChart: Working with Datasets (Python)

#1

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?


0 Likes

#2

Use the system.dataset.addRow() function.

Best,

0 Likes

#3

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?

0 Likes

#4

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.

0 Likes

#5

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.

0 Likes

#6

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 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.

0 Likes

#7

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

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,

0 Likes

#8

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

0 Likes

#9

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,

0 Likes

#10

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

0 Likes

#11

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)

0 Likes