Hide some category labels in JFreeChart to avoid overlapping

jfreechart

I'm generating a StackedBarChart using JFreeChart. Depending on the input data I can have a lot of categories (usually between 20 and 40), leading to overlapping of the labels.
In the following screenshot you can see the chart with categories from 1 to 38:

chart with categories from 1 to 38, overlapping labels

I'd like to show some of the category labels as reference, but not all. It would be perfect to show the first and last, and every fifth inbetween.
Is this possible?

I can't change the width of the chart, and making the labels smaller does only work if they are so small that you can't read them anymore…
Last resort would be to hide the whole category axis…

Thanks for any suggestions!

Best Answer

One simple solution is to set the Category lables to the backround colour (in this case white).

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setTickLabelPaint("Category 2", Color.white);
    domainAxis.setTickLabelPaint("Category 4", Color.white);

This will produce a chart like this

enter image description here

Related Topic