Easy Chart Tick Label Alignment

I am having trouble figuring out how to right align my Easy Chart tick labels. For example, the screenshot below shows the values as left aligned:

However, I would like them to be right aligned. Is there some way easy way to do this that I am missing? Right now, the only thing I can figure out is to just cover them up and type my own labels as separate components but that’s not ideal - it’s more of a patch than a solution.

I was able to do this using a custom number format and a monospaced font:

Result:

configureChart extension function script:

#def configureChart(self, chart):
	from java.awt import Font
	from java.text import NumberFormat
	
	# Create a custom number format that will right align labels up to 5 characters in length
	maxCharacterLength = 5
	class AlignedFormat(NumberFormat):
		def format(self, number, toAppendTo, pos):
			text = unicode(int(number))
			
			# Assumes that a text label will never be more than 5 characters
			# simply adds spaces to the left
			toAppendTo.append(text.rjust(maxCharacterLength))
			return toAppendTo

	for index in xrange(chart.plot.rangeAxisCount):
		axis = chart.plot.getRangeAxis(index)
		
		# Create a monospaced version of the current axis font, so kerning won't affect alignment
		originalFont = axis.tickLabelFont
		monoFont = Font(
			Font.MONOSPACED,
			originalFont.style,
			originalFont.size
		)
		
		# Update the current axis with the custom font and number format to perfectly right align the text
		axis.tickLabelFont = monoFont
		axis.setNumberFormatOverride(AlignedFormat())

This script could be made more flexible and efficient by moving it to a library function with the axis and maxCharacterLength variables as arguments.

2 Likes

Worked perfectly! Thank you @justinedwards.jle!

1 Like