Easy Chart - How to change cursor to Hand, when on point

Does anybody (@justinedwards.jle maybe) know, how to change the cursor to HAND when on point on Easy Chart in Vision?
EasyChartCursorChangeOnPoint
I have this (custom ChartMouseListener from another forum post and it is working great) in the MouseReleased event of the EasyChat to allow users to click on the point on the chart, to show relevant data:

from org.jfree.chart import ChartMouseListener

class myChartMouseListener(ChartMouseListener):
	def chartMouseMoved(self, cme):
		pass
	
	def chartMouseClicked(self, cme):
		chartEntity = cme.getEntity()
		
		if chartEntity is not None:
			epochLong = chartEntity.getDataset().getX(chartEntity.getSeriesIndex(),  chartEntity.getItem()).longValue()
			date_issued = str(system.date.fromMillis(epochLong))
			selectQuery = "SELECT order_id, zacetek FROM orders_krpan WHERE zacetek = ?::timestamp"
			args = [date_issued]
			results= system.db.runPrepQuery(query=selectQuery,args=args,database=my_scripts.database)
			if len(results)>0:
				order_id = results[0][0]
				date_issued = results[0][1]
				selectQuery = "SELECT * FROM izdelki_podatki_krpan WHERE narocilo = ?"
				args = [order_id]
				results= system.db.runPrepQuery(query=selectQuery,args=args,database=my_scripts.database)

				event.source.parent.ds = results
					
		chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
		chart.removeChartMouseListener(self)

chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
chart.addChartMouseListener(myChartMouseListener())

I'm assuming that one must write another listener somewhere, but I'm not good at Java... and I couldn't find any forum posts about similar... things...

I'm not where I can test code, but you should be able to import cursor and use your chartMouseMoved function. It will look like this:

from java.awt import Cursor

class myChartMouseListener(ChartMouseListener):
	def chartMouseMoved(self, cme):
		chartEntity = cme.getEntity()
		chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
		if chartEntity is not None:
			chart.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR))
		else:
			chart.setCursor(Cursor.getDefaultCursor())
1 Like

Thank you @justinedwards.jle , I knew, that you were the man... :+1:

For 'some' reason I had to move the listener from MouseReleased to MouseMoved.

class myChartMouseListener(moje_skripte.ChartMouseListener):
	def chartMouseMoved(self, cme):
		chartEntity = cme.getEntity()
		chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
		if chartEntity is not None:
			chart.setCursor(moje_skripte.Cursor.getPredefinedCursor(moje_skripte.Cursor.HAND_CURSOR))
		else:
			chart.setCursor(moje_skripte.Cursor.getDefaultCursor())
		
		chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
		chart.removeChartMouseListener(self)
#
	def chartMouseClicked(self, cme):
		chartEntity = cme.getEntity()
		
		if chartEntity is not None:
			epochLong = chartEntity.getDataset().getX(chartEntity.getSeriesIndex(),  chartEntity.getItem()).longValue()
			zacetek = str(system.date.fromMillis(epochLong))
			selectQuery = "SELECT order_id, zacetek FROM orders_krpan WHERE zacetek = ?::timestamp"
			args = [zacetek]
			results= system.db.runPrepQuery(query=selectQuery,args=args,database=my_scripts.database)
			if len(results)>0:
				order_id = results[0][0]
				zacetek = results[0][1]
				selectQuery = "SELECT * FROM izdelki_podatki_krpan WHERE narocilo = ?"
				args = [order_id]
				results= system.db.runPrepQuery(query=selectQuery,args=args,database=my_scripts.database)
				event.source.parent.ds = results
					
		chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
		chart.removeChartMouseListener(self)

chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
chart.addChartMouseListener(myChartMouseListener())

And here is the result:
Animation

This repeated call within the listener bugs me. I believe this can simply be deleted, since the variable has already been created below. Furthermore, the chart is the source of the event, so if the variable did need to be created, you could simply use cme.source, or for that type of listener, IIRC, you can use cme.chart (event.chart).

I'm certain there are other improvements to made as well, but unfortunately, I'm replying from my phone. It's going to be a while before I get home to my test environment.

Edit:

If this means the MouseMoved event handler of the Easy Chart, this is wrong. The script will be constructing and adding a listener every time the mouse moves a pixel. Run the script from the componentRunning property change event, so the listener only gets constructed and added once when the Easy Chart is first initialized. Then, you won't need this line of code:

I found some time to play around with this in my test environment, and I believe this to be the proper script for what you are doing:

# Check if the event property name is 'componentRunning'
# This will run once and only once when the component is first initialized
# Note: To see the effect if this in the designer, preview mode must be started prior to opening the window
if event.propertyName == 'componentRunning':
	# Import the ChartMouseListener class
	# ...and define a custom ChartMouseListener class that extends it
	from org.jfree.chart import ChartMouseListener
	class myChartMouseListener(ChartMouseListener):
		
		# This method is triggered by any mouse movement within the chart
		def chartMouseMoved(self, mouseEvent):
			
			# Check if mouseEvent has an entity
			# Set the cursor to HAND_CURSOR if an entity is present
			if mouseEvent.entity:
				chart.setCursor(moje_skripte.Cursor.getPredefinedCursor(Cursor.HAND_CURSOR))
			
			# Set the cursor to the default cursor if no entity is present
			else:
				chart.setCursor(moje_skripte.Cursor.getDefaultCursor())
				
		# This method is triggered by mouse clicks within the chart
		def chartMouseClicked(self, mouseEvent):
			
			# mouseEvent.entity gets used a lot in this method,
			# ...so assign it to an easy to work with variable
			entity = mouseEvent.entity
			
			# If the mouse event has an entity, get the epochLong value from the entity's dataset
			# ...and use it as argument in a prep query
			if entity:
				epochLong = entity.dataset.getX(entity.seriesIndex,  entity.item).longValue()
				results = system.db.runPrepQuery(
					query="SELECT order_id, zacetek FROM orders_krpan WHERE zacetek = ?::timestamp",
					args=[unicode(system.date.fromMillis(epochLong))],
					database=my_scripts.database)
				
				# If the query returns a result, use the [0],[0] value as an argument in a subsequent query
				# Assign the result of the subsequent query to a dataset property
				if results:
					event.source.parent.ds = system.db.runPrepQuery(
						query="SELECT * FROM izdelki_podatki_krpan WHERE narocilo = ?",
						args=[results[0][0]],
						database=my_scripts.database)
						
	# Get the inner chart from the Easy Chart component, and add the custom listener to it.
	chart = event.source.parent.getComponent('Easy Chart').getComponent(0).getComponent(0)
	chart.addChartMouseListener(myChartMouseListener())
1 Like

Yup, it's working like it should.
Thank you @justinedwards.jle , I appreciate your efforts very much. :+1: