001: /* ============================================================================
002: * GNU Lesser General Public License
003: * ============================================================================
004: *
005: * Copyright (C) 2001-2007 JasperSoft Corporation http://www.jaspersoft.com
006: *
007: * This class is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This class is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this class; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * JasperSoft Corporation
022: * 303 Second Street, Suite 450 North
023: * San Francisco, CA 94107
024: * http://www.jaspersoft.com
025: *
026: *
027: *
028: * DefaultChartFactory.java
029: *
030: * Created on 26 settembre 2004, 18.58
031: *
032: */
033:
034: package it.businesslogic.ireport.chart;
035:
036: import it.businesslogic.ireport.IReportScriptlet;
037: import java.awt.Image;
038: import java.util.Vector;
039: import org.jfree.chart.JFreeChart;
040: import org.jfree.chart.plot.CategoryPlot;
041: import org.jfree.chart.plot.PiePlot3D;
042: import org.jfree.chart.plot.PlotOrientation;
043: import org.jfree.data.DefaultKeyedValues;
044: import org.jfree.data.general.DefaultPieDataset;
045: import org.jfree.data.category.DefaultCategoryDataset;
046: import org.jfree.ui.RectangleEdge;
047:
048: /**
049: *
050: * @author Administrator
051: */
052: public class DefaultChartFactory extends ChartFactory {
053:
054: static final String PIE3D = "Pie3D";
055: static final String PIE = "Pie";
056: static final String BAR = "Bar";
057: static final String BAR3D = "Bar3D";
058: static final String LINE = "Line";
059: static final String AREA = "Area";
060: static final String CANDLESTICK = "Candlestick";
061:
062: public static Image drawChart(String[] parameters,
063: IReportScriptlet scriptlet) {
064: java.util.Properties props = parseProperties(parameters);
065: int width = getParameterAsInteger("width", props, 250);
066: int height = getParameterAsInteger("height", props, 100);
067: int quality = getParameterAsInteger("quality", props, 1);
068: String chartName = props.getProperty("chartName");
069:
070: boolean showLegend = getParameterAsBoolean("legend", props,
071: false);
072: boolean showTooltips = getParameterAsBoolean("tooltips", props,
073: false);
074:
075: if (chartName.equals(PIE3D)) {
076: Vector labels = getSeries("serie0", props, scriptlet);
077: Vector values = getSeries("serie1", props, scriptlet);
078:
079: DefaultKeyedValues dkv = new DefaultKeyedValues();
080:
081: for (int i = 0; i < values.size(); ++i) {
082:
083: String key = (i + 1) + "";
084: if (labels != null) {
085: key = "" + labels.get(i);
086: }
087: dkv.addValue(key, new Double(""
088: + ((values.get(i) != null) ? values.get(i)
089: : "0")));
090: }
091:
092: double depthFactor = getParameterAsDouble("depthFactor",
093: props, 0.2);
094: JFreeChart chart = org.jfree.chart.ChartFactory
095: .createPieChart3D("", new DefaultPieDataset(dkv),
096: showLegend, showTooltips, false);
097: ((PiePlot3D) (chart.getPlot())).setDepthFactor(depthFactor);
098:
099: ((PiePlot3D) (chart.getPlot()))
100: .setForegroundAlpha((float) getParameterAsDouble(
101: "foregroundAlpha", props, 0.0));
102: setChartProperties(props, chart);
103:
104: return chart.createBufferedImage(width * quality, height
105: * quality);
106: } else if (chartName.equals(PIE)) {
107: Vector labels = getSeries("serie0", props, scriptlet);
108: Vector values = getSeries("serie1", props, scriptlet);
109:
110: DefaultKeyedValues dkv = new DefaultKeyedValues();
111:
112: for (int i = 0; i < values.size(); ++i) {
113:
114: String key = (i + 1) + "";
115: if (labels != null) {
116: key = "" + labels.get(i);
117: }
118: dkv.addValue(key, new Double(""
119: + ((values.get(i) != null) ? values.get(i)
120: : "0")));
121: }
122:
123: JFreeChart chart = org.jfree.chart.ChartFactory
124: .createPieChart("", new DefaultPieDataset(dkv),
125: showLegend, showTooltips, false);
126: setChartProperties(props, chart);
127: return chart.createBufferedImage(width * quality, height
128: * quality);
129: } else if (chartName.equals(BAR) || chartName.equals(BAR3D)) {
130: Vector values = getSeries("serie0", props, scriptlet);
131: Vector theCategories = getSeries("serie1", props, scriptlet);
132: Vector theSeries = getSeries("serie2", props, scriptlet);
133:
134: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
135:
136: if (scriptlet == null) {
137: dataset = getSampleCategoryDataset();
138: } else {
139: for (int i = 0; i < values.size(); ++i) {
140:
141: String category = (i + 1) + "";
142: if (theCategories != null
143: && theCategories.size() > i) {
144: category = "" + theCategories.get(i);
145: }
146:
147: String theSerie = "";
148: if (theSeries != null && theSeries.size() > i) {
149: theSerie = "" + theSeries.get(i);
150: }
151:
152: dataset.addValue(new Double(""
153: + ((values.get(i) != null) ? values.get(i)
154: : "0")), theSerie, category);
155: }
156: }
157:
158: int plotOrientation = getParameterAsInteger(
159: "plotOrientation", props, 1);
160:
161: JFreeChart chart = null;
162:
163: if (chartName.equals(BAR)) {
164: chart = org.jfree.chart.ChartFactory
165: .createBarChart(
166: "",
167: nvl(props.getProperty("categoryLabel"),
168: ""),
169: nvl(props.getProperty("valueLabel"), ""),
170: dataset,
171: (plotOrientation == 1) ? PlotOrientation.HORIZONTAL
172: : PlotOrientation.VERTICAL, // orientation
173: showLegend, // include legend
174: showTooltips, // tooltips?
175: false // URLs?
176: );
177: } else {
178: chart = org.jfree.chart.ChartFactory
179: .createBarChart3D(
180: "",
181: nvl(props.getProperty("categoryLabel"),
182: ""),
183: nvl(props.getProperty("valueLabel"), ""),
184: dataset,
185: (plotOrientation == 1) ? PlotOrientation.HORIZONTAL
186: : PlotOrientation.VERTICAL, // orientation
187: showLegend, // include legend
188: showTooltips, // tooltips?
189: false // URLs?
190: );
191: }
192: setChartProperties(props, chart);
193: return chart.createBufferedImage(width * quality, height
194: * quality);
195: } else if (chartName.equals(LINE)) {
196: Vector valuesX = getSeries("serie0", props, scriptlet);
197: Vector valuesY = getSeries("serie1", props, scriptlet);
198: Vector theSeries = getSeries("serie2", props, scriptlet);
199:
200: //XYSeries dataset = new XYSeries("");
201: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
202:
203: int plotOrientation = getParameterAsInteger(
204: "plotOrientation", props, 1);
205: if (scriptlet == null) {
206: dataset = getSampleCategoryDataset();
207: } else {
208: for (int i = 0; i < valuesX.size(); ++i) {
209: String theSerie = "";
210: if (theSeries != null && theSeries.size() > i) {
211: theSerie = "" + theSeries.get(i);
212: }
213:
214: dataset
215: .addValue(
216: (Number) new Double(
217: ""
218: + ((valuesX.get(i) != null) ? valuesX
219: .get(i)
220: : "0")),
221: (Comparable) theSerie,
222: (Comparable) new Double(
223: ""
224: + ((valuesY.get(i) != null) ? valuesY
225: .get(i)
226: : "0")));
227: }
228: }
229:
230: JFreeChart chart = null;
231:
232: chart = org.jfree.chart.ChartFactory.createLineChart("",
233: nvl(props.getProperty("categoryLabel"), ""), nvl(
234: props.getProperty("valueLabel"), ""),
235: dataset,
236: (plotOrientation == 1) ? PlotOrientation.HORIZONTAL
237: : PlotOrientation.VERTICAL, // orientation
238: showLegend, // include legend
239: showTooltips, // tooltips?
240: false // URLs?
241: );
242:
243: setChartProperties(props, chart);
244: return chart.createBufferedImage(width * quality, height
245: * quality);
246: } else if (chartName.equals(AREA)) {
247: Vector valuesX = getSeries("serie0", props, scriptlet);
248: Vector valuesY = getSeries("serie1", props, scriptlet);
249: Vector theSeries = getSeries("serie2", props, scriptlet);
250:
251: //XYSeries dataset = new XYSeries("");
252: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
253:
254: int plotOrientation = getParameterAsInteger(
255: "plotOrientation", props, 1);
256:
257: if (scriptlet == null) {
258: dataset = getSampleCategoryDataset();
259: } else {
260: for (int i = 0; i < valuesX.size(); ++i) {
261: String theSerie = "";
262: if (theSeries != null && theSeries.size() > i) {
263: theSerie = "" + theSeries.get(i);
264: }
265:
266: dataset
267: .addValue(
268: (Number) new Double(
269: ""
270: + ((valuesX.get(i) != null) ? valuesX
271: .get(i)
272: : "0")),
273: (Comparable) theSerie,
274: (Comparable) new Double(
275: ""
276: + ((valuesY.get(i) != null) ? valuesY
277: .get(i)
278: : "0")));
279: }
280:
281: }
282: JFreeChart chart = null;
283:
284: chart = org.jfree.chart.ChartFactory.createAreaChart("",
285: nvl(props.getProperty("categoryLabel"), ""), nvl(
286: props.getProperty("valueLabel"), ""),
287: dataset,
288: (plotOrientation == 1) ? PlotOrientation.HORIZONTAL
289: : PlotOrientation.VERTICAL, // orientation
290: showLegend, // include legend
291: showTooltips, // tooltips?
292: false // URLs?
293: );
294:
295: setChartProperties(props, chart);
296: CategoryPlot cplot = (CategoryPlot) chart.getPlot();
297: cplot.setForegroundAlpha((float) getParameterAsDouble(
298: "foregroundAlpha", props, 0.0));
299:
300: return chart.createBufferedImage(width * quality, height
301: * quality);
302: }
303:
304: return null;
305: }
306:
307: protected static void setChartProperties(
308: java.util.Properties props, JFreeChart chart) {
309: String title = props.getProperty("title");
310: if (title != null && title.length() > 0) {
311: chart.setTitle(title);
312: int titlePosition = getParameterAsInteger("titlePosition",
313: props, 1);
314: if (titlePosition == 1)
315: chart.getTitle().setPosition(RectangleEdge.TOP);
316: if (titlePosition == 2)
317: chart.getTitle().setPosition(RectangleEdge.BOTTOM);
318: if (titlePosition == 3)
319: chart.getTitle().setPosition(RectangleEdge.LEFT);
320: if (titlePosition == 4)
321: chart.getTitle().setPosition(RectangleEdge.RIGHT);
322:
323: }
324:
325: title = props.getProperty("subtitle");
326: if (title != null && title.length() > 0) {
327: org.jfree.chart.title.TextTitle subtitle1 = new org.jfree.chart.title.TextTitle(
328: title);
329: chart.addSubtitle(subtitle1);
330: }
331:
332: java.awt.Color bgColor = getParameterAsColor("chartBackground",
333: props, java.awt.Color.WHITE);
334: if (bgColor != null)
335: chart.setBackgroundPaint(bgColor);
336:
337: bgColor = getParameterAsColor("plotBackground", props,
338: java.awt.Color.WHITE);
339: if (bgColor != null)
340: chart.getPlot().setBackgroundPaint(bgColor);
341:
342: }
343:
344: protected static java.util.Properties parseProperties(
345: String[] properties) {
346: java.util.Properties props = new java.util.Properties();
347:
348: for (int i = 0; i < properties.length; ++i) {
349: String key = properties[i].substring(0, properties[i]
350: .indexOf("="));
351: String val = properties[i].substring(key.length() + 1);
352: props.setProperty(key, val);
353: }
354: return props;
355: }
356:
357: protected static DefaultCategoryDataset getSampleCategoryDataset() {
358: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
359: dataset.addValue(1.0, "Series 1", "Type 1");
360: dataset.addValue(4.0, "Series 1", "Type 2");
361: dataset.addValue(3.0, "Series 1", "Type 3");
362: dataset.addValue(5.0, "Series 1", "Type 4");
363: dataset.addValue(5.0, "Series 1", "Type 5");
364: dataset.addValue(7.0, "Series 1", "Type 6");
365: dataset.addValue(7.0, "Series 1", "Type 7");
366: dataset.addValue(8.0, "Series 1", "Type 8");
367: dataset.addValue(5.0, "Series 2", "Type 1");
368: dataset.addValue(7.0, "Series 2", "Type 2");
369: dataset.addValue(6.0, "Series 2", "Type 3");
370: dataset.addValue(8.0, "Series 2", "Type 4");
371: dataset.addValue(4.0, "Series 2", "Type 5");
372: dataset.addValue(4.0, "Series 2", "Type 6");
373: dataset.addValue(2.0, "Series 2", "Type 7");
374: dataset.addValue(1.0, "Series 2", "Type 8");
375: dataset.addValue(4.0, "Series 3", "Type 1");
376: dataset.addValue(3.0, "Series 3", "Type 2");
377: dataset.addValue(2.0, "Series 3", "Type 3");
378: dataset.addValue(3.0, "Series 3", "Type 4");
379: dataset.addValue(6.0, "Series 3", "Type 5");
380: dataset.addValue(3.0, "Series 3", "Type 6");
381: dataset.addValue(4.0, "Series 3", "Type 7");
382: dataset.addValue(3.0, "Series 3", "Type 8");
383: return dataset;
384: }
385:
386: public static String nvl(Object obj, String def) {
387: return (obj == null) ? def : obj.toString();
388: }
389: }
|