问题
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