1

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.

dic19
  • 17,821
  • 6
  • 40
  • 69
Camilo
  • 199
  • 2
  • 13
  • An SSCCE might help. http://www.sscce.org/. How about adding the string " " to the end of each item in the legend? By " " I mean a string of 6 spaces: Stack Overflow formatted my " " incorrectly. – George Tomlinson Mar 06 '14 at 21:22
  • Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=116838&p=177519). – trashgod Mar 08 '14 at 02:48

2 Answers2

6

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.

David Gilbert
  • 4,427
  • 14
  • 22
  • It will be the renderer but you could also pass in the plot (the legend requires a LegendItemSource...this interface is implemented by plots and renderers). – David Gilbert Dec 05 '16 at 08:25
-3

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

"2012 ".

George Tomlinson
  • 1,871
  • 3
  • 17
  • 32
  • Thank you! It worked. However, is there a way to make it throug the JFreechart API ? just wondering.... – Camilo Mar 06 '14 at 21:40
  • I tryed that method as well. Nevertheless It is about the padding between legends and its surrounding rectangle, not between them. I posted a question in JFreechart forum, keep waiting their response. – Camilo Mar 06 '14 at 21:54
  • No problems. Yes, I just discovered that. Note that a legend is a whole set of labels, not just one, not that this will necessarily help you to answer the question. – George Tomlinson Mar 06 '14 at 22:08
  • No George! Go directly to jail, do not pass Go! – David Gilbert Mar 07 '14 at 11:09
  • 3
    Those spaces. You should not change your underlying data just to control the presentation, it will come back and bite you (or someone else) almost every time. There is an 'itemLabelPadding' attribute in JFreeChart to give you control over the presentation here and even if there wasn't, JFreeChart is open source so you can change the way it presents things without resorting to tricks. Unless you're in a hurry and your boss is not very nice, in which case the spaces are fine. – David Gilbert Mar 08 '14 at 05:26
  • lol. I did try the `setItemLabelPadding()` method, but so far I was only able to alter the size of the legend with it, as opposed to the gap between labels. – George Tomlinson Mar 09 '14 at 09:58