Take the 2-minute tour ×

I need to add break line in some legend in JFree Chart. I have some legends with 316 characters and need to break every 80. Finally, I'll have 4 lines.

Anyway, I tried with "\n", "\u2424" and " ". It did nothing. (From http://www.jfree.org/forum/viewtopic.php?f=3&t=10226 & http://www.jfree.org/forum/viewtopic.php?f=3&t=22417)

The only solution I could find (but I wished it could be avoided, since I want it to be dynamically done) is to fix a width for each legend, so it should break as I need to. Edit : that even didn't work.

I'm using jFree Chart 0.9.20


For the moment, with a small legend, that's what I have : good

It's fine but when I have my long legends : problem :'(

For that last picture, I logged my legend and break lines are here, but they don't show up with jFree Chart.

share|improve this question
I even tried the fixed width… It didn't work :( –  Shikiryu Apr 30 '12 at 9:12
Cross-posted here. –  trashgod May 3 '12 at 6:52
add comment

2 Answers

Two alternatives to consider: Given an abbreviated legend display string,

  • Use setLegendItemToolTipGenerator() to display the full, unbroken string as a tool tip.

        new StandardXYSeriesLabelGenerator("Legend {0}"));
  • Use addChartMouseListener(), shown here, and forward mouse moved events over the legend to an adjacent text component.

share|improve this answer
There's a related example of the latter here. –  trashgod Apr 28 '12 at 9:42
Not really what I asked for but a nice recommandation for my clients ;-) I'll ask them what they think about the 2nd one. They refused my tooltip idea :( –  Shikiryu Apr 28 '12 at 13:10
Do you have any example for the 1st example? They may be interested afterall. –  Shikiryu Apr 30 '12 at 9:28
I've updated the answer. –  trashgod Apr 30 '12 at 9:42
Oh, if I understand well, in each solution, you consider only 1 legend right ? I have at least 2 each time. I should have said that. I'll update my question. –  Shikiryu Apr 30 '12 at 10:36
show 3 more comments
up vote 1 down vote accepted

Alright, I made it work as my client wanted.

First, you need to make a new kind of Legend, for example named MyLegend (but please, don't name it like that in the real world).

That class needs to extend Legend and implement Serializable, the same way StandardLegend does.

To be honest, I even copied/pasted the whole StandardLegend in MyLegend. Then, you can modify the standard legend to your custom one.

For my needs, I changed :

  • draw() for the height and width calculation of the whole Legend group
  • drawSeriesElements() to split the legend's label and draw every lines one under another.

// Multi line management for Legend
String[] multiline = item.getItem().getLabel().split(System.getProperty("line.separator"));
for(int j = 0; j<multiline.length; j++) {
    RefineryUtilities.drawAlignedString(multiline[j], g2,
        (float) item.getLabelPosition().getX(), (float) item
        .getLabelPosition().getY() + g2.getFontMetrics().getHeight()*j, TextAnchor.CENTER_LEFT);
  • createDrawableLegendItem() to calculate each item width and height. Since, now legends are multiline, each line of one item doesn't have the same width than others. We need to find the longest one to define the item's real width. Same goes for height. Now it's multiline, so it needs to calculate how many lines it got to know the item's real height.

Optionally, you could change drawLegendTitle() to make it multiline too.

When that class is configured as you want to, you need to apply it on your chart.

So, you do as usual :

JFreeChart chart = new JFreeChart(...);
chart.set ... // apply your series and options

MyLegend legend = new MyLegend();
legend.set... // apply your legend options if applicable

That's it.

Result :

Final Result

share|improve this answer
add comment

Your Answer


