001: package dinamica.charts;
002:
003: import java.awt.Color;
004: import dinamica.*;
005: import org.jfree.chart.*;
006: import org.jfree.data.time.*;
007: import org.jfree.chart.plot.*;
008:
009: /**
010: * Chart plugin for TimeSeries charts (dates in X axis) with multiseries support
011: * Last update: 27/06/2006
012: * @author Martin Cordova (dinamica@martincordova.com)
013: */
014: public class DateChart 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 series labels - if any */
023: String series[] = null;
024: String seriesLabels = chartInfo.getString("title-series");
025: if (seriesLabels != null)
026: series = StringUtil.split(seriesLabels, ";");
027: else {
028: series = new String[1];
029: series[0] = "";
030: }
031:
032: String colx = chartInfo.getString("column-x");
033:
034: /* are there multiple series? */
035: String dataCols[] = null;
036: String coly = chartInfo.getString("column-y");
037: if (coly.indexOf(";") > 0)
038: dataCols = StringUtil.split(coly, ";");
039: else {
040: dataCols = new String[1];
041: dataCols[0] = coly;
042: }
043:
044: /* get value y for each serie */
045: TimeSeriesCollection dataset = new TimeSeriesCollection();
046:
047: for (int i = 0; i < dataCols.length; i++) {
048:
049: TimeSeries dataSerie = new TimeSeries(series[i], Day.class);
050:
051: /* navigate the recordset and feed the chart dataset */
052: data.top();
053: while (data.next()) {
054: Double value = new Double(data.getDouble(dataCols[i]));
055: if (value == null)
056: value = new Double(0);
057: dataSerie.add(new Day(data.getDate(colx)), value
058: .doubleValue());
059: }
060: dataset.addSeries(dataSerie);
061:
062: }
063:
064: /* get chart params */
065: String title = (String) chartInfo.getValue("title");
066: String titlex = (String) chartInfo.getValue("title-x");
067: String titley = (String) chartInfo.getValue("title-y");
068:
069: /* if there is more than 1 series then use legends */
070: boolean useLegend = (dataCols.length > 1);
071:
072: /* create a chart */
073:
074: JFreeChart chart = ChartFactory.createTimeSeriesChart(title, // chart title
075: titlex, // domain axis label
076: titley, // range axis label
077: dataset, // data
078: useLegend, // include legend
079: false, // tooltips
080: false // urls
081: );
082:
083: /* set chart decoration */
084: configurePlot(chart.getPlot());
085:
086: //PATCH 2005-07-19 - support for custom default color
087: //for single series charts - line, bar and area only
088: String color = chartInfo.getString("color");
089: if (!useLegend && color != null) {
090: CategoryPlot p = (CategoryPlot) chart.getPlot();
091: p.getRenderer().setSeriesPaint(0, Color.decode(color));
092: }
093:
094: /* return chart */
095: return chart;
096:
097: }
098:
099: /**
100: * Configure chart decorations
101: */
102: public void configurePlot(Plot p) {
103:
104: XYPlot plot = (XYPlot) p;
105:
106: plot.setBackgroundPaint(Color.WHITE);
107: plot.setRangeGridlinePaint(Color.BLACK);
108: plot.setDomainGridlinePaint(Color.BLACK);
109: plot.setRangeGridlinesVisible(true);
110:
111: }
112:
113: }
|