001: package dinamica;
002:
003: import java.awt.*;
004: import java.awt.image.*;
005: import java.io.*;
006: import javax.imageio.ImageIO;
007: import org.jfree.chart.*;
008:
009: /**
010: * Specific Output Plugin for server-side charting using the JFreeChart
011: * LGPL component - every available chart type is also base on a plugin
012: * architecture (AbstracChartPlugin).
013: * <br>
014: * <br>
015: * Creation date: 17/11/2003<br>
016: * Last Update: 17/11/2003<br>
017: * (c) 2003 Martin Cordova<br>
018: * This code is released under the LGPL license<br>
019: * @author Martin Cordova
020: * */
021: public class ChartOutput extends GenericOutput {
022:
023: /* (non-Javadoc)
024: * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
025: */
026: public void print(GenericTransaction t) throws Throwable {
027:
028: //get chart parameters
029: Recordset chartinfo = t.getRecordset("chartinfo");
030:
031: //get chart data
032: String id = chartinfo.getString("data");
033: Recordset data = (Recordset) getSession().getAttribute(id);
034: if (data == null)
035: throw new Throwable(
036: "Invalid Recordset ID:"
037: + id
038: + " - The session does not contain an attribute with this ID.");
039:
040: //general chart params
041: Integer width = (Integer) chartinfo.getValue("width");
042: Integer height = (Integer) chartinfo.getValue("height");
043:
044: //load chart plugin
045: String plugin = (String) chartinfo.getValue("chart-plugin");
046: AbstractChartPlugin obj = (AbstractChartPlugin) Thread
047: .currentThread().getContextClassLoader().loadClass(
048: plugin).newInstance();
049:
050: JFreeChart chart = null;
051: synchronized (data) {
052: chart = obj.getChart(chartinfo, data);
053: }
054:
055: //set gradient
056: chart.setBackgroundPaint(getGradient());
057:
058: //set border and legend params
059: chart.setBorderPaint(Color.LIGHT_GRAY);
060: chart.setBorderVisible(true);
061: if (chart.getLegend() != null) {
062: chart.getLegend().setBorder(0.2, 0.2, 0.2, 0.2);
063: chart.getLegend().setPadding(5, 5, 5, 5);
064: chart.getLegend().setMargin(4, 5, 4, 4);
065: }
066:
067: //render chart in memory
068: BufferedImage img = chart.createBufferedImage(width.intValue(),
069: height.intValue());
070: ByteArrayOutputStream b = new ByteArrayOutputStream(16384);
071:
072: //encode as PNG
073: ImageIO.write(img, "png", b);
074:
075: //send bitmap via servlet output
076: byte image[] = b.toByteArray();
077: getResponse().setContentType("image/png");
078: getResponse().setContentLength(image.length);
079: OutputStream out = getResponse().getOutputStream();
080: out.write(image);
081: out.close();
082:
083: //save image bytes in session attribute if requested
084: if (chartinfo.containsField("session")) {
085: String session = chartinfo.getString("session");
086: if (session != null && session.equals("true"))
087: getSession().setAttribute(
088: chartinfo.getString("image-id"), image);
089: }
090:
091: }
092:
093: /**
094: * Creates a gradient (white->gray) for charts, may
095: * be overrided by subclasses to provide a custom gradient
096: * @return Default gradient for charts
097: */
098: protected GradientPaint getGradient() {
099: return new GradientPaint(0, 0, Color.WHITE, 1000, 0,
100: Color.LIGHT_GRAY);
101: }
102: }
|