KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > ChartOutput


1 package dinamica;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.io.*;
6 import javax.imageio.ImageIO JavaDoc;
7 import org.jfree.chart.*;
8
9 /**
10  * Specific Output Plugin for server-side charting using the JFreeChart
11  * LGPL component - every available chart type is also base on a plugin
12  * architecture (AbstracChartPlugin).
13  * <br>
14  * <br>
15  * Creation date: 17/11/2003<br>
16  * Last Update: 17/11/2003<br>
17  * (c) 2003 Martin Cordova<br>
18  * This code is released under the LGPL license<br>
19  * @author Martin Cordova
20  * */

21 public class ChartOutput extends GenericOutput
22 {
23
24     /* (non-Javadoc)
25      * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
26      */

27     public void print(GenericTransaction t) throws Throwable JavaDoc
28     {
29
30         /* get chart parameters */
31         Recordset chartinfo = t.getRecordset("chartinfo");
32
33         /* get chart data */
34         String JavaDoc id = chartinfo.getString("data");
35         Recordset data = (Recordset)getSession().getAttribute(id);
36         if (data==null)
37             throw new Throwable JavaDoc("Invalid Recordset ID:" + id + " - The session does not contain an attribute with this ID.");
38
39         /* general chart params */
40         Integer JavaDoc width = (Integer JavaDoc)chartinfo.getValue("width");
41         Integer JavaDoc height = (Integer JavaDoc)chartinfo.getValue("height");
42         
43         /* load chart plugin */
44         String JavaDoc plugin = (String JavaDoc)chartinfo.getValue("chart-plugin");
45         AbstractChartPlugin obj = (AbstractChartPlugin) Thread.currentThread().getContextClassLoader().loadClass(plugin).newInstance();
46         JFreeChart chart = obj.getChart(chartinfo, data);
47                     
48         /* set gradient */
49         chart.setBackgroundPaint(getGradient());
50         
51         //set border
52
chart.setBorderVisible(true);
53                 
54         /* render chart in memory */
55         BufferedImage img = chart.createBufferedImage(width.intValue(), height.intValue());
56         ByteArrayOutputStream b = new ByteArrayOutputStream();
57         
58         //encode as PNG
59
ImageIO.write(img, "png", b);
60
61         /* send bitmap via servlet output */
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         //save image bytes in session attribute if requested
70
if (chartinfo.containsField("session"))
71         {
72             String JavaDoc session = chartinfo.getString("session");
73             if (session!=null && session.equals("true"))
74                 getSession().setAttribute(chartinfo.getString("image-id"), image);
75         }
76                 
77     }
78     
79     /**
80      * Creates a gradient (white->gray) for charts, may
81      * be overrided by subclasses to provide a custom gradient
82      * @return Default gradient for charts
83      */

84     protected GradientPaint getGradient()
85     {
86         return new GradientPaint(0, 0, Color.WHITE, 1000, 0, Color.GRAY);
87     }
88 }
89
Popular Tags