KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > chart > DefaultChartFactory


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * DefaultChartFactory.java
28  *
29  * Created on 26 settembre 2004, 18.58
30  *
31  */

32
33 package it.businesslogic.ireport.chart;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.util.*;
36 import java.awt.Image JavaDoc;
37 import org.jfree.data.*;
38 import org.jfree.chart.*;
39 import org.jfree.ui.*;
40 import org.jfree.chart.plot.*;
41 import org.jfree.data.general.DefaultPieDataset;
42 import org.jfree.data.category.DefaultCategoryDataset;
43 import org.jfree.data.xy.XYSeries;
44 import org.jfree.data.xy.DefaultHighLowDataset;
45 import org.jfree.data.xy.OHLCDataset;
46 import java.util.*;
47
48 /**
49  *
50  * @author Administrator
51  */

52 public class DefaultChartFactory extends ChartFactory {
53     
54     static final String JavaDoc PIE3D = "Pie3D";
55     static final String JavaDoc PIE = "Pie";
56     static final String JavaDoc BAR = "Bar";
57     static final String JavaDoc BAR3D = "Bar3D";
58     static final String JavaDoc LINE = "Line";
59     static final String JavaDoc AREA = "Area";
60     static final String JavaDoc CANDLESTICK = "Candlestick";
61     
62     public static Image JavaDoc drawChart(String JavaDoc[] parameters, IReportScriptlet scriptlet)
63     {
64         java.util.Properties JavaDoc props = parseProperties(parameters);
65         int width = getParameterAsInteger("width",props,250);
66         int height = getParameterAsInteger("height",props,100);
67         int quality = getParameterAsInteger("quality",props,1);
68         String JavaDoc chartName = props.getProperty("chartName");
69         
70         boolean showLegend = getParameterAsBoolean("legend",props,false);
71         boolean showTooltips = getParameterAsBoolean("tooltips",props,false);
72         
73         
74          if ( chartName.equals(PIE3D))
75         {
76             Vector labels = getSeries("serie0", props,scriptlet);
77             Vector values = getSeries("serie1", props,scriptlet);
78             
79             DefaultKeyedValues dkv = new DefaultKeyedValues();
80
81             for (int i=0; i<values.size(); ++i)
82             {
83
84                 String JavaDoc key = (i+1)+"";
85                 if (labels != null)
86                 {
87                     key = ""+labels.get(i);
88                 }
89                 dkv.addValue( key, new Double JavaDoc(""+ ((values.get( i ) != null) ? values.get( i ) : "0")) );
90             }
91
92             double depthFactor = getParameterAsDouble("depthFactor",props, 0.2);
93             JFreeChart chart = org.jfree.chart.ChartFactory.createPieChart3D("", new DefaultPieDataset(dkv), showLegend, showTooltips, false);
94             ((PiePlot3D)(chart.getPlot())).setDepthFactor( depthFactor );
95             
96             ((PiePlot3D)(chart.getPlot())).setForegroundAlpha( (float)getParameterAsDouble("foregroundAlpha",props, 0.0));
97             setChartProperties(props, chart);
98             
99             
100             return chart.createBufferedImage(width*quality,height*quality);
101         }
102         else if ( chartName.equals(PIE))
103         {
104             Vector labels = getSeries("serie0", props,scriptlet);
105             Vector values = getSeries("serie1", props,scriptlet);
106             
107             DefaultKeyedValues dkv = new DefaultKeyedValues();
108
109             for (int i=0; i<values.size(); ++i)
110             {
111
112                 String JavaDoc key = (i+1)+"";
113                 if (labels != null)
114                 {
115                     key = ""+labels.get(i);
116                 }
117                 dkv.addValue( key, new Double JavaDoc(""+ ((values.get( i ) != null) ? values.get( i ) : "0") ));
118             }
119
120             JFreeChart chart = org.jfree.chart.ChartFactory.createPieChart("", new DefaultPieDataset(dkv), showLegend, showTooltips, false);
121             setChartProperties(props, chart);
122             return chart.createBufferedImage(width*quality,height*quality);
123         }
124         else if ( chartName.equals(BAR) || chartName.equals(BAR3D))
125         {
126             Vector values = getSeries("serie0", props,scriptlet);
127             Vector theCategories = getSeries("serie1", props,scriptlet);
128             Vector theSeries = getSeries("serie2", props,scriptlet);
129             
130             DefaultCategoryDataset dataset = new DefaultCategoryDataset();
131
132             if (scriptlet == null)
133             {
134                 dataset = getSampleCategoryDataset();
135             }
136             else
137             {
138                     for (int i=0; i<values.size(); ++i)
139                     {
140
141                         String JavaDoc category = (i+1)+"";
142                         if (theCategories != null && theCategories.size() > i)
143                         {
144                             category = ""+theCategories.get(i);
145                         }
146
147                         String JavaDoc theSerie = "";
148                         if (theSeries != null && theSeries.size() > i)
149                         {
150                             theSerie = ""+theSeries.get(i);
151                         }
152
153
154                         dataset.addValue(new Double JavaDoc(""+ ((values.get( i ) != null) ? values.get( i ) : "0")) ,theSerie, category);
155                     }
156             }
157
158             int plotOrientation = getParameterAsInteger("plotOrientation", props, 1);
159             
160             JFreeChart chart = null;
161             
162             if ( chartName.equals(BAR) )
163             {
164                 chart = org.jfree.chart.ChartFactory.createBarChart("",
165                 Misc.nvl(props.getProperty("categoryLabel"),""),
166                 Misc.nvl(props.getProperty("valueLabel"),""),
167                 dataset,
168                 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, // orientation
169
showLegend, // include legend
170
showTooltips, // tooltips?
171
false // URLs?
172
);
173             }
174             else
175             {
176                 chart = org.jfree.chart.ChartFactory.createBarChart3D("",
177                 Misc.nvl(props.getProperty("categoryLabel"),""),
178                 Misc.nvl(props.getProperty("valueLabel"),""),
179                 dataset,
180                 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, // orientation
181
showLegend, // include legend
182
showTooltips, // tooltips?
183
false // URLs?
184
);
185             }
186             setChartProperties(props, chart);
187             return chart.createBufferedImage(width*quality,height*quality);
188         }
189         else if ( chartName.equals(LINE) )
190         {
191             Vector valuesX = getSeries("serie0", props,scriptlet);
192             Vector valuesY = getSeries("serie1", props,scriptlet);
193             Vector theSeries = getSeries("serie2", props,scriptlet);
194             
195             //XYSeries dataset = new XYSeries("");
196
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
197             
198             int plotOrientation = getParameterAsInteger("plotOrientation", props, 1);
199             if (scriptlet == null)
200             {
201                 dataset = getSampleCategoryDataset();
202             }
203             else
204             {
205                     for (int i=0; i<valuesX.size(); ++i)
206                     {
207                         String JavaDoc theSerie = "";
208                         if (theSeries != null && theSeries.size() > i)
209                         {
210                             theSerie = ""+theSeries.get(i);
211                         }
212
213                         dataset.addValue( (Number JavaDoc)new Double JavaDoc(""+ ((valuesX.get( i ) != null) ? valuesX.get( i ) : "0")),
214                                      (Comparable JavaDoc)theSerie,
215                                      (Comparable JavaDoc)new Double JavaDoc(""+ ((valuesY.get( i ) != null) ? valuesY.get( i ) : "0")));
216                     }
217             }
218             
219             JFreeChart chart = null;
220
221             chart = org.jfree.chart.ChartFactory.createLineChart("",
222             Misc.nvl(props.getProperty("categoryLabel"),""),
223             Misc.nvl(props.getProperty("valueLabel"),""),
224             dataset,
225             (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, // orientation
226
showLegend, // include legend
227
showTooltips, // tooltips?
228
false // URLs?
229
);
230
231             setChartProperties(props, chart);
232             return chart.createBufferedImage(width*quality,height*quality);
233         }
234         else if ( chartName.equals(AREA) )
235         {
236             Vector valuesX = getSeries("serie0", props,scriptlet);
237             Vector valuesY = getSeries("serie1", props,scriptlet);
238             Vector theSeries = getSeries("serie2", props,scriptlet);
239             
240             //XYSeries dataset = new XYSeries("");
241
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
242             
243             int plotOrientation = getParameterAsInteger("plotOrientation", props, 1);
244             
245             if (scriptlet == null)
246             {
247                 dataset = getSampleCategoryDataset();
248             }
249             else
250             {
251                 for (int i=0; i<valuesX.size(); ++i)
252                 {
253                     String JavaDoc theSerie = "";
254                     if (theSeries != null && theSeries.size() > i)
255                     {
256                         theSerie = ""+theSeries.get(i);
257                     }
258
259                     dataset.addValue( (Number JavaDoc)new Double JavaDoc(""+ ((valuesX.get( i ) != null) ? valuesX.get( i ) : "0")),
260                                  (Comparable JavaDoc)theSerie,
261                                  (Comparable JavaDoc)new Double JavaDoc(""+ ((valuesY.get( i ) != null) ? valuesY.get( i ) : "0")));
262                 }
263
264             }
265             JFreeChart chart = null;
266
267             chart = org.jfree.chart.ChartFactory.createAreaChart("",
268             Misc.nvl(props.getProperty("categoryLabel"),""),
269             Misc.nvl(props.getProperty("valueLabel"),""),
270             dataset,
271             (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, // orientation
272
showLegend, // include legend
273
showTooltips, // tooltips?
274
false // URLs?
275
);
276
277             setChartProperties(props, chart);
278             CategoryPlot cplot = (CategoryPlot)chart.getPlot();
279             cplot.setForegroundAlpha( (float)getParameterAsDouble("foregroundAlpha",props, 0.0) );
280             
281             return chart.createBufferedImage(width*quality,height*quality);
282         }
283        
284         
285         return null;
286     }
287     
288     protected static void setChartProperties(java.util.Properties JavaDoc props,JFreeChart chart)
289     {
290         String JavaDoc title = props.getProperty("title");
291         if (title!=null && title.length() >0)
292         {
293                chart.setTitle(title);
294                int titlePosition = getParameterAsInteger("titlePosition", props, 1);
295                if (titlePosition == 1) chart.getTitle().setPosition(RectangleEdge.TOP );
296                if (titlePosition == 2) chart.getTitle().setPosition(RectangleEdge.BOTTOM );
297                if (titlePosition == 3) chart.getTitle().setPosition(RectangleEdge.LEFT );
298                if (titlePosition == 4) chart.getTitle().setPosition(RectangleEdge.RIGHT );
299                
300         }
301         
302         title = props.getProperty("subtitle");
303         if (title!=null && title.length() >0)
304         {
305             org.jfree.chart.title.TextTitle subtitle1 = new org.jfree.chart.title.TextTitle(title);
306             chart.addSubtitle(subtitle1);
307         }
308         
309         java.awt.Color JavaDoc bgColor = getParameterAsColor("chartBackground",props, java.awt.Color.WHITE);
310         if (bgColor!=null) chart.setBackgroundPaint( bgColor );
311         
312         bgColor = getParameterAsColor("plotBackground",props, java.awt.Color.WHITE);
313         if (bgColor!=null) chart.getPlot().setBackgroundPaint( bgColor );
314         
315     }
316     
317     
318     
319     protected static java.util.Properties JavaDoc parseProperties(String JavaDoc[] properties)
320     {
321         java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
322         
323         for (int i=0; i<properties.length; ++i)
324         {
325             String JavaDoc key = properties[i].substring(0, properties[i].indexOf("="));
326             String JavaDoc val = properties[i].substring(key.length()+1);
327             props.setProperty(key,val);
328         }
329         return props;
330     }
331     
332     protected static DefaultCategoryDataset getSampleCategoryDataset()
333     {
334         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
335         dataset.addValue(1.0, "Series 1", "Type 1");
336         dataset.addValue(4.0, "Series 1", "Type 2");
337         dataset.addValue(3.0, "Series 1", "Type 3");
338         dataset.addValue(5.0, "Series 1", "Type 4");
339         dataset.addValue(5.0, "Series 1", "Type 5");
340         dataset.addValue(7.0, "Series 1", "Type 6");
341         dataset.addValue(7.0, "Series 1", "Type 7");
342         dataset.addValue(8.0, "Series 1", "Type 8");
343         dataset.addValue(5.0, "Series 2", "Type 1");
344         dataset.addValue(7.0, "Series 2", "Type 2");
345         dataset.addValue(6.0, "Series 2", "Type 3");
346         dataset.addValue(8.0, "Series 2", "Type 4");
347         dataset.addValue(4.0, "Series 2", "Type 5");
348         dataset.addValue(4.0, "Series 2", "Type 6");
349         dataset.addValue(2.0, "Series 2", "Type 7");
350         dataset.addValue(1.0, "Series 2", "Type 8");
351         dataset.addValue(4.0, "Series 3", "Type 1");
352         dataset.addValue(3.0, "Series 3", "Type 2");
353         dataset.addValue(2.0, "Series 3", "Type 3");
354         dataset.addValue(3.0, "Series 3", "Type 4");
355         dataset.addValue(6.0, "Series 3", "Type 5");
356         dataset.addValue(3.0, "Series 3", "Type 6");
357         dataset.addValue(4.0, "Series 3", "Type 7");
358         dataset.addValue(3.0, "Series 3", "Type 8");
359         return dataset;
360     }
361 }
362
Popular Tags