Hi all,
I've put together a script to update a Power Table with some data from an Easy Chart so that it shows a nicer legend than the built in one.
It seems to work OK, but I would like some feedback on how I might make the code clearer or more efficient or if there are any best practices (say for fault handling and robustness) that I should be following. I'm a new to scripting in Python so I appreciate any guidance!
The script is as follows and sits on the Property Change function of the Easy Chart. It uses some data from custom properties on the PowerTable (xTraceValues) which are written by the getXTraceLabel extension function but otherwise it is pretty straight forward.
# Whenever the startDate property on the chart changes update the data on the power chart
# including tooltip/description, current values, xTrace values and engineering units.
if event.propertyName == "startDate":
# Get all the data from the chart pens dataset
pensDataSet = event.source.parent.getComponent('Easy Chart').tagPens
powerTableData = event.source.parent.getComponent('Power Table').data
toolTipPaths = []
valuePaths = []
engUnitPaths = []
qualifiedToolTips = []
qualifiedValues = []
qualifiedEngUnits = []
toolTips = []
values = []
engUnits = []
xTraceValues = []
# Scan through the rows of pens from the chart
# and put the path for the tooltips, values etc. into seperate datasets
for row in range(pensDataSet.getRowCount()):
toolTipPaths.append(pensDataSet.getValueAt(row, 1)+".Tooltip")
valuePaths.append(pensDataSet.getValueAt(row, 1))
engUnitPaths.append(pensDataSet.getValueAt(row, 1)+".EngUnit")
# Fetch the qualified values from the tagPaths
qualifiedToolTips = system.tag.readBlocking(toolTipPaths)
qualifiedValues = system.tag.readBlocking(valuePaths)
qualifiedEngUnits = system.tag.readBlocking(engUnitPaths)
# Get only the value we are interested in
for row in range(len(qualifiedToolTips)):
toolTips.append(qualifiedToolTips[row].value)
values.append(qualifiedValues[row].value)
engUnits.append(qualifiedEngUnits[row].value)
xTraceValues.append(event.source.parent.getComponent('Power Table').xTraceValues.getValueAt(row,1))
# Combine the pens data set with the new columns
pensDataSet = system.dataset.addColumn(pensDataSet, 1, toolTips, "DESCRIPTION", str)
pensDataSet = system.dataset.addColumn(pensDataSet, 2, values, "VALUE", float)
pensDataSet = system.dataset.addColumn(pensDataSet, 3, xTraceValues, "XTRACE", float)
pensDataSet = system.dataset.addColumn(pensDataSet, 4, engUnits, "UNIT", str)
# Add the new columns to the power table
event.source.parent.getComponent('Power Table').data = pensDataSet