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 :
