Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rotate itemLabels above bars in bar chart of a jasper report?

I need to rotate the itemLabels of a bar chart about 90° so that they have vertical orientation. The itemLabels are NOT those on the axis, but the ones above the bars. I have to do this because otherwise the itemLabels would overlap. I know there are ways to prevent labels from overlapping, but I have a specific request to make them be oriented vertically. If there is a way please tell me.

like image 771
mismor Avatar asked Oct 29 '25 13:10

mismor


1 Answers

I figured out how to solve the problem. You have to set the BasePositiveItemLabelPosition within the customize function of your ChartCustomizer which itself extends the JRAbstractChartCustomizer. The overridden costomize method takes two parameters. In this case "chart" is the first one which is a JFreeChart. You have got to get the right renderer from the chart, which in this case is the CategoryPlotRenderer. The renderer has a function to set the BasePositiveItemLabelPosition, with which you can define where the ItemLabels will be positioned and how they will be rotated (-Math.PI / 2 -> 90° counterclockwise).

CategoryItemRenderer renderer = chart.getCategoryPlot().getRenderer();
    ItemLabelPosition itemLabelPosition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT,
            TextAnchor.CENTER_LEFT, -Math.PI / 2);
    renderer.setBasePositiveItemLabelPosition(itemLabelPosition);
like image 187
mismor Avatar answered Oct 31 '25 12:10

mismor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!