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 Line charts 3D with multiseries support
011: * Last update: 24/july/2005
012: * @author Martin Cordova (dinamica@martincordova.com)
013: */
014: public class LineChart3D 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 = chartInfo.getString("title-series");
031: if (seriesLabels != null)
032: series = StringUtil.split(seriesLabels, ";");
033: else {
034: series = new String[1];
035: series[0] = "";
036: }
037:
038: /* are there multiple series? */
039: String dataCols[] = null;
040: String coly = (String) chartInfo.getValue("column-y");
041: if (coly.indexOf(";") > 0)
042: dataCols = StringUtil.split(coly, ";");
043: else {
044: dataCols = new String[1];
045: dataCols[0] = coly;
046: }
047:
048: /* navigate the recordset and feed the chart dataset */
049: data.top();
050: while (data.next()) {
051:
052: /* get label x */
053: String colx = (String) chartInfo.getValue("column-x");
054: RecordsetField f = data.getField(colx);
055: String label = null;
056: if (f.getType() == java.sql.Types.DATE)
057: label = StringUtil.formatDate((java.util.Date) data
058: .getValue(colx), dateFormat);
059: else
060: label = String.valueOf(data.getValue(colx));
061:
062: /* get value y for each serie */
063: for (int i = 0; i < dataCols.length; i++) {
064: Double value = new Double(String.valueOf(data
065: .getValue(dataCols[i])));
066: if (value == null)
067: value = new Double(0);
068:
069: chartdata.addValue(value.doubleValue(), series[i],
070: 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.createLineChart3D(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:
118: plot.setForegroundAlpha(0.5F);
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: }
126:
127: }
|