001: package dinamica.charts;
002:
003: import java.awt.Color;
004: import dinamica.*;
005: import org.jfree.chart.*;
006: import org.jfree.data.category.*;
007: import org.jfree.chart.plot.*;
008:
009: /**
010: * Chart plugin for Area 2D charts with multiseries support
011: * Last update: 18/11/2003
012: * @author Martin Cordova (dinamica@martincordova.com)
013: */
014: public class AreaChart extends AbstractChartPlugin {
015:
016: /* (non-Javadoc)
017: * @see dinamica.AbstractChartPlugin#getChart(dinamica.Recordset, dinamica.Recordset)
018: */
019: public JFreeChart getChart(Recordset chartInfo, Recordset data)
020: throws Throwable {
021:
022: /* get date format for x-axis if required */
023: String dateFormat = (String) chartInfo.getValue("dateformat");
024:
025: /* create a chart dataset using the data contained in the Recordset "data" */
026: DefaultCategoryDataset chartdata = new DefaultCategoryDataset();
027:
028: /* get series labels - if any */
029: String series[] = null;
030: String seriesLabels = (String) chartInfo
031: .getValue("title-series");
032: if (seriesLabels != null)
033: series = StringUtil.split(seriesLabels, ";");
034: else {
035: series = new String[1];
036: series[0] = "";
037: }
038:
039: /* are there multiple series? */
040: String dataCols[] = null;
041: String coly = (String) chartInfo.getValue("column-y");
042: if (coly.indexOf(";") > 0)
043: dataCols = StringUtil.split(coly, ";");
044: else {
045: dataCols = new String[1];
046: dataCols[0] = coly;
047: }
048:
049: /* navigate the recordset and feed the chart dataset */
050: data.top();
051: while (data.next()) {
052:
053: /* get label x */
054: String colx = (String) chartInfo.getValue("column-x");
055: RecordsetField f = data.getField(colx);
056: String label = null;
057: if (f.getType() == java.sql.Types.DATE)
058: label = StringUtil.formatDate((java.util.Date) data
059: .getValue(colx), dateFormat);
060: else
061: label = String.valueOf(data.getValue(colx));
062:
063: /* get value y for each serie */
064: for (int i = 0; i < dataCols.length; i++) {
065: Double value = new Double(String.valueOf(data
066: .getValue(dataCols[i])));
067: if (value == null)
068: value = new Double(0);
069:
070: chartdata.addValue(value, series[i], label);
071:
072: }
073:
074: }
075:
076: /* get chart params */
077: String title = (String) chartInfo.getValue("title");
078: String titlex = (String) chartInfo.getValue("title-x");
079: String titley = (String) chartInfo.getValue("title-y");
080:
081: /* if there is more than 1 series then use legends */
082: boolean useLegend = (dataCols.length > 1);
083:
084: /* create a chart */
085: JFreeChart chart = ChartFactory.createAreaChart(title, // chart title
086: titlex, // domain axis label
087: titley, // range axis label
088: chartdata, // data
089: PlotOrientation.VERTICAL, // orientation
090: useLegend, // include legend
091: false, // tooltips
092: false // urls
093: );
094:
095: /* set chart decoration */
096: configurePlot(chart.getPlot());
097:
098: //PATCH 2005-07-19 - support for custom default color
099: //for single series charts - line, bar and area only
100: String color = chartInfo.getString("color");
101: if (!useLegend && color != null) {
102: CategoryPlot p = (CategoryPlot) chart.getPlot();
103: p.getRenderer().setSeriesPaint(0, Color.decode(color));
104: }
105:
106: /* return chart */
107: return chart;
108:
109: }
110:
111: /**
112: * Configure chart decorations
113: */
114: public void configurePlot(Plot p) {
115:
116: CategoryPlot plot = (CategoryPlot) p;
117: plot.setBackgroundPaint(Color.WHITE);
118: plot.setRangeGridlinePaint(Color.BLACK);
119: plot.setDomainGridlinePaint(Color.BLACK);
120: plot.setDomainGridlinesVisible(true);
121: plot.setRangeGridlinesVisible(true);
122: plot.setForegroundAlpha(0.6F);
123:
124: }
125:
126: }
|