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: import org.jfree.chart.renderer.category.LineAndShapeRenderer;
009:
010: /**
011: * Chart plugin for Line charts with multiseries support
012: * Last update: 18/11/2003
013: * @author Martin Cordova (dinamica@martincordova.com)
014: */
015: public class LineChart 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 date format for x-axis if required */
024: String dateFormat = (String) chartInfo.getValue("dateformat");
025:
026: /* create a chart dataset using the data contained in the Recordset "data" */
027: DefaultCategoryDataset chartdata = new DefaultCategoryDataset();
028:
029: /* get series labels - if any */
030: String series[] = null;
031: String seriesLabels = chartInfo.getString("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.doubleValue(), series[i],
071: label);
072:
073: }
074:
075: }
076:
077: /* get chart params */
078: String title = (String) chartInfo.getValue("title");
079: String titlex = (String) chartInfo.getValue("title-x");
080: String titley = (String) chartInfo.getValue("title-y");
081:
082: /* if there is more than 1 series then use legends */
083: boolean useLegend = (dataCols.length > 1);
084:
085: /* create a chart */
086: JFreeChart chart = ChartFactory.createLineChart(title, // chart title
087: titlex, // domain axis label
088: titley, // range axis label
089: chartdata, // data
090: PlotOrientation.VERTICAL, // orientation
091: useLegend, // include legend
092: false, // tooltips
093: false // urls
094: );
095:
096: /* set chart decoration */
097: configurePlot(chart.getPlot());
098:
099: //PATCH 2005-07-19 - support for custom default color
100: //for single series charts - line, bar and area only
101: String color = chartInfo.getString("color");
102: if (!useLegend && color != null) {
103: CategoryPlot p = (CategoryPlot) chart.getPlot();
104: p.getRenderer().setSeriesPaint(0, Color.decode(color));
105: }
106:
107: /* return chart */
108: return chart;
109:
110: }
111:
112: /**
113: * Configure chart decorations
114: */
115: public void configurePlot(Plot p) {
116:
117: CategoryPlot plot = (CategoryPlot) p;
118:
119: plot.setBackgroundPaint(Color.WHITE);
120: plot.setRangeGridlinePaint(Color.BLACK);
121: plot.setDomainGridlinePaint(Color.BLACK);
122: plot.setDomainGridlinesVisible(true);
123: plot.setRangeGridlinesVisible(true);
124:
125: LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot
126: .getRenderer();
127: lineandshaperenderer.setBaseShapesVisible(true);
128: lineandshaperenderer.setBaseShapesFilled(true);
129:
130: }
131:
132: }
|