Bar Chart (stacked) - How to hide labels if they equal zero

Title pretty much says it. I have a bar chart that has several stacked bars and “they” want the labels to show, however, several of those bars have a value of 0 (zero). Anyone happen to know a way to hide a value of zero on a label on the Bar Chart? I have a feeling this will end up being an import of jfree libraries to access the chart from that side, but unsure how to go about it.

Any help would be great.

Thanks!

1 Like

[code]from org.jfree.chart.labels import StandardCategoryItemLabelGenerator

class myLabelGenerator(StandardCategoryItemLabelGenerator):
def generateLabel(self,dataset,series, category):
value = dataset.getValue(series,category)
if value == 0:
return None
else:
return str(int(value))

chart = event.source.parent.getComponent(‘Bar Chart’).getChart()
plot = chart.getPlot()
renderer = plot.getRenderer()
renderer.setItemLabelGenerator(myLabelGenerator())
renderer.setItemLabelsVisible(True)[/code]

3 Likes

Hi @jpark
What if I want to use StandardCategoryItemLabelGenerator(String labelFormat, NumberFormat formatter) instead of just the default StandardCategoryItemLabelGenerator() that only takes in on argument, how would that work here? I want to change the format of the text and I somehow could not figure it out to save my life… :cry:

I want to use {“2”} as the label format and either “#,#0.00” or “0” for the number format depending on a if else statement.

if I use StandardCategoryItemLabelGenerator("{2}", DecimalFormat("XXX")) it works like a charm but I cant get it to work when I try to pass the different Number formating.

You should be able to modify the subclass to use an explicit constructor (Jython’s __init__ auto-magically wraps Java constructors, even overloaded ones):

class myLabelGenerator(StandardCategoryItemLabelGenerator):
	def __init__(self, labelFormat, formatter):
		super().__init__(labelFormat, formatter)
		
	def generateLabel(self,dataset,series, category):	
		value = dataset.getValue(series,category)	
		if value == 0:
			return None
		else:
			return str(int(value))

Then use it with your label/formatter as expected:
renderer.setItemLabelGenerator(myLabelGenerator('{"2"}', myNumberFormatter))

1 Like