简体   繁体   中英

JFree Chart legend with line break

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", "\␤" 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 : 好

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

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

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.

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 :

最后结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM