1 package dinamica; 2 3 import java.awt.*; 4 import java.awt.image.*; 5 import java.io.*; 6 import javax.imageio.ImageIO ; 7 import org.jfree.chart.*; 8 9 21 public class ChartOutput extends GenericOutput 22 { 23 24 27 public void print(GenericTransaction t) throws Throwable 28 { 29 30 31 Recordset chartinfo = t.getRecordset("chartinfo"); 32 33 34 String id = chartinfo.getString("data"); 35 Recordset data = (Recordset)getSession().getAttribute(id); 36 if (data==null) 37 throw new Throwable ("Invalid Recordset ID:" + id + " - The session does not contain an attribute with this ID."); 38 39 40 Integer width = (Integer )chartinfo.getValue("width"); 41 Integer height = (Integer )chartinfo.getValue("height"); 42 43 44 String plugin = (String )chartinfo.getValue("chart-plugin"); 45 AbstractChartPlugin obj = (AbstractChartPlugin) Thread.currentThread().getContextClassLoader().loadClass(plugin).newInstance(); 46 JFreeChart chart = obj.getChart(chartinfo, data); 47 48 49 chart.setBackgroundPaint(getGradient()); 50 51 chart.setBorderVisible(true); 53 54 55 BufferedImage img = chart.createBufferedImage(width.intValue(), height.intValue()); 56 ByteArrayOutputStream b = new ByteArrayOutputStream(); 57 58 ImageIO.write(img, "png", b); 60 61 62 byte image[] = b.toByteArray(); 63 getResponse().setContentType("image/png"); 64 getResponse().setContentLength(image.length); 65 OutputStream out = getResponse().getOutputStream(); 66 out.write(image); 67 out.close(); 68 69 if (chartinfo.containsField("session")) 71 { 72 String session = chartinfo.getString("session"); 73 if (session!=null && session.equals("true")) 74 getSession().setAttribute(chartinfo.getString("image-id"), image); 75 } 76 77 } 78 79 84 protected GradientPaint getGradient() 85 { 86 return new GradientPaint(0, 0, Color.WHITE, 1000, 0, Color.GRAY); 87 } 88 } 89 | Popular Tags |