Pie Chart label format

Using this post as inspiration, Pie chart labels - #6 by zxcslo,
I have data from a SQL query, two columns, name and seconds (sum). I want to display the seconds as HH:mm:ss. I tried the following code, it doesn’t work, it locks the designer.

from org.jfree.chart.labels import StandardPieSectionLabelGenerator
import datetime	
		
class LabelGenerator(StandardPieSectionLabelGenerator):
	def __init__(self, component):
		self.component = component
		StandardPieSectionLabelGenerator.__init__(self, "")
			
	def generateSectionLabel(self, dataset, key):
		defaultValues = self.createItemArray(dataset, key)
		return "{0}: {1} ({2})".format(
			key,
			defaultValues[1],
			str(datetime.timedelta(seconds=defaultValues[2]))
		)
			
	def generatedAttributedSectionLabel(self, dataset, key):
		return null
chart.plot.labelGenerator = LabelGenerator(self)

I also tried returning from SQL as string in HH:mm:ss, but then the value is displaysed as date from 1970 (I just want HH:mm:ss). I tried to format or parse in the above code and that also locked up the designer. What else can I do on this?

‘Locking’ the designer is odd, that probably means the script is going into an infinite loop somehow.
What if you change generateSectionLabel to return a constant string? e.g.

	def generateSectionLabel(self, dataset, key):
		return "test"

Does that still lock up?

That works fine, also so does this:

def generateSectionLabel(self, dataset, key):
			defaultValues = self.createItemArray(dataset, key)
			return "{0}: {1} ({2})".format(
				key,
				defaultValues[1],
				defaultValues[2]
			)

Replace defaultValues[2] with str(datetime.timedelta(seconds=defaultValues[2])) and the window will start blinking and then get unresponsive.

It sounds like there might be an uncaught exception happening in your timedelta code then. Try wrapping it in a try: except: block? To see if you can find out the error; it won’t actually fix anything.

Even if I use code from this post:

m,s = divmod(defaultValues[2], 60)
h,m = divmod(m, 60)
time = '{hr:02d}:{mn:02d}:{sc:02d}'.format(hr=h,mn=m,sc=s)

It goes bonkers. The interesting thing is I can't interact with anything on the window, but I can the project browser. I can edit the component in the browser, by disabling the extension. But, even after disabling, the window is still "locked' up. I have to restart the designer. I guess I will give IA a call.

I threw it into the IDE and get this:

Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "<extension-method configureChart>", line 21, in generateSectionLabel
  File "C:\Users\pgriffith\Projects\ignition\IDE-Launchers\gateway-launcher\src\main\home\user-lib\pylib\datetime.py", line 500, in __new__
    seconds += minutes*60 + hours*3600
TypeError: cannot concatenate 'unicode' and 'int' objects

Try

	def generateSectionLabel(self, dataset, key):
		defaultValues = self.createItemArray(dataset, key)
		return "{0}: {1} ({2})".format(
			key,
			defaultValues[1],
			str(datetime.timedelta(seconds=int(defaultValues[2])))
		)

EDIT: Nope, still not quite there:

Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "<extension-method configureChart>", line 21, in generateSectionLabel
ValueError: invalid literal for int() with base 10: '15%'

There it is:

	from org.jfree.chart.labels import StandardPieSectionLabelGenerator
	import datetime	
			
	class LabelGenerator(StandardPieSectionLabelGenerator):
		def __init__(self, component):
			self.component = component
			StandardPieSectionLabelGenerator.__init__(self, "")
				
		def generateSectionLabel(self, dataset, key):
			defaultValues = self.createItemArray(dataset, key)
			return "{0}: {1} \n({2})".format(
				key,
				defaultValues[1],
				str(datetime.timedelta(seconds=dataset.getValue(key)))
			)

		def generatedAttributedSectionLabel(self, dataset, key):
			return null
	chart.plot.labelGenerator = LabelGenerator(self)

image

2 Likes

Dear Team,
I attempted this. But i wa unable to get the desired outcome.Could you please assist me.
image
Screenshot 2024-09-30 101054

Show your code.

Otherwise, you've just posted pretty pictures.

EDIT: Also, note that the scripting in this thread is for Vision, and is incompatible with perspective.

1 Like