Help with Easy Chart Time Axis

I am struggling to format a chart time axis and I am hoping this community can help!

I have a real-time Easy Chart with a 30 minute time duration. By default, it puts gridlines every 2 minutes. I tried to change the gridline intervals by using the following script in configureChart (guided by script found in the forum):

plot = chart.getPlot()

from org.jfree.chart.axis import DateTickUnit

plot.domainAxis.tickUnit = DateTickUnit(DateTickUnit.MINUTE, 5)

That did change gridlines to every 5 minutes but it also changed the tick labels to show only the date with no time:

image

(Side note: it also stopped the scrolling feature of the real time chart but I found a workaround by copy and pasting the chart - the new copy will scroll)

I then tried to change the format of the time axis to show only the time (with no date) by using the following script (again, guided by script I found in the forum):

from org.jfree.chart.axis import DateAxis
from java.text import SimpleDateFormat

pureDateFormat = SimpleDateFormat("")
pureTimeFormat = SimpleDateFormat("HH:mm")
dateAxis = DateAxis()
plot.setDomainAxis(dateAxis)
plotField1 = plot.getClass().getDeclaredField('dateFormat')
plotField2 = plot.getClass().getDeclaredField('timeFormat')
plotField1.setAccessible(True)
plotField2.setAccessible(True)
plotField1.set(plot, pureDateFormat)
plotField2.set(plot, pureTimeFormat)

But then that reset the gridlines to every 2 minutes (and also changed the font of my tick labels).

If I change the order of the script to set the date format first, then set the 5 minute gridline interval, the date format resets to show the date and not the time. So it seems that one script overwrites the other.

Can anyone help me to set the gridline interval to 5 minutes and show the time instead of the date?

Much appreciated!

I was able to do it on configureChart in this way:

#def configureChart(self, chart):
	
	# Only run in real time mode
	if self.chartMode == self.MODE_REALTIME:
		
		# Documentation Pointers:
		# 	com.inductiveautomation.factorypmi.application.components.chart.AutoTickDisplayDateAxis
		# 	https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.49/com/inductiveautomation/factorypmi/application/components/chart/AutoTickDisplayDateAxis.html
		
		# Get the default tick unit and date format parameters from the domain axis
		domainAxis  = chart.plot.domainAxis
		defaultTickUnit = domainAxis.tickUnit
		defaultDateFormat = domainAxis.dateFormat
		
		# Construct custom tick unit and date format instances
		fiveMinuteTickUnit = defaultTickUnit.__class__(defaultTickUnit.MINUTE, 5)
		hourMinuteDateFormat = defaultDateFormat.__class__('HH:mm')
		
		# Set the tick unit and override the date format using the custom class instances
		domainAxis.tickUnit = fiveMinuteTickUnit
		domainAxis.dateFormatOverride = hourMinuteDateFormat

Thank you! This worked expect it stopped the chart from scrolling (it should always be showing the last 30 minutes but now it’s frozen at the last time). Is there a way to re-enable that? The cut-and-paste workaround that I used before didn’t work.

I'm not sure what the correct way to programmatically control play and pause would be, but I know the real time mode will automatically start playing if the unit count changes, so perhaps the simplest approach is to just toggle the unit count by some arbitrary amount at the end of the script.

		# Trigger a unitCount propertyChange event to jump start the real time mode after reconfiguration
		self.unitCount = 31 # set to 31 minutes, so the value actually changes,
		self.unitCount = 30 # ...and immediately set it back to the desired 30 minutes

That worked like a charm! Thank you so much!

1 Like