Ad-hoc trending - drop on button press?

I think I've got a solution. I needed a way to mimic dragging and dropping on a touch screen that was incapable of doing so. So instead of over-complicating it, I just used a button to put the selected item in the tag browse tree into the easy chart.

It uses Ignition's addPen custom method from their demo project but I adjusted it slightly to work for us.

So, you will need three components on screen - Tag Browse Tree, Easy Chart and a Button.

Your Tag Browse Tree will need an added custom property "tagPath". This property needs to be bound to {pathToYourComponent.selectedPaths}[0,0] and also ensure that the Tag Browse Tree is set to Single Selection under Behavior/Selection Mode.

The Button should then have the custom property of "penColors" which is a Dataset.
This is the data copied from the dataset:

"#NAMES"
"color"
"#TYPES"
"clr"
"#ROWS","10"
"color(255,0,0,255)"
"color(0,0,255,255)"
"color(0,255,0,255)"
"color(255,255,0,255)"
"color(0,0,0,255)"
"color(255,0,255,255)"
"color(170,170,170,255)"
"color(255,140,0,255)"
"color(0,255,255,255)"
"color(148,105,220,255)"

Then you will need this script on the button pressed event handler: (obviously you'll need to change paths to suit your needs)

tagPath = event.source.parent.getComponent('Tag Browse Tree').tagPath
fullTagPath = tagPath
tagProvider = "default"
queryMode = 0

def addPen(fullTagPath, tagPath, tagProvider, queryMode):

from random import randint

topColors = system.dataset.toPyDataSet(event.source.penColors)

tagPath = tagPath.upper()

if tagProvider != None and len(tagProvider):
realtimeTagPath = "[%s]%s" % (tagProvider, tagPath)
else:
realtimeTagPath = "[default]%s" % tagPath

try:
min = system.tag.read("%s.EngLow" % realtimeTagPath).value
max = system.tag.read("%s.EngHigh" % realtimeTagPath).value
except:
min = 0.0
max = 100.0

tagName = tagPath.split("/")[-1]
group = ""

axes = event.source.parent.getComponent('Group').getComponent('Easy Chart').axes
tagPens = event.source.parent.getComponent('Group').getComponent('Easy Chart').tagPens

numPens = tagPens.getRowCount()
try:
colorStr = topColors[numPens][0]
except:
c1 = randint(0,255)
c2 = randint(0,255)
c3 = randint(0,255)
colorStr = "color(%d,%d,%d,255)" % (c1,c2,c3)

existingTagPenNames = [row[0] for row in system.dataset.toPyDataSet(tagPens)]
existingTagPenPaths = [row[1] for row in system.dataset.toPyDataSet(tagPens)]

if fullTagPath not in existingTagPenPaths:
if tagName in existingTagPenNames:
tagName += " %d" % (tagPens.getRowCount() + 1)

  minValue = None
  maxValue = None
  avgValue = None
  
  try:
  	s = event.source.parent.getComponent('Group').getComponent('Easy Chart').startDate
  	e = event.source.parent.getComponent('Group').getComponent('Easy Chart').endDate
  	historyValues = system.tag.queryTagHistory(paths=[fullTagPath, fullTagPath, fullTagPath], startDate=s, endDate=e, returnSize=1, columnNames=['%sMin', '%sMax', '%sAvg'], aggregationModes=['Minimum', 'Maximum', 'Average'])
  	if historyValues.getRowCount():
  		minValue = historyValues.getValueAt(0, 1)
  		maxValue = historyValues.getValueAt(0, 2)
  		avgValue = historyValues.getValueAt(0, 3)
  except:
  	pass
  	
  newRow = [tagName,fullTagPath,"MinMax",tagName,1,True,colorStr,"",1,1.0,0,True,False,group,False,False,False,True,"",True]
  newAxisRow = [tagName,tagName,"Numeric","color(0,0,0,255)","color(0,0,0,255)","color(0,0,0,255)",0,False,True,False,0.05,min,max,True,5.0,5.0,"","normal","",False,"color(232,234,232,128)","color(0,0,0,0)"]
  axes = system.dataset.addRow(axes, newAxisRow)
  tagPens = system.dataset.addRow(tagPens, newRow)
  event.source.parent.getComponent('Group').getComponent('Easy Chart').axes = axes
  event.source.parent.getComponent('Group').getComponent('Easy Chart').tagPens = tagPens

else:
system.gui.messageBox("'%s' already exists" % tagPath)

addPen(fullTagPath, tagPath, tagProvider, queryMode)

I then bound the text of the button to an expression showing the selected tag, just to show that it knows which pen to add:

"Add " +
right({Root Container.Tag Browse Tree.tagPath},
len({Root Container.Tag Browse Tree.tagPath})- 1 -
lastIndexOf({Root Container.Tag Browse Tree.tagPath},"/"))
+
" to Chart"

You may need to play around with the number of columns in your Tag Pens dataset and adjust accordingly in the script.

Hope that helps!

1 Like