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