KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
32 import java.util.List JavaDoc;
33
34 import net.sf.jasperreports.charts.JRHighLowDataset;
35 import net.sf.jasperreports.engine.JRChartDataset;
36 import net.sf.jasperreports.engine.JRException;
37 import net.sf.jasperreports.engine.JRExpression;
38 import net.sf.jasperreports.engine.JRExpressionCollector;
39 import net.sf.jasperreports.engine.JRHyperlink;
40 import net.sf.jasperreports.engine.JRPrintHyperlink;
41 import net.sf.jasperreports.engine.JRRuntimeException;
42 import net.sf.jasperreports.engine.design.JRVerifier;
43 import net.sf.jasperreports.engine.fill.JRCalculator;
44 import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
45 import net.sf.jasperreports.engine.fill.JRFillChartDataset;
46 import net.sf.jasperreports.engine.fill.JRFillHyperlinkHelper;
47 import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
48
49 import org.jfree.data.general.Dataset;
50 import org.jfree.data.xy.DefaultHighLowDataset;
51
52
53 /**
54  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
55  * @version $Id: JRFillHighLowDataset.java 1532 2006-12-21 19:50:55 +0200 (Thu, 21 Dec 2006) teodord $
56  */

57 public class JRFillHighLowDataset extends JRFillChartDataset implements JRHighLowDataset
58 {
59
60     /**
61      *
62      */

63     private String JavaDoc series = null;
64     private List JavaDoc elements = new ArrayList JavaDoc();
65     private Date JavaDoc date = null;
66     private Number JavaDoc high = null;
67     private Number JavaDoc low = null;
68     private Number JavaDoc open = null;
69     private Number JavaDoc close = null;
70     private Number JavaDoc volume = null;
71     
72     private JRPrintHyperlink itemHyperlink;
73     private List JavaDoc itemHyperlinks;
74
75     
76     /**
77      *
78      */

79     public JRFillHighLowDataset(JRHighLowDataset dataset, JRFillObjectFactory factory)
80     {
81         super(dataset, factory);
82     }
83
84
85     protected void customInitialize()
86     {
87         elements = new ArrayList JavaDoc();
88         itemHyperlinks = new ArrayList JavaDoc();
89     }
90
91
92     protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException
93     {
94         series = (String JavaDoc) calculator.evaluate(getSeriesExpression());
95         date = (Date JavaDoc) calculator.evaluate(getDateExpression());
96         high = (Number JavaDoc) calculator.evaluate(getHighExpression());
97         low = (Number JavaDoc) calculator.evaluate(getLowExpression());
98         open = (Number JavaDoc) calculator.evaluate(getOpenExpression());
99         close = (Number JavaDoc) calculator.evaluate(getCloseExpression());
100         volume = (Number JavaDoc) calculator.evaluate(getVolumeExpression());
101         
102         if (hasItemHyperlink())
103         {
104             evaluateSectionHyperlink(calculator);
105         }
106     }
107
108
109     protected void evaluateSectionHyperlink(JRCalculator calculator) throws JRExpressionEvalException
110     {
111         try
112         {
113             itemHyperlink = JRFillHyperlinkHelper.evaluateHyperlink(getItemHyperlink(), calculator, JRExpression.EVALUATION_DEFAULT);
114         }
115         catch (JRExpressionEvalException e)
116         {
117             throw e;
118         }
119         catch (JRException e)
120         {
121             throw new JRRuntimeException(e);
122         }
123     }
124
125
126     protected void customIncrement()
127     {
128         elements.add(new HighLowElement(date, high, low, open, close, volume));
129         
130         if (hasItemHyperlink())
131         {
132             itemHyperlinks.add(itemHyperlink);
133         }
134     }
135
136
137     public Dataset getCustomDataset()
138     {
139         int size = elements.size();
140         if (size > 0)
141         {
142             Date JavaDoc[] dateArray = new Date JavaDoc[size];
143             double[] highArray = new double[size];
144             double[] lowArray = new double[size];
145             double[] openArray = new double[size];
146             double[] closeArray = new double[size];
147             double[] volumeArray = new double[size];
148
149             for (int i = 0; i < elements.size(); i++) {
150                 HighLowElement bean = (HighLowElement) elements.get(i);
151                 dateArray[i] = new Date JavaDoc(bean.getDate().getTime());
152                 highArray[i] = bean.getHigh().doubleValue();
153                 lowArray[i] = bean.getLow().doubleValue();
154                 openArray[i] = bean.getOpen().doubleValue();
155                 closeArray[i] = bean.getClose().doubleValue();
156                 volumeArray[i] = bean.getVolume().doubleValue();
157             }
158
159             return new DefaultHighLowDataset(series, dateArray, highArray, lowArray, openArray, closeArray, volumeArray);
160         }
161         
162         return null;
163     }
164
165
166     public JRExpression getSeriesExpression()
167     {
168         return ((JRHighLowDataset)parent).getSeriesExpression();
169     }
170
171
172     public JRExpression getDateExpression()
173     {
174         return ((JRHighLowDataset)parent).getDateExpression();
175     }
176
177
178     public JRExpression getHighExpression()
179     {
180         return ((JRHighLowDataset)parent).getHighExpression();
181     }
182
183
184     public JRExpression getLowExpression()
185     {
186         return ((JRHighLowDataset)parent).getLowExpression();
187     }
188
189
190     public JRExpression getOpenExpression()
191     {
192         return ((JRHighLowDataset)parent).getOpenExpression();
193     }
194
195
196     public JRExpression getCloseExpression()
197     {
198         return ((JRHighLowDataset)parent).getCloseExpression();
199     }
200
201
202     public JRExpression getVolumeExpression()
203     {
204         return ((JRHighLowDataset)parent).getVolumeExpression();
205     }
206
207     /**
208      *
209      */

210     private static class HighLowElement
211     {
212         Date JavaDoc date;
213         Number JavaDoc high;
214         Number JavaDoc low;
215         Number JavaDoc open;
216         Number JavaDoc close;
217         Number JavaDoc volume;
218
219
220         public HighLowElement(
221             Date JavaDoc date,
222             Number JavaDoc high,
223             Number JavaDoc low,
224             Number JavaDoc open,
225             Number JavaDoc close,
226             Number JavaDoc volume
227             )
228         {
229             if (date == null)
230                 throw new JRRuntimeException("Date value is null in high-low series.");
231             this.date = date;
232
233             if (high == null)
234                 throw new JRRuntimeException("High value is null in high-low series.");
235             this.high = high;
236             
237             if (low == null)
238                 throw new JRRuntimeException("Low value is null in high-low series.");
239             this.low = low;
240             
241             if (open == null)
242                 throw new JRRuntimeException("Open value is null in high-low series.");
243             this.open = open;
244             
245             if (close == null)
246                 throw new JRRuntimeException("Close value is null in high-low series.");
247             this.close = close;
248             
249             if (volume == null)
250                 throw new JRRuntimeException("Volume value is null in high-low series.");
251             this.volume = volume;
252         }
253
254
255         public Date JavaDoc getDate()
256         {
257             return date;
258         }
259
260
261         public void setDate(Date JavaDoc date)
262         {
263             this.date = date;
264         }
265
266
267         public Number JavaDoc getHigh()
268         {
269             return high;
270         }
271
272
273         public void setHigh(Number JavaDoc high)
274         {
275             this.high = high;
276         }
277
278
279         public Number JavaDoc getLow()
280         {
281             return low;
282         }
283
284
285         public void setLow(Number JavaDoc low)
286         {
287             this.low = low;
288         }
289
290
291         public Number JavaDoc getOpen()
292         {
293             return open;
294         }
295
296
297         public void setOpen(Number JavaDoc open)
298         {
299             this.open = open;
300         }
301
302
303         public Number JavaDoc getClose()
304         {
305             return close;
306         }
307
308
309         public void setClose(Number JavaDoc close)
310         {
311             this.close = close;
312         }
313
314
315         public Number JavaDoc getVolume()
316         {
317             return volume;
318         }
319
320
321         public void setVolume(Number JavaDoc volume)
322         {
323             this.volume = volume;
324         }
325     }
326
327     /**
328      *
329      */

330     public byte getDatasetType() {
331         return JRChartDataset.HIGHLOW_DATASET;
332     }
333
334     
335     /**
336      *
337      */

338     public void collectExpressions(JRExpressionCollector collector)
339     {
340         collector.collect(this);
341     }
342
343
344     public JRHyperlink getItemHyperlink()
345     {
346         return ((JRHighLowDataset) parent).getItemHyperlink();
347     }
348
349
350     public boolean hasItemHyperlink()
351     {
352         return getItemHyperlink() != null;
353     }
354
355     
356     public List JavaDoc getItemHyperlinks()
357     {
358         return itemHyperlinks;
359     }
360
361
362     public void validate(JRVerifier verifier)
363     {
364         verifier.verify(this);
365     }
366
367 }
368
Popular Tags