Stacked Bar Chart Labels

Hi ALL,
I have stacked bar charts and would like to total the bars and put the total over the bar. I have the values on each of the three parts of the bar, but would like to place the total above the bar.
Anyone done this?

Thanks.

(added a screenshot)

I don’t see a built-in option for this, but you could get ‘total’ labels with a hacky solution:

  1. Make a copy of your original chart
  2. Change the dataset to only contain 1 column filled with the total of the old columns
  3. Set the bar label offset to 0, and set most of the color properties to be transparent.

Overlay them and it looks like this:

Unfortunately this doesn’t get rid of the horizontal tick marks, which will appear over the existing chart labels.
Good luck!

I was looking to do something similar a couple of weeks ago, check out this thread here Moving value label positions on chart bar graph? and look up the ItemLabelAnchor and ItemLabelPosition jFreeChart classes.

bfusion and brian,
thank you. I ended up taking the easy route, stacking two charts. Works great.
I would like to play around with jFreeChart classes a bit more, just not enough hours in the day.

1 Like

After running into this same issue and many hours running through the forums. I think i found a way to do this. though it is not perfect.

if event.propertyName == "data":
	from org.jfree.chart.labels import CategoryToolTipGenerator
	from org.jfree.chart.labels import StandardCategoryItemLabelGenerator
	from org.jfree.chart.labels import ItemLabelPosition
	from org.jfree.chart.labels import ItemLabelAnchor
	from org.jfree.ui import TextAnchor
	from java.awt import Color

	class myLabelGenerator(StandardCategoryItemLabelGenerator):
		def generateLabel(self,dataset,series,category):	
			value = dataset.getValue(series,category) + dataset.getValue(series+1,category) 	
			if value == 0.0:
				return None
			else:
				return str(round(value,1))
				
	class CustomToolTipGenerator(CategoryToolTipGenerator):
		lines = event.source.parent.getComponent('Bar Chart 1').LineName
		lineName = lines.getColumnAsList(0)
		combined = event.source.parent.getComponent('Toggle Button').selected
		def generateToolTip(self,dataset,row,column):
			OPTag = "OP" + dataset.getColumnKey(column)
			if self.combined:
				OPName = self.lineName[column]
			else:
				OPName = OPTag
			
			return "%s = %s %.1f"%(OPName,dataset.getRowKey(row),(dataset.getValue(row,column)))
		
	chart = event.source.getChart()
	renderer = chart.getPlot().getRenderer()
	renderer.setToolTipGenerator(CustomToolTipGenerator())
	renderer.setSeriesItemLabelGenerator(0,myLabelGenerator())
	renderer.setSeriesItemLabelPaint(1, Color(0,0,0,0))
	renderer.setBasePositiveItemLabelPosition(ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.BOTTOM_CENTER))

This bit of code contains a custom label generator that converts the labels from the 1st series to be a total of all values in that category. and also hides the labels for the other series. there is also some code for a custom tool tip but that isn’t needed here. To move the label just change the Anchors and if you want to add a label that is the total while leaving all other labels un changed, simply add a new column to your data property to create a new series. set all data values for that series to 0 then apply the custom label generator to that new series.

1 Like

Forgot to mention the script is put onto the bar charts property changed event script and is in version 7.9.XX

1 Like