Dynamically Add Easy Chart Pens

Hello,

I’m trying to write a python script to dynamically add pens to an easy chart I have on a screen. I tried all day and have failed miserably so I thought I’d just post something simple. The script below should append the last row of the tagPens dataset to itsself. I’m getting an error saying that
“data has no attribute ‘append’”. When I did a dir(data) I saw that it was a pyRow object… should this be a normal list object?


easyChart = event.source.parent.getComponent('Easy Chart')
data = system.dataset.toPyDataSet(easyChart.tagPens)

headers = ["NAME","TAG_PATH","AGGREGATION_MODE","AXIS","SUBPLOT","ENABLED","COLOR","DASH_PATTERN","RENDER_STYLE","LINE_WEIGHT","SHAPE","FILL_SHAPE","GROUP_NAME","DIGITAL","OVERRIDE_AUTOCOLOR","HIDDEN"]

for row in data:
	row2 = row

# append last row to data again
data.append(row2)

easyChart.tagPens = system.dataset.toDataSet(headers, data)

Finally, if there is any example of even a hard coded script adding pens to an easy chart would somebody mind sharing that with me? I could probably find some hints there.

Cody I’ve been working the same thing.

Couple of notes. 1 - I use a drop-down list to select what I want to add to the easy chart, I then have to do a SQL query so I can build the tag reference for the tag data as my SQLTags are of a different naming convention than my drop down list. I am also not changing the header, just appending a row into the chart dataset.

My list might contain:

FT-101
FT-102
FT-103

I then have to relate that to the actual historical SQLtag:
Analog_Input/AI 1
Analog_Input/AI 2
Analog_Input/AI 3

# Set Property Reference
Property = event.source.parent

# Get selected tag from dropdown
selectedTag = Property.getComponent('Dropdown').selectedLabel

# Build SQLTag Path for trend data
# Setup SQL Query Parameters 
Database = "TAG_INFO"
WhereExpr = "SELECT PLC, Device FROM tbl_AnalogInput WHERE TagName = '%s'" % selectedTag

# SQL Query Execution
SQLResult = system.db.runQuery(WhereExpr,Database)
NumRows = len(SQLResult)

if NumRows > 0:
	for row in SQLResult:
		tagPath = "[historian]Analog_Input/AI %s/Value" %(int(row[1]))

# Add New Tag to Easy Chart
data = Property.getComponent('Easy Chart').tagPens

# Table Column Data
col1Name = selectedTag
col2Path = tagPath
col3Aggregation = "MinMax"
col4Axis = "Default Axis"
col5Subplot = "1"
col6Enabled = "true"
col7Color = "000000"
col8Dash = ""
col9Render = "1"
col10Line = "1"
col11Shape = "0"
col12Fill = "true"
col13Group = ""
col14Digital = "false"
col15Override = "false"
col16Hidden = "false"

Property.getComponent('Easy Chart').tagPens = system.dataset.addRow(data, [col1Name, col2Path, col3Aggregation, col4Axis, col5Subplot, col6Enabled, col7Color, col8Dash, col9Render, col10Line, col11Shape, col12Fill, col13Group, col14Digital, col15Override, col16Hidden, ])

Most of the attributes are hard-coded “default” values, however I would like to add a color selection during the process. So a user selects the tag from the drop-down, selects the color for the trend, then clicks “add”.

Next part I need to work on is a “delete” function.

Hey Paully,

Thanks that did the trick! I haven’t played with Ignition’s dataset object much so I didn’t know that everything had to be a string I suppose…

Here’s a quick little module (now that I’ve got things working) that you might like Paully. (You’ll want to remove my exception handling code because you don’t have my Util.logError module)


#
#	appendPen
#	Written By: Cody Warren
#	On: June 17, 2014
#
#	Adds new pen to easy chart dataset 
#
#	Required Parameters:
#	easyChart	- Easy Chart to be used
#	TagName		- Tagname to be added to the dataset
#	TagPath		- Historical tagpath to be added to the dataset
#
def appendPen(	
				easyChart,
				TagName,
				TagPath,
				Aggregation = "LastValue",
				Axis = "Default Axis",
				Subplot = "1",
				Enabled = "true",
				Color = "000000",
				Dash = "",
				Render = "1",
				Line = "1",
				Shape = "0",
				Fill = "true",
				Group = "",
				Digital = "false",
				Override = "false",
				Hidden = "false",	
			  ):
	
	import system
	import sys
	import app.Util

	try:
		row =	([
					TagName, 
					TagPath,
					Aggregation,
					Axis,
					Subplot,
					Enabled,
					Color,
					Dash,
					Render,
					Line,
					Shape,
					Fill,
					Group,
					Digital,
					Override,
					Hidden,
				])

		easyChart.tagPens = system.dataset.addRow(easyChart.tagPens, row)
	except:
		app.Util.logError("appendPen", sys.exc_info()[1])
	# end try
#end def

#
#	deletePen
#	Written By: Cody Warren
#	On: June 17, 2014
#
#	Deletes pen from easy chart tagpen dataset  
#
#	Parameters:
#	easyChart	- Easy Chart to be used
#	TagName		- Tagname to be removed from dataset
#
def deletePen(easyChart, TagName):
	import system
	import sys
	import app.Util

	try:
		# Convert easyChart data to PyDataSet
		pyData = system.dataset.toPyDataSet(easyChart.tagPens)

		# Search the dataset for the tagname
		i = 0 
		for row in pyData:
			if(row['NAME'] == TagName):
				break
		
			i = i + 1
		# end for

		# Delete row in dataset
		easyChart.tagPens = system.dataset.deleteRow(easyChart.tagPens, i)
	except:
		app.Util.logError("deletePen", sys.exc_info()[1])
	# end try
# end def

And a bit of demo code…

easyChart = event.source.parent.getComponent('Easy Chart')
TagName = 'TT_TEST_01'
TagPath = '[Historian]beerbrewing/devices/tt_test_01/in'

app.Chart.appendPen(easyChart, TagName, TagPath, Color='FFFFFF')
system.gui.messageBox('Pen Added!')

app.Chart.deletePen(easyChart, TagName)
system.gui.messageBox('Pen Removed!')

Does anybody know why I can’t get the python dataset to work? I think I’d still prefer manipulating data with that over the Ignition dataset…

There are already some screens that do this functionality. Look for the Click to graph windows on the website

Thanks drewdin, took the words right out of my mouth.

The Click to Graph can be found on our extras download page. This will give you the functionality you are looking for. The zip file contains everything you need to make this work.

Thanks for that reference on the “Click to Graph”, investigating it now. I have got one question though.

It seems on any buttons that have scripting which manipulate the database where the CTG tables reside, I get an error “data source CTG does not exist on this gateway”. Where can I change this so it refers to the database I am using?

Hey Pauly,

So inside the zip file there’s two SQL scripts to build/structure a database for it (one for MySQL and one for MSSQL). You’ll have to make a new database in SQL and run the script on it then connect it to Ignition.

I gave them a look and they’re pretty cool. Thanks for pointing me towards that guys.

Cody -

I’ve got that all sorted out, as I created the tables into an existing database. I’ve assigned all the window components to reference the database that contains the tables. But somewhere in the scripting there is a reference to the database the tables reside in when the script needs to manipulate the data. It currently it seems to refer to a database called “CTG” which I don’t have.

So I just need to figure out where “CTG” is defined, as it pertains to button scripts/gateway scripts. Otherwise I have it all up and “running” just can’t do much with it until I figure this part out.

Never mind, figured it out.

Upon the “import” of the example project, it changed my default database to “CTG”.