I am trying to create a Stacked Bar Chart. My requirement is I need percentage composition inside the bar and total count on top of the Bar. Please suggest solutions.
My Requirement:
Sample : http://www.jfree.org/jfreechart/api/javadoc/images/StackedBarRenderer3DSample.png
I want percentage composition inside the bar and total composition on the top of the bar.
Best Answer
It's not clear what you are doing now, but using a
StackedBarRenderer
withsetRenderAsPercentages(true)
will display the percentages. To get the total, extendStackedBarRenderer
, loop through the dataset for each column, and overridedrawItem()
to draw the result. An example may be found in the JFreeChart Demo as part ofStackedBarChartDemo3
.As an alternative, consider a custom
CategoryToolTipGenerator
, added viasetBaseToolTipGenerator()
.Addendum: You linked to an example using
StackedBarRenderer3D
, which also has asetRenderAsPercentages()
method. It can be extended similarly.