KickJava   Java API By Example, From Geeks To Geeks.

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


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.JRXyDataset;
36 import net.sf.jasperreports.charts.JRXySeries;
37 import net.sf.jasperreports.charts.util.XYDatasetLabelGenerator;
38 import net.sf.jasperreports.engine.JRChartDataset;
39 import net.sf.jasperreports.engine.JRExpressionCollector;
40 import net.sf.jasperreports.engine.design.JRVerifier;
41 import net.sf.jasperreports.engine.fill.JRCalculator;
42 import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
43 import net.sf.jasperreports.engine.fill.JRFillChartDataset;
44 import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
45 import net.sf.jasperreports.engine.util.Pair;
46
47 import org.jfree.data.general.Dataset;
48 import org.jfree.data.xy.XYSeries;
49 import org.jfree.data.xy.XYSeriesCollection;
50
51
52 /**
53  * @author Teodor Danciu (teodord@users.sourceforge.net)
54  * @version $Id: JRFillXyDataset.java 1364 2006-08-31 18:13:20 +0300 (Thu, 31 Aug 2006) lucianc $
55  */

56 public class JRFillXyDataset extends JRFillChartDataset implements JRXyDataset
57 {
58
59     /**
60      *
61      */

62     protected JRFillXySeries[] xySeries = null;
63
64     private List JavaDoc seriesNames = null;
65     private Map JavaDoc seriesMap = null;
66     private Map JavaDoc labelsMap = null;
67     
68     private Map JavaDoc itemHyperlinks;
69     
70     
71     /**
72      *
73      */

74     public JRFillXyDataset(
75         JRXyDataset xyDataset,
76         JRFillObjectFactory factory
77         )
78     {
79         super(xyDataset, factory);
80
81         /* */
82         JRXySeries[] srcXySeries = xyDataset.getSeries();
83         if (srcXySeries != null && srcXySeries.length > 0)
84         {
85             xySeries = new JRFillXySeries[srcXySeries.length];
86             for(int i = 0; i < xySeries.length; i++)
87             {
88                 xySeries[i] = (JRFillXySeries)factory.getXySeries(srcXySeries[i]);
89             }
90         }
91     }
92     
93     
94     /**
95      *
96      */

97     public JRXySeries[] getSeries()
98     {
99         return xySeries;
100     }
101
102
103     /**
104      *
105      */

106     protected void customInitialize()
107     {
108         seriesNames = null;
109         seriesMap = null;
110         labelsMap = null;
111         itemHyperlinks = null;
112     }
113
114     
115     /**
116      *
117      */

118     protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException
119     {
120         if (xySeries != null && xySeries.length > 0)
121         {
122             for(int i = 0; i < xySeries.length; i++)
123             {
124                 xySeries[i].evaluate(calculator);
125             }
126         }
127     }
128
129     
130     /**
131      *
132      */

133     protected void customIncrement()
134     {
135         if (xySeries != null && xySeries.length > 0)
136         {
137             if (seriesNames == null)
138             {
139                 seriesNames = new ArrayList JavaDoc();
140                 seriesMap = new HashMap JavaDoc();
141                 labelsMap = new HashMap JavaDoc();
142                 itemHyperlinks = new HashMap JavaDoc();
143             }
144
145             for(int i = 0; i < xySeries.length; i++)
146             {
147                 JRFillXySeries crtXySeries = xySeries[i];
148
149                 Comparable JavaDoc seriesName = crtXySeries.getSeries();
150                 XYSeries xySrs = (XYSeries)seriesMap.get(seriesName);
151                 if (xySrs == null)
152                 {
153                     xySrs = new XYSeries(seriesName);
154                     seriesNames.add(seriesName);
155                     seriesMap.put(seriesName, xySrs);
156                 }
157                 
158                 xySrs.addOrUpdate(
159                     crtXySeries.getXValue(),
160                     crtXySeries.getYValue()
161                     );
162                 
163                 if (crtXySeries.getLabelExpression() != null)
164                 {
165                     Map JavaDoc seriesLabels = (Map JavaDoc)labelsMap.get(seriesName);
166                     if (seriesLabels == null)
167                     {
168                         seriesLabels = new HashMap JavaDoc();
169                         labelsMap.put(seriesName, seriesLabels);
170                     }
171                     
172                     seriesLabels.put(crtXySeries.getXValue(), crtXySeries.getLabel());
173                 }
174                 
175                 if (crtXySeries.hasItemHyperlinks())
176                 {
177                     Map JavaDoc seriesLinks = (Map JavaDoc) itemHyperlinks.get(seriesName);
178                     if (seriesLinks == null)
179                     {
180                         seriesLinks = new HashMap JavaDoc();
181                         itemHyperlinks.put(seriesName, seriesLinks);
182                     }
183                     Pair xyKey = new Pair(crtXySeries.getXValue(), crtXySeries.getYValue());
184                     seriesLinks.put(xyKey, crtXySeries.getPrintItemHyperlink());
185                 }
186             }
187         }
188     }
189
190     
191     /**
192      *
193      */

194     public Dataset getCustomDataset()
195     {
196         XYSeriesCollection dataset = new XYSeriesCollection();
197         if (seriesNames != null)
198         {
199             for(int i = 0; i < seriesNames.size(); i++)
200             {
201                 Comparable JavaDoc seriesName = (Comparable JavaDoc)seriesNames.get(i);
202                 dataset.addSeries((XYSeries)seriesMap.get(seriesName));
203             }
204         }
205         return dataset;
206     }
207
208     
209     /**
210      *
211      */

212     public byte getDatasetType() {
213         return JRChartDataset.XY_DATASET;
214     }
215     
216     
217     /**
218      *
219      */

220     public XYDatasetLabelGenerator getLabelGenerator(){
221         return new XYDatasetLabelGenerator(labelsMap);
222     }
223     
224     
225     /**
226      *
227      */

228     public void collectExpressions(JRExpressionCollector collector)
229     {
230         collector.collect(this);
231     }
232
233     
234     public Map JavaDoc getItemHyperlinks()
235     {
236         return itemHyperlinks;
237     }
238     
239     
240     public boolean hasItemHyperlinks()
241     {
242         boolean foundLinks = false;
243         if (xySeries != null && xySeries.length > 0)
244         {
245             for (int i = 0; i < xySeries.length && !foundLinks; i++)
246             {
247                 JRFillXySeries serie = xySeries[i];
248                 foundLinks = serie.hasItemHyperlinks();
249             }
250         }
251         return foundLinks;
252     }
253
254
255     public void validate(JRVerifier verifier)
256     {
257         verifier.verify(this);
258     }
259
260
261 }
262
Popular Tags