JFreeChart gap between legend labels

安稳与你 提交于 2019-12-08 06:30:06

问题


I've been using JFreeChart in order to plot some series but I have a doubt related to the space between the labels in the legend.

Does anyone know how to set a space between legends? For example, right now:

and this is how I want it to look like

I'll appreciate your help.


回答1:


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

"2012 ".




回答2:


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.



来源:https://stackoverflow.com/questions/22235824/jfreechart-gap-between-legend-labels

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