Stacked chart with percentage composition inside a bar and general over a bar in JFreeChart
I am trying to create a Stacked Bar Chart. My requirement is that I need the percentage composition inside the bar and the total score on the top bar. Please suggest solutions.
My requirements: Example: http://www.jfree.org/jfreechart/api/javadoc/images/StackedBarRenderer3DSample.png
I want a percentage composition inside the bar and an overall composition at the top of the panel.
a source to share
It's not clear what you are doing right now, but using StackedBarRenderer
c will setRenderAsPercentages(true)
display the percentages. To get the total, add StackedBarRenderer
, loop through the dataset for each column, and override drawItem()
to draw the result. An example can be found in the JFreeChart Demo as part StackedBarChartDemo3
.
Alternatively, consider a custom CategoryToolTipGenerator
one added via setBaseToolTipGenerator()
.
Appendix: you linked to an example using StackedBarRenderer3D
which also has a method setRenderAsPercentages()
. It can be extended in a similar way.
a source to share
I faced the same problem. For some reason, the latest version of JFreeChart does not display percentage composition inside the panel. This is how I got it working:
StackedBarRenderer br = new StackedBarRenderer(true); //enable perc. display
br.setBarPainter(new StandardBarPainter());
br.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
br.setBaseItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(br);
Hope it helps
a source to share