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.BarRenderer3D;
009:
010: /**
011: * Chart plugin for Bar3D charts with multiseries support
012: * Last update: 18/11/2003
013: * @author Martin Cordova (dinamica@martincordova.com)
014: */
015: public class VerticalBarChart3D 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 = (String) chartInfo
032: .getValue("title-series");
033: if (seriesLabels != null)
034: series = StringUtil.split(seriesLabels, ";");
035: else {
036: series = new String[1];
037: series[0] = "";
038: }
039:
040: /* are there multiple series? */
041: String dataCols[] = null;
042: String coly = (String) chartInfo.getValue("column-y");
043: if (coly.indexOf(";") > 0)
044: dataCols = StringUtil.split(coly, ";");
045: else {
046: dataCols = new String[1];
047: dataCols[0] = coly;
048: }
049:
050: /* navigate the recordset and feed the chart dataset */
051: data.top();
052: while (data.next()) {
053:
054: /* get label x */
055: String colx = (String) chartInfo.getValue("column-x");
056: RecordsetField f = data.getField(colx);
057: String label = null;
058: if (f.getType() == java.sql.Types.DATE)
059: label = StringUtil.formatDate((java.util.Date) data
060: .getValue(colx), dateFormat);
061: else
062: label = String.valueOf(data.getValue(colx));
063:
064: /* get value y for each serie */
065: for (int i = 0; i < dataCols.length; i++) {
066: Double value = new Double(String.valueOf(data
067: .getValue(dataCols[i])));
068: if (value == null)
069: value = new Double(0);
070:
071: chartdata.addValue(value, series[i], 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.createBarChart3D(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.setForegroundAlpha(0.8F);
120: plot.setBackgroundPaint(Color.WHITE);
121: plot.setRangeGridlinePaint(Color.BLACK);
122: plot.setDomainGridlinePaint(Color.BLACK);
123: plot.setDomainGridlinesVisible(true);
124: plot.setRangeGridlinesVisible(true);
125:
126: BarRenderer3D barrenderer3d = (BarRenderer3D) plot
127: .getRenderer();
128: barrenderer3d.setDrawBarOutline(false);
129:
130: }
131:
132: }
|