private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException {
try {
JFreeChart chart = (JFreeChart) c;
final int width = cd.getWidth();
final int height = cd.getHeight();
LegendTitle legend = getLegend(chart);
boolean haslegend = true;
// with JFreeChart v0.9.20, the only way to get a valid legend,
// is either to retrieve it from the chart or to assign a new
// one to the chart. In the case where the chart has no legend,
// a new one must be assigned, but just for rendering. After, we
// have to reset the legend to null in the chart.
if (null == legend) {
haslegend = false;
legend = new LegendTitle(chart.getPlot());
}
legend.setPosition(RectangleEdge.BOTTOM);
BufferedImage bimage = ImageHelper.createImage(width, height);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
legend.arrange(g,new RectangleConstraint(width,height));
legend.draw(g, new Rectangle(width, height));
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(1.0f, true);
encoder.encode(bimage, param);