KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > charts > fill > JRFillTimeSeriesDataset


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.charts.fill;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sf.jasperreports.charts.JRTimeSeries;
36 import net.sf.jasperreports.charts.JRTimeSeriesDataset;
37 import net.sf.jasperreports.charts.util.TimeSeriesLabelGenerator;
38 import net.sf.jasperreports.engine.JRChartDataset;
39 import net.sf.jasperreports.engine.JRExpressionCollector;
40 import net.sf.jasperreports.engine.JRRuntimeException;
41 import net.sf.jasperreports.engine.design.JRVerifier;
42 import net.sf.jasperreports.engine.fill.JRCalculator;
43 import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
44 import net.sf.jasperreports.engine.fill.JRFillChartDataset;
45 import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
46
47 import org.jfree.data.general.Dataset;
48 import org.jfree.data.time.RegularTimePeriod;
49 import org.jfree.data.time.TimeSeries;
50 import org.jfree.data.time.TimeSeriesCollection;
51
52 /**
53  * @author Flavius Sana (flavius_sana@users.sourceforge.net)
54  * @version $Id: JRFillTimeSeriesDataset.java 1531 2006-12-21 19:38:30 +0200 (Thu, 21 Dec 2006) teodord $
55  */

56 public class JRFillTimeSeriesDataset extends JRFillChartDataset implements JRTimeSeriesDataset
57 {
58
59     /**
60      *
61      */

62     protected JRFillTimeSeries[] timeSeries = null;
63     
64     private List JavaDoc seriesNames = null;
65     private Map JavaDoc seriesMap = null;
66     private Map JavaDoc labelsMap = null;
67     private Map JavaDoc itemHyperlinks;
68     
69     
70     public JRFillTimeSeriesDataset(
71         JRTimeSeriesDataset timeSeriesDataset,
72         JRFillObjectFactory factory
73         )
74     {
75         super( timeSeriesDataset, factory );
76         
77         JRTimeSeries[] srcTimeSeries = timeSeriesDataset.getSeries();
78         if( srcTimeSeries != null && srcTimeSeries.length > 0)
79         {
80             timeSeries = new JRFillTimeSeries[srcTimeSeries.length];
81             for (int i = 0; i < timeSeries.length; i++)
82             {
83                 timeSeries[i] = (JRFillTimeSeries)factory.getTimeSeries(srcTimeSeries[i]);
84             }
85         }
86     }
87     
88     public JRTimeSeries[] getSeries()
89     {
90         return timeSeries;
91     }
92     
93     protected void customInitialize()
94     {
95         seriesNames = null;
96         seriesMap = null;
97         labelsMap = null;
98         itemHyperlinks = null;
99     }
100     
101     protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException
102     {
103         if(timeSeries != null && timeSeries.length > 0)
104         {
105             for (int i = 0; i < timeSeries.length; i++)
106             {
107                 timeSeries[i].evaluate( calculator );
108             }
109         }
110     }
111     
112     
113     protected void customIncrement()
114     {
115         if (timeSeries != null && timeSeries.length > 0)
116         {
117             if (seriesNames == null)
118             {
119                 seriesNames = new ArrayList JavaDoc();
120                 seriesMap = new HashMap JavaDoc();
121                 labelsMap = new HashMap JavaDoc();
122                 itemHyperlinks = new HashMap JavaDoc();
123             }
124
125             for (int i = 0; i < timeSeries.length; i++)
126             {
127                 JRFillTimeSeries crtTimeSeries = timeSeries[i];
128                 
129                 Comparable JavaDoc seriesName = crtTimeSeries.getSeries();
130                 if (seriesName == null)
131                 {
132                     throw new JRRuntimeException("Time series name is null.");
133                 }
134
135                 TimeSeries series = (TimeSeries)seriesMap.get(seriesName);
136                 if(series == null)
137                 {
138                     series = new TimeSeries(seriesName.toString(), getTimePeriod());
139                     seriesNames.add(seriesName);
140                     seriesMap.put(seriesName, series);
141                 }
142                 
143                 RegularTimePeriod tp =
144                     RegularTimePeriod.createInstance(
145                         getTimePeriod(),
146                         crtTimeSeries.getTimePeriod(),
147                         getTimeZone()
148                         );
149
150                 series.addOrUpdate(tp, crtTimeSeries.getValue());
151
152                 if (crtTimeSeries.getLabelExpression() != null)
153                 {
154                     Map JavaDoc seriesLabels = (Map JavaDoc)labelsMap.get(seriesName);
155                     if (seriesLabels == null)
156                     {
157                         seriesLabels = new HashMap JavaDoc();
158                         labelsMap.put(seriesName, seriesLabels);
159                     }
160                     
161                     seriesLabels.put(tp, crtTimeSeries.getLabel());
162                 }
163                 
164                 if (crtTimeSeries.hasItemHyperlink())
165                 {
166                     Map JavaDoc seriesLinks = (Map JavaDoc) itemHyperlinks.get(seriesName);
167                     if (seriesLinks == null)
168                     {
169                         seriesLinks = new HashMap JavaDoc();
170                         itemHyperlinks.put(seriesName, seriesLinks);
171                     }
172                     seriesLinks.put(tp, crtTimeSeries.getPrintItemHyperlink());
173                 }
174             }
175         }
176     }
177     
178     public Dataset getCustomDataset()
179     {
180         TimeSeriesCollection dataset = new TimeSeriesCollection();
181         if (seriesNames != null)
182         {
183             for(int i = 0; i < seriesNames.size(); i++)
184             {
185                 Comparable JavaDoc seriesName = (Comparable JavaDoc)seriesNames.get(i);
186                 dataset.addSeries((TimeSeries)seriesMap.get(seriesName));
187             }
188         }
189         return dataset;
190     }
191
192
193     public Class JavaDoc getTimePeriod() {
194         return ((JRTimeSeriesDataset)parent).getTimePeriod();
195     }
196
197     public void setTimePeriod(Class JavaDoc timePeriod) {
198     }
199
200     /**
201      *
202      */

203     public byte getDatasetType() {
204         return JRChartDataset.TIMESERIES_DATASET;
205     }
206     
207     
208     public TimeSeriesLabelGenerator getLabelGenerator(){
209         return new TimeSeriesLabelGenerator(labelsMap);
210     }
211     
212     
213     /**
214      *
215      */

216     public void collectExpressions(JRExpressionCollector collector)
217     {
218         collector.collect(this);
219     }
220
221     
222     public Map JavaDoc getItemHyperlinks()
223     {
224         return itemHyperlinks;
225     }
226     
227     
228     public boolean hasItemHyperlinks()
229     {
230         boolean foundLinks = false;
231         if (timeSeries != null && timeSeries.length > 0)
232         {
233             for (int i = 0; i < timeSeries.length && !foundLinks; i++)
234             {
235                 foundLinks = timeSeries[i].hasItemHyperlink();
236             }
237         }
238         return foundLinks;
239     }
240
241
242     public void validate(JRVerifier verifier)
243     {
244         verifier.verify(this);
245     }
246
247
248 }
249
Popular Tags