Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart

笑着哭i 提交于 2019-12-13 15:26:46

问题


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.


回答1:


It's not clear what you are doing now, but using a StackedBarRenderer with setRenderAsPercentages(true) will display the percentages. To get the total, extend StackedBarRenderer, loop through the dataset for each column, and override drawItem() to draw the result. An example may be found in the JFreeChart Demo as part of StackedBarChartDemo3.

As an alternative, consider a custom CategoryToolTipGenerator, added via setBaseToolTipGenerator().

Addendum: You linked to an example using StackedBarRenderer3D, which also has a setRenderAsPercentages() method. It can be extended similarly.




回答2:


I ran into the same problem too. For some reason the latest version of JFreeChart does not display the percentage composition inside the bar. Here's how I got it to work:

    StackedBarRenderer br = new StackedBarRenderer(true); //enable perc. display
    br.setBarPainter(new StandardBarPainter());
    br.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    br.setBaseItemLabelsVisible(true);
    chart.getCategoryPlot().setRenderer(br);

Hope this helps



来源:https://stackoverflow.com/questions/2757287/stacked-bar-chart-with-percentage-composition-inside-the-bar-and-total-above-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!