KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > DefaultWindDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either 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, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------------
23  * DefaultWindDataset.java
24  * -----------------------
25  * (C) Copyright 2001-2003, by Achilleus Mantzios and Contributors.
26  *
27  * Original Author: Achilleus Mantzios;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: DefaultWindDataset.java,v 1.5 2003/07/10 07:16:45 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 06-Feb-2002 : Version 1, based on code contributed by Achilleus Mantzios (DG);
35  *
36  */

37
38 package org.jfree.data;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * A default implementation of the {@link WindDataset} interface.
47  *
48  * @author Achilleus Mantzios
49  */

50 public class DefaultWindDataset extends AbstractSeriesDataset implements WindDataset {
51
52     /** The names of the series. */
53     private List JavaDoc seriesNames;
54
55     /** Storage for the series data. */
56     private List JavaDoc allSeriesData;
57
58     /**
59      * Constructs a new, empty, WindDataset.
60      */

61     public DefaultWindDataset() {
62         seriesNames = new java.util.ArrayList JavaDoc();
63         allSeriesData = new java.util.ArrayList JavaDoc();
64     }
65
66     /**
67      * Constructs a WindDataset based on the specified data.
68      *
69      * @param data the wind dataset.
70      */

71     public DefaultWindDataset(Object JavaDoc[][][] data) {
72         this(seriesNameListFromDataArray(data), data);
73     }
74
75     /**
76      * Constructs a WindDataset based on the specified data.
77      *
78      * @param seriesNames the names of the series.
79      * @param data the wind dataset.
80      */

81     public DefaultWindDataset(String JavaDoc[] seriesNames, Object JavaDoc[][][] data) {
82         this(Arrays.asList(seriesNames), data);
83     }
84
85     /**
86      * Constructs a WindDataset based on the specified data.
87      *
88      * @param seriesNames the names of the series.
89      * @param data the wind dataset.
90      */

91     public DefaultWindDataset(List JavaDoc seriesNames, Object JavaDoc[][][] data) {
92
93         this.seriesNames = seriesNames;
94
95         int seriesCount = data.length;
96
97         allSeriesData = new java.util.ArrayList JavaDoc(seriesCount);
98
99         for (int seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++) {
100             List JavaDoc oneSeriesData = new java.util.ArrayList JavaDoc();
101             int maxItemCount = data[seriesIndex].length;
102             for (int itemIndex = 0; itemIndex < maxItemCount; itemIndex++) {
103                 Object JavaDoc xObject = data[seriesIndex][itemIndex][0];
104                 if (xObject != null) {
105                     Number JavaDoc xNumber = null;
106                     if (xObject instanceof Number JavaDoc) {
107                         xNumber = (Number JavaDoc) xObject;
108                     }
109                     else {
110                         if (xObject instanceof Date JavaDoc) {
111                             Date JavaDoc xDate = (Date JavaDoc) xObject;
112                             xNumber = new Long JavaDoc(xDate.getTime());
113                         }
114                         else {
115                             xNumber = new Integer JavaDoc(0);
116                         }
117                     }
118                     Number JavaDoc windDir = (Number JavaDoc) data[seriesIndex][itemIndex][1];
119                     Number JavaDoc windForce = (Number JavaDoc) data[seriesIndex][itemIndex][2];
120                     oneSeriesData.add(new WindDataItem(xNumber, windDir, windForce));
121                 }
122             }
123             Collections.sort(oneSeriesData);
124             allSeriesData.add(seriesIndex, oneSeriesData);
125         }
126
127     }
128
129     /**
130      * Returns the number of series in the dataset.
131      * @return The number of series in the dataset.
132      */

133     public int getSeriesCount() {
134         return allSeriesData.size();
135     }
136
137     /**
138      * Returns the number of items in a series.
139      * @param series The series (zero-based index).
140      * @return The number of items in a series.
141      */

142     public int getItemCount(int series) {
143         List JavaDoc oneSeriesData = (List JavaDoc) allSeriesData.get(series);
144         return oneSeriesData.size();
145     }
146
147     /**
148      * Returns the name of a series.
149      * @param series The series (zero-based index).
150      * @return The name of the specified series.
151      */

152     public String JavaDoc getSeriesName(int series) {
153         return seriesNames.get(series).toString();
154     }
155
156     /**
157      * Returns the x-value for one item within a series. This should represent
158      * a point in time, encoded as milliseconds in the same way as
159      * java.util.Date.
160      *
161      * @param series The series (zero-based index).
162      * @param item The item (zero-based index).
163      * @return The x-value for the item within the series.
164      */

165     public Number JavaDoc getXValue(int series, int item) {
166         List JavaDoc oneSeriesData = (List JavaDoc) allSeriesData.get(series);
167         WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
168         return windItem.getX();
169     }
170
171     /**
172      * Returns the y-value for one item within a series. This maps to the
173      * getWindForce(...) method and is implemented because WindDataset is an
174      * extension of XYDataset.
175      *
176      * @param series The series (zero-based index).
177      * @param item The item (zero-based index).
178      * @return The y-value for the item within the series.
179      */

180     public Number JavaDoc getYValue(int series, int item) {
181         return getWindForce(series, item);
182     }
183
184     /**
185      * Returns the wind direction for one item within a series. This is a
186      * number between 0 and 12, like the numbers on a clock face.
187      * @param series The series (zero-based index).
188      * @param item The item (zero-based index).
189      * @return The wind direction for the item within the series.
190      */

191     public Number JavaDoc getWindDirection(int series, int item) {
192         List JavaDoc oneSeriesData = (List JavaDoc) allSeriesData.get(series);
193         WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
194         return windItem.getWindDirection();
195     }
196
197     /**
198      * Returns the wind force for one item within a series. This is a number
199      * between 0 and 12, as defined by the Beaufort scale.
200      * @param series The series (zero-based index).
201      * @param item The item (zero-based index).
202      * @return The wind force for the item within the series.
203      */

204     public Number JavaDoc getWindForce(int series, int item) {
205         List JavaDoc oneSeriesData = (List JavaDoc) allSeriesData.get(series);
206         WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
207         return windItem.getWindForce();
208     }
209
210     /**
211      * Utility method for automatically generating series names.
212      * @param data the wind dataset.
213      *
214      * @return an array of <i>Series N</i> with N = { 1 .. data.length }.
215      */

216     public static List JavaDoc seriesNameListFromDataArray(Object JavaDoc[][] data) {
217
218         int seriesCount = data.length;
219         List JavaDoc seriesNameList = new java.util.ArrayList JavaDoc(seriesCount);
220         for (int i = 0; i < seriesCount; i++) {
221             seriesNameList.add("Series " + (i + 1));
222         }
223         return seriesNameList;
224
225     }
226
227 }
228
229 /**
230  * A wind data item.
231  *
232  * @author Achilleus Mantzios
233  */

234 class WindDataItem implements Comparable JavaDoc {
235
236     /** The x-value. */
237     private Number JavaDoc x;
238
239     /** The wind direction. */
240     private Number JavaDoc windDir;
241
242     /** The wind force. */
243     private Number JavaDoc windForce;
244
245     /**
246      * Creates a new wind data item.
247      *
248      * @param x the x-value.
249      * @param windDir the direction.
250      * @param windForce the force.
251      */

252     public WindDataItem(Number JavaDoc x, Number JavaDoc windDir, Number JavaDoc windForce) {
253         this.x = x;
254         this.windDir = windDir;
255         this.windForce = windForce;
256     }
257
258     /**
259      * Returns the x-value.
260      *
261      * @return The x-value.
262      */

263     public Number JavaDoc getX() {
264         return this.x;
265     }
266
267     /**
268      * Returns the wind direction.
269      *
270      * @return The wind direction.
271      */

272     public Number JavaDoc getWindDirection() {
273         return this.windDir;
274     }
275
276     /**
277      * Returns the wind force.
278      *
279      * @return The wind force.
280      */

281     public Number JavaDoc getWindForce() {
282         return this.windForce;
283     }
284
285     /**
286      * Compares this item to another object.
287      *
288      * @param object the other object.
289      *
290      * @return An int that indicates the relative comparison.
291      */

292     public int compareTo(Object JavaDoc object) {
293         if (object instanceof WindDataItem) {
294             WindDataItem item = (WindDataItem) object;
295             if (this.x.doubleValue() > item.x.doubleValue()) {
296                 return 1;
297             }
298             else if (this.x.equals(item.x)) {
299                 return 0;
300             }
301             else {
302                 return -1;
303             }
304         }
305         else {
306             throw new ClassCastException JavaDoc("WindDataItem.compareTo(error)");
307         }
308     }
309
310 }
311
Popular Tags