Question

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:

No space between legends

and this is how I want it to look like

space between legends

I'll appreciate your help.

Was it helpful?

Solution 2

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

"2012 ".

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top