Hide some category labels in JFreeChart to avoid overlapping

谁说我不能喝 提交于 2019-12-17 21:32:45

问题


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:

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!


回答1:


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




回答2:


You can use setVerticalTickLabels(true) on your domain axis, as shown in this example.

Addendum: Oops, the example cited is for a ValueAxis. For a CategoryAxis, as used in StackedBarChart, you have even more flexility with the method setCategoryLabelPositions(). Typical usage is illustrated in the BarChartDemo1 source, shown here.



来源:https://stackoverflow.com/questions/10913201/hide-some-category-labels-in-jfreechart-to-avoid-overlapping

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