1 22 23 package de.laures.cewolf.util; 24 25 import java.awt.Color ; 26 import java.awt.Dimension ; 27 import java.awt.Graphics2D ; 28 import java.awt.Rectangle ; 29 import java.awt.geom.Rectangle2D ; 30 import java.awt.image.BufferedImage ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStreamWriter ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import org.apache.batik.dom.GenericDOMImplementation; 38 import org.apache.batik.svggen.SVGGeneratorContext; 39 import org.apache.batik.svggen.SVGGraphics2D; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 import org.jfree.chart.ChartRenderingInfo; 43 import org.jfree.chart.ChartUtilities; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.block.RectangleConstraint; 46 import org.jfree.chart.entity.StandardEntityCollection; 47 import org.jfree.chart.title.LegendTitle; 48 import org.jfree.ui.RectangleEdge; 49 import org.w3c.dom.DOMImplementation ; 50 import org.w3c.dom.Document ; 51 52 import com.sun.image.codec.jpeg.JPEGCodec; 53 import com.sun.image.codec.jpeg.JPEGEncodeParam; 54 import com.sun.image.codec.jpeg.JPEGImageEncoder; 55 56 import de.laures.cewolf.CewolfException; 57 import de.laures.cewolf.ChartImage; 58 import de.laures.cewolf.ChartRenderingException; 59 import de.laures.cewolf.ConfigurationException; 60 import de.laures.cewolf.WebConstants; 61 62 69 public class Renderer implements WebConstants { 70 71 private final static Log log = LogFactory.getLog(Renderer.class); 72 73 74 private Renderer() { 75 }; 76 77 84 public static RenderedImage render(ChartImage cd, Object chart) throws CewolfException { 85 log.debug("rendering " + cd); 86 switch (cd.getType()) { 87 case ChartImage.IMG_TYPE_CHART : 88 return renderChart(cd, chart); 89 case ChartImage.IMG_TYPE_LEGEND : 90 return renderLegend(cd, chart); 91 default : 92 throw new ConfigurationException(cd.getType() + " is not a supported image type"); 93 } 94 } 95 96 102 private static RenderedImage renderChart(ChartImage cd, Object chart) throws CewolfException { 103 try { 104 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 105 final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 106 final String mimeType = cd.getMimeType(); 107 if (MIME_PNG.equals(mimeType)) { 108 handlePNG(baos, (JFreeChart)chart, cd.getWidth(), cd.getHeight(), info); 109 } else if (MIME_JPEG.equals(mimeType)) { 110 handleJPEG(baos, (JFreeChart)chart, cd.getWidth(), cd.getHeight(), info); 111 } else if (MIME_SVG.equals(mimeType)) { 112 handleSVG(baos, (JFreeChart)chart, cd.getWidth(), cd.getHeight()); 113 } else { 114 throw new RenderingException("Mime type " + mimeType + " is unsupported."); 115 } 116 baos.close(); 117 return new RenderedImage(baos.toByteArray(), mimeType, info); 118 } catch (IOException ioe) { 119 log.error(ioe); 120 throw new ChartRenderingException(ioe.getMessage(),ioe); 121 } 122 } 123 124 135 private static synchronized void handlePNG( 136 ByteArrayOutputStream baos, 137 JFreeChart chart, 138 int width, 139 int height, 140 ChartRenderingInfo info) 141 throws IOException { 142 ChartUtilities.writeChartAsPNG(baos, chart, width, height, info); 143 } 144 145 156 private static synchronized void handleJPEG( 157 ByteArrayOutputStream baos, 158 JFreeChart chart, 159 int width, 160 int height, 161 ChartRenderingInfo info) 162 throws IOException { 163 ChartUtilities.writeChartAsJPEG(baos, chart, width, height, info); 164 } 165 166 176 private static synchronized void handleSVG(ByteArrayOutputStream baos, JFreeChart chart, int width, int height) 177 throws IOException { 178 OutputStreamWriter writer = new OutputStreamWriter (baos, "UTF-8"); 179 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); 180 Document document = domImpl.createDocument("cewolf-svg", "svg", null); 181 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); 182 ctx.setComment("Generated by Cewolf using JFreeChart and Apache Batik SVG Generator"); 183 SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false); 184 svgGenerator.setSVGCanvasSize(new Dimension (width, height)); 185 chart.draw(svgGenerator, new Rectangle2D.Double (0, 0, width, height), null); 186 svgGenerator.stream(writer, false); 187 writer.close(); 188 } 189 190 public static LegendTitle getLegend(JFreeChart chart) 192 { 193 LegendTitle legend = null; 195 List subTitles = chart.getSubtitles(); 196 Iterator iter = subTitles.iterator(); 197 while (iter.hasNext()) 198 { 199 Object o = iter.next(); 200 if (o instanceof LegendTitle) 201 { 202 legend = (LegendTitle) o; 203 break; 204 } 205 } 206 return legend; 207 } 208 209 public static void removeLegend(JFreeChart chart) 211 { 212 List subTitles = chart.getSubtitles(); 213 Iterator iter = subTitles.iterator(); 214 while (iter.hasNext()) 215 { 216 Object o = iter.next(); 217 if (o instanceof LegendTitle) 218 { 219 iter.remove(); 220 break; 221 } 222 } 223 } 224 225 231 private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException { 232 try { 233 JFreeChart chart = (JFreeChart) c; 234 final int width = cd.getWidth(); 235 final int height = cd.getHeight(); 236 LegendTitle legend = getLegend(chart); 237 boolean haslegend = true; 238 239 if (null == legend) { 245 haslegend = false; 246 legend = new LegendTitle(chart.getPlot()); 247 } 248 legend.setPosition(RectangleEdge.BOTTOM); 249 BufferedImage bimage = ImageHelper.createImage(width, height); 250 Graphics2D g = bimage.createGraphics(); 251 g.setColor(Color.white); 252 g.fillRect(0, 0, width, height); 253 legend.arrange(g,new RectangleConstraint(width,height)); 254 legend.draw(g, new Rectangle (width, height)); 255 ByteArrayOutputStream out = new ByteArrayOutputStream (); 256 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 257 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); 258 param.setQuality(1.0f, true); 259 encoder.encode(bimage, param); 260 out.close(); 261 262 if (!haslegend) { 265 removeLegend(chart); 266 } 267 268 return new RenderedImage( 269 out.toByteArray(), 270 "image/jpeg", 271 new ChartRenderingInfo(new StandardEntityCollection())); 272 } catch (IOException ioex) { 273 log.error(ioex); 274 throw new ChartRenderingException(ioex.getMessage(), ioex); 275 } 276 } 277 278 } 279 | Popular Tags |