Align bars with bar labels in Jfreechart

巧了我就是萌 提交于 2019-11-28 10:01:40

问题


I have a problem with data labels to align with correct bar in this chart.
This bar chart is generated using jfreechart in jsp.


回答1:


This is a pretty common mistake in JFreeChart. Each category in your chart ('abc', 'xyz' etc) has a value for each of the 6 series ('q1', 'q2' ... 'q6'), so there are 36 data items in all. But 30 of those data items are null, because you didn't specify them. JFreeChart leaves a space where the bar would appear, when the data value is null.

If you really have only six data values, you should have one series with 6 categories, or one category and six series.




回答2:


The simple workaround is to make this chart stacked bar instead of bar one.

This may be done by using ChartFactory.createStackedBarChart instead of ChartFactory.createBarChart or passing StackedBarRenderer to the Plot constructor instead of BarRenderer depending on how you create your chart.

This works because in stack bar chart the data part is always centered on the label, and the null/zero data parts are just not visible.

This is useful when you can't add your data as one series (for example because you want different colors), but also can't add them as one category (for example you want to have labels for each one).



来源:https://stackoverflow.com/questions/19116867/align-bars-with-bar-labels-in-jfreechart

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