JFreeChart gap between legend labels

末鹿安然 提交于 2019-12-06 16:03:36

Just go to the place in your code where the legend items are set, and change "2012" to

"2012 ".

You can use the setItemLabelPadding() method in the LegendTitle class.

LegendTitle legend = chart.getLegend();
legend.setItemLabelPadding(new RectangleInsets(2, 2, 2, 30));

The only issue with this approach is that it also leaves whitespace after the last item in each row of the legend.

If you don't mind a bit more complexity, you can remove the default legend and create a new one with a few parameters specified for the layout:

chart.removeLegend();
FlowArrangement hlayout = new FlowArrangement(
        HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 20, 2);
LegendTitle legend = new LegendTitle(r, hlayout, new ColumnArrangement());
legend.setPosition(RectangleEdge.BOTTOM);
chart.addLegend(legend);

The legend requires a horizontal layout when it is positioned at the top or bottom of the chart and a vertical layout when it is positioned at the left or right of the chart. Here we have customised the horizontal layout only, specifying that the items should have a flow layout, be centered with a gap of 20 between each item and, if wrapping lines, a gap of 2 between lines. I think this gets the result you are asking for.

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