Set X-Trace on Easy Chart to specific date/time

I honestly would have expected changing the plot's domainCrosshairValue to move the xTrace, but I couldn't get it to work event with a repaint. What I did get to work was calculating the x-coordinate from the domain location and simulating a mouse click using robot. Here is the custom method I developed that simply requires the specified date in millis to be passed in:

def setXTrace(self, timeStampInMillis):
	from java.awt import Robot
	from java.awt.event import MouseEvent
	
	# Get the internal EasyChart Class, the JFreeChart, and the plot
	chartField = self.getClass().getDeclaredField('chart')
	chartField.setAccessible(True)
	easyChartPanel = chartField.get(self)
	chart = easyChartPanel.chart
	plot = chart.plot
	
	# Calculate the x-coordinate based on the time in millis
	xCoordinate = int((timeStampInMillis - plot.domainAxis.lowerBound) / (plot.domainAxis.upperBound - plot.domainAxis.lowerBound) * easyChartPanel.width)
	
	# Create a robot for simulating mouse events
	robot = Robot()
	
	# Simulate a mouse click event at the calculated x-coordinate within the easyChart
	robot.mouseMove(easyChartPanel.locationOnScreen.x + xCoordinate, easyChartPanel.locationOnScreen.y)
	robot.mousePress(MouseEvent.BUTTON1_MASK)
	robot.mouseRelease(MouseEvent.BUTTON1_MASK)

Alternatively, you could simply draw a value marker on the chart to mark the location of the alarm event. Here is an example I developed for the easy chart that you could possibly adapt to your requirement:

1 Like