# Wait for query results before executing extension method code

**URL:** <https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337>\
**Category:** Ignition\
**Created:** [June 29, 2021, 10:08pm UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337 "2021-06-29T22:08:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dillon.hoefener](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dillon.hoefener](https://forum.inductiveautomation.com/u/dillon.hoefener)\
**Post date:** [June 29, 2021, 10:08pm UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/1 "2021-06-29T22:08:53Z")

</div>

Hi,  
I’ve written a function within the configureChart extension function on a Chart component that I’d like to defer execution of until the chart’s SQL query has returned results. My code is working to do what I need to do, but the afterLoad function should be called after chart’s data property SQL query returns results after an update instead of a manual timeout via the system.util.invokeLater function as I have here.

Can someone please point me in the right direction on how to go about this?

```auto
def configureChart(self, chart):
	from java.awt import Color

	def afterLoad():
		plot = chart.getCategoryPlot()
		renderer = plot.getRenderer(1)
		ds = chart.getPlot().getDataset(1)
		metricVal = self.Data.getValueAt(0,1)
		targetVal = self.Data.getValueAt(0,2)
		
		#get y coord based on chart width,
		#then pad the Y coord value to place arrows
		#at correct positions relative to bars
		axis = plot.getRangeAxis()
		area = self.getChartRenderingInfo().getPlotInfo().getPlotArea()
		axisEdge = plot.getRangeAxisEdge()
		yCoord = axis.valueToJava2D(targetVal, area, axisEdge)
		padding = 100+25
		arrowCoord = yCoord+padding
		try:
			window = system.gui.findWindow('Production/PlantCostView')[0]
		except:
			print 'Cannot find window'
		arrow = window.getRootContainer().getComponent('targetArrowLE')
		print arrowCoord
		system.gui.transform(arrow, newX=arrowCoord)
		
		#color bars based on performance
		if metricVal >= targetVal:
			renderer.setSeriesPaint(0, Color.GREEN)
		else:
			renderer.setSeriesPaint(0, Color.RED)
	try:
		system.util.invokeLater(afterLoad,5000)
	except:
		pass

```

Thanks,  
Dillon

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [June 30, 2021, 12:30am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/2 "2021-06-30T00:30:41Z")

</div>

Have you tried utilizing the propertyChange event handler for the chart? I’m also assuming this in Vision and the ‘Data’ property of the chart has the SQL binding on it?

Place this in the propertyChanged event handler. Since confgureChart() is technically a function of the chart I believe you should be able to call it like you would any other function

```auto
if propertyName == 'Data':
	self.configureChart()
	

```

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [June 30, 2021, 12:47am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/3 "2021-06-30T00:47:03Z")

</div>

The easiest way for this one is to replicate the script from configureChart to property Change when ‘Data’ change. In this scope “chart” won’t be defined. Instead, define it manually with:

```auto
chart = event.source.getChart()

```

There’s a clever way of doing this but you’ll need to ask via message.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [June 30, 2021, 12:59am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/4 "2021-06-30T00:59:14Z")

</div>

where that script should be placed?  
Because I see syntax error everywhere.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [June 30, 2021, 1:34am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/5 "2021-06-30T01:34:53Z")

</div>

If you right click the chart component you should see ‘Scripting’ in the list that pops up.

Select that, then on the top left you should see where it says ‘Event Handlers’.

Under that you should see a folder called ‘properyChange’

Expand that folder and select ‘propertyChange’

Then select the ‘Script Editor’ tab in the upper right of the window

Place the script I gave into there. You might have to double check spacing/tabbing, I know the forums can screw with formatting some times.

This is all assuming you are working in vision

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [June 30, 2021, 1:37am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/6 "2021-06-30T01:37:22Z")

</div>

well haha.  
I’ll just ignore that.  
I was being sarcastic.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [June 30, 2021, 1:53am UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/7 "2021-06-30T01:53:58Z")

</div>

Not going to lie, I didn’t pay attention and thought you were the original poster asking…

Its been a day 🙃

---

<div class="post-metadata">

**Author:** ![dillon.hoefener](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dillon.hoefener](https://forum.inductiveautomation.com/u/dillon.hoefener)\
**Post date:** [July 8, 2021, 9:42pm UTC](https://forum.inductiveautomation.com/t/wait-for-query-results-before-executing-extension-method-code/48337/8 "2021-07-08T21:42:52Z")

</div>

Thanks, guys.  
@jespinmartin1 , your method works just fine and is a simple change so I’m going to implement as you’ve described.  
@ryan.white , as you wrong the call to configure chart from propertyChange, the propertyChange script can’t refer to self to call the fnc. Also tried event.source but that didn’t work either.

I appreciate the quick responses, it took me a little while to circle back to this.
