labeljfreechartword-wrapcategories

How to wrap category labels in JFreeChart


I have Stacked Bar Chart created using JFreeChart. The labels of the category are quite big, and they overlap with the label of the next Bar. I would like to wrap it to the next line. I did some searching and found that Ii have to use the below code.

setMaximumCategoryLabelLines(2) in the CategoryAxis

and still it doesn't wrap to the next line. Please suggest solutions.


Solution

  • Altering the original BarChartDemo1 or the current BarChartDemo1 as shown and using long category names, I see a chart similar to the one below. It seems to work with stacked rendering, too. Is that the desired effect?

    private static JFreeChart createChart(CategoryDataset dataset) {
        ...
        var domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        domainAxis.setMaximumCategoryLabelLines(2);
        ...
    }
    

    image