Chart set bar width on stacked

#1

I used the chart tool to create a stacked bar chart. I cannot figure out how to set the width of the bar with any functions I try in the configureChart extension function. Does anyone know how to do this ?
When I make the entire object narrower in the designer it makes the text to small in the legend.
Here is the code I have in the configureChart extention function for setting legend font, getting rid of the gradient, shadow and background grid:

def configureChart(self,chart):
	from java.awt import Color
	from org.jfree.chart.renderer.category import StandardBarPainter
	from  java.awt import Font
	
	darkColor = Color(43,43,43)	
	lightColor = Color(213,213,213)
	
	plot = chart.getPlot()
	legend = chart.getLegend()
	
	chart.setBackgroundPaint(darkColor)
	
	plot.setBackgroundPaint(darkColor)
	plot.setDomainGridlinesVisible(0) #turn off background grid lines
	plot.setRangeGridlinesVisible(0) #turn off background grid lines
	plot.setOutlineVisible(0) #turn off background outline
	
	legend.setBackgroundPaint(darkColor)
	legend.setItemPaint(lightColor) # sets font color in legend
	d = Font("Dialog", Font.BOLD, 14) 
	legend.setItemFont(d) #sets font in legend
	legend.setBorder(0, 0, 0, 0) #removes the legend border
	
	#removes the gradient rendering of bars and replaces with single colors
	renderer = plot.getRenderer()
	renderer.setBarPainter(StandardBarPainter())
	renderer.setShadowVisible(0)  #removes shadow on bars
	
	#these functions didn't work, error reported these methods didn't exist or they had no effect
#	renderer.setMaximumBarWidth(1)
#	renderer.setMaximumBarWidth(5)
#	renderer.setSeriesBarWidth(2,4)

0 Likes

#2

Looking into it, it looks like you need to provide a custom subclass of StandardBarPainter - you’ll notice StandardBarPainter calls paintBar with a BarRenderer - and that BarRenderer has a getMaximumBarWidth() method you could override.

0 Likes

#3

I am still very new to using jython and external libraries. Can you provide a quick example ?
Thanks

0 Likes

#4

Actually, looking at it again, your commented out code should work fine - you’re just missing one aspect - the setMaximumBarWidth function expects you to pass a double, which will be a percentage used:

Sets the maximum bar width, which is specified as a percentage of the available space for all bars

Try passing 0.1 or 0.2 and see if you notice a difference in the final chart.

0 Likes

#5

That worked thanks. I added the lineb below which resulted in the graph below. Now to figure out how to get rid of the little gray line at the top…

renderer.setMaximumBarWidth(0.2) #value must be double and is a normalized percentage range 0 to 1

The complete code:

def configureChart(self, chart):
	darkColor = Color(43,43,43)	
	lightColor = Color(213,213,213)
	
	plot = chart.getPlot()
	legend = chart.getLegend()
	
	chart.setBackgroundPaint(darkColor)
	
	plot.setBackgroundPaint(darkColor)
	plot.setDomainGridlinesVisible(0) #turn off background grid lines
	plot.setRangeGridlinesVisible(0) #turn off background grid lines
	plot.setOutlineVisible(0) #turn off background outline
	
	legend.setBackgroundPaint(darkColor)
	legend.setItemPaint(lightColor) # sets font color in legend
	d = Font("Dialog", Font.BOLD, 14) 
	legend.setItemFont(d) #sets font in legend
	legend.setBorder(0, 0, 0, 0) #removes the legend border
	
	from org.jfree.chart.renderer.category import BarRenderer
	from org.jfree.chart.renderer.category import StandardBarPainter
	
	#removes the gradient rendering of bars and replaces with single colors
	renderer = plot.getRenderer()
	print type(renderer)

	renderer.setBarPainter(StandardBarPainter())
	renderer.setShadowVisible(0)
	renderer.setMaximumBarWidth(0.2) #value must be double and is a normalized percentage range 0 to 1

1 Like