1

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


EDIT

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.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75

2 Answers2

3

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

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

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • There's a related example of the latter [here](http://stackoverflow.com/a/8219445/230513). – trashgod Apr 28 '12 at 09: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 09:28
  • 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
  • The mouse can only hover over one legend item at a time, and each is handled in turn. Version 1.0.14 is current. – trashgod Apr 30 '12 at 11:31
  • But, is there really a mouse listener using JFree Chart with j2ee? I mean it only generates a picture… doesn't it? – Shikiryu Apr 30 '12 at 12:10
  • Same with a tool tip. On a web page, I'd use the `org.jfree.chart.imagemap` support or link to a [tag:java-web-start] application. – trashgod Apr 30 '12 at 15:19
1

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
chart.setLegend(legend);

That's it.

Result :

Final Result

Shikiryu
  • 10,180
  • 8
  • 49
  • 75