KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultContourDataset.java
24  * --------------------------
25  * (C) Copyright 2002, 2003, by David M. O'Donnell and Contributors.
26  *
27  * Original Author: David M. O'Donnell;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: DefaultContourDataset.java,v 1.5 2003/07/24 11:09:13 mungady Exp $
31  *
32  * Changes (from 23-Jan-2003)
33  * --------------------------
34  * 23-Jan-2003 : Added standard header (DG);
35  * 20-May-2003 : removed member vars numX and numY, which were never used (TM)
36  */

37
38 package org.jfree.data;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 /**
45  * A convenience class that provides a default implementation of the {@link ContourDataset}
46  * interface.
47  *
48  * @author David M. O'Donnell
49  */

50 public class DefaultContourDataset extends AbstractDataset implements ContourDataset {
51
52     /** The series name (this dataset supports only one series). */
53     protected String JavaDoc seriesName = null;
54
55     /** Storage for the x values. */
56     protected Number JavaDoc[] xValues = null;
57
58     /** Storage for the y values. */
59     protected Number JavaDoc[] yValues = null;
60
61     /** Storage for the z values. */
62     protected Number JavaDoc[] zValues = null;
63
64     /** The index for the start of each column in the data. */
65     protected int xIndex[] = null;
66
67     /** Flags that track whether x, y and z are dates. */
68     boolean[] dateAxis = new boolean[3];
69
70     /**
71      * Creates a new dataset, initially empty.
72      */

73     public DefaultContourDataset() {
74     }
75
76     /**
77      * Constructs a new dataset with the given data.
78      *
79      * @param seriesName the series name.
80      * @param xData the x values.
81      * @param yData the y values.
82      * @param zData the z values.
83      */

84     public DefaultContourDataset(String JavaDoc seriesName,
85                                  Object JavaDoc[] xData,
86                                  Object JavaDoc[] yData,
87                                  Object JavaDoc[] zData) {
88
89         this.seriesName = seriesName;
90         initialize(xData, yData, zData);
91     }
92
93     /**
94      * Initialises the dataset.
95      *
96      * @param xData the x values.
97      * @param yData the y values.
98      * @param zData the z values.
99      */

100     public void initialize(Object JavaDoc[] xData,
101                             Object JavaDoc[] yData,
102                             Object JavaDoc[] zData) {
103
104         xValues = new Double JavaDoc[xData.length];
105         yValues = new Double JavaDoc[yData.length];
106         zValues = new Double JavaDoc[zData.length];
107
108        // We organise the data with the following assumption:
109
// 1) the data are sorted by x then y
110
// 2) that the data will be represented by a rectangle formed by
111
// using x[i+1], x, y[j+1], and y.
112
// 3) we march along the y-axis at the same value of x until a new value x is
113
// found at which point we will flag the index where x[i+1]<>x[i]
114

115         Vector JavaDoc tmpVector = new Vector JavaDoc(); //create a temporary vector
116
double x = 1.123452e31; // set x to some arbitary value (used below)
117
for (int k = 0; k < xValues.length; k++) {
118             if (xData[k] != null) {
119                 Number JavaDoc xNumber = null;
120                 if (xData[k] instanceof Number JavaDoc) {
121                     xNumber = (Number JavaDoc) xData[k];
122                 }
123                 else if (xData[k] instanceof Date JavaDoc) {
124                     dateAxis[0] = true;
125                     Date JavaDoc xDate = (Date JavaDoc) xData[k];
126                     xNumber = new Long JavaDoc(xDate.getTime()); //store data as Long
127
}
128                 else {
129                     xNumber = new Integer JavaDoc(0);
130                 }
131                 xValues[k] = new Double JavaDoc(xNumber.doubleValue()); // store Number as Double
132

133                 // check if starting new column
134
if (x != xValues[k].doubleValue()) {
135                     tmpVector.add(new Integer JavaDoc(k)); //store index where new column starts
136
x = xValues[k].doubleValue(); // set x to most recent value
137
}
138             }
139         }
140
141         Object JavaDoc[] inttmp = tmpVector.toArray();
142         xIndex = new int[inttmp.length]; // create array xIndex to hold new column indices
143

144         for (int i = 0; i < inttmp.length; i++) {
145             xIndex[i] = ((Integer JavaDoc) inttmp[i]).intValue();
146         }
147         for (int k = 0; k < yValues.length; k++) { // store y and z axes as Doubles
148
yValues[k] = (Double JavaDoc) yData[k];
149             if (zData[k] != null) {
150                 zValues[k] = (Double JavaDoc) zData[k];
151             }
152         }
153     }
154
155     /**
156      * Creates an object array from an array of doubles.
157      *
158      * @param data the data.
159      *
160      * @return An array of <code>Double</code> objects.
161      */

162     public static Object JavaDoc[][] formObjectArray(double[][] data) {
163         Object JavaDoc[][] object = new Double JavaDoc[data.length][data[0].length];
164
165         for (int i = 0; i < object.length; i++) {
166             for (int j = 0; j < object[i].length; j++) {
167                 object[i][j] = new Double JavaDoc(data[i][j]);
168             }
169         }
170         return object;
171     }
172
173     /**
174      * Creates an object array from an array of doubles.
175      *
176      * @param data the data.
177      *
178      * @return An array of <code>Double</code> objects.
179      */

180     public static Object JavaDoc[] formObjectArray(double[] data) {
181
182         Object JavaDoc[] object = new Double JavaDoc[data.length];
183         for (int i = 0; i < object.length; i++) {
184             object[i] = new Double JavaDoc(data[i]);
185         }
186         return object;
187     }
188
189     /**
190      * Returns the number of items in the specified series.
191      * <P>
192      * Method provided to satisfy the {@link XYDataset} interface implementation.
193      *
194      * @param series must be zero, as this dataset only supports one series.
195      *
196      * @return the item count.
197      */

198     public int getItemCount(int series) {
199         if (series > 0) {
200             System.out.println("Only one series for contour");
201         }
202         return zValues.length;
203     }
204
205     /**
206      * Returns the maximum z-value.
207      *
208      * @return The maximum z-value.
209      */

210     public double getMaxZValue() {
211         double zMax = -1.e20;
212         for (int k = 0; k < zValues.length; k++) {
213             if (zValues[k] != null) {
214                 zMax = Math.max(zMax, zValues[k].doubleValue());
215             }
216         }
217         return zMax;
218     }
219
220     /**
221      * Returns the minimum z-value.
222      *
223      * @return The minimum z-value.
224      */

225     public double getMinZValue() {
226
227         double zMin = 1.e20;
228         for (int k = 0; k < zValues.length; k++) {
229             if (zValues[k] != null) {
230                 zMin = Math.min(zMin, zValues[k].doubleValue());
231             }
232         }
233         return zMin;
234     }
235
236     /**
237      * Returns the maximum z-value within visible region of plot.
238      *
239      * @param x the x range.
240      * @param y the y range.
241      *
242      * @return The z range.
243      */

244     public Range getZValueRange(Range x, Range y) {
245
246         double minX = x.getLowerBound();
247         double minY = y.getLowerBound();
248         double maxX = x.getUpperBound();
249         double maxY = y.getUpperBound();
250
251         double zMin = 1.e20;
252         double zMax = -1.e20;
253         for (int k = 0; k < zValues.length; k++) {
254             if (xValues[k].doubleValue() >= minX
255                 && xValues[k].doubleValue() <= maxX
256                 && yValues[k].doubleValue() >= minY
257                 && yValues[k].doubleValue() <= maxY) {
258                 if (zValues[k] != null) {
259                     zMin = Math.min(zMin, zValues[k].doubleValue());
260                     zMax = Math.max(zMax, zValues[k].doubleValue());
261                 }
262             }
263         }
264
265         return new Range(zMin, zMax);
266     }
267
268     /**
269      * Returns the minimum z-value.
270      *
271      * @param minX the minimum x value.
272      * @param minY the minimum y value.
273      * @param maxX the maximum x value.
274      * @param maxY the maximum y value.
275      *
276      * @return the minimum z-value.
277      */

278     public double getMinZValue(double minX, double minY, double maxX, double maxY) {
279
280         double zMin = 1.e20;
281         for (int k = 0; k < zValues.length; k++) {
282             if (zValues[k] != null)
283             zMin = Math.min(zMin, zValues[k].doubleValue());
284         }
285         return zMin;
286
287     }
288
289     /**
290      * Returns the number of series.
291      * <P>
292      * Required by XYDataset interface (this will always return 1)
293      *
294      * @return 1.
295      */

296     public int getSeriesCount() {
297         return 1;
298     }
299
300     /**
301      * Returns the name of the specified series.
302      *
303      * Method provided to satisfy the XYDataset interface implementation
304      *
305      * @param series must be zero.
306      *
307      * @return the series name.
308      */

309     public String JavaDoc getSeriesName(int series) {
310         if (series > 0) {
311             System.out.println("Only one series for contour");
312         }
313         return seriesName;
314     }
315
316     /**
317      * Returns the index of the xvalues.
318      *
319      * @return The x values.
320      */

321     public int[] getXIndices() {
322         return xIndex;
323     }
324
325     /**
326      * Returns the x values.
327      *
328      * @return The x values.
329      */

330     public Number JavaDoc[] getXValues() {
331         return xValues;
332     }
333
334     /**
335      * Returns the x value for the specified series and index (zero-based indices).
336      * Required by the XYDataset
337      *
338      * @param series must be zero;
339      * @param item the item index (zero-based).
340      *
341      * @return The x value.
342      */

343     public Number JavaDoc getXValue(int series, int item) {
344         if (series > 0) {
345             System.out.println("Only one series for contour");
346         }
347         return xValues[item];
348     }
349
350     /**
351      * Returns an x value.
352      *
353      * @param item the item index (zero-based).
354      *
355      * @return The X value.
356      */

357     public Number JavaDoc getXValue(int item) {
358         return xValues[item];
359     }
360
361     /**
362      * Returns a Number array containing all y values.
363      *
364      * @return The Y values.
365      */

366     public Number JavaDoc[] getYValues() {
367         return yValues;
368     }
369
370     /**
371      * Returns the y value for the specified series and index (zero-based indices).
372      * Required by the XYDataset
373      *
374      * @param series the series index (must be zero for this dataset).
375      * @param item the item index (zero-based).
376      *
377      * @return The Y value.
378      */

379     public Number JavaDoc getYValue(int series, int item) {
380         if (series > 0) {
381             System.out.println("Only one series for contour");
382         }
383         return yValues[item];
384     }
385
386     /**
387      * Returns a Number array containing all z values.
388      *
389      * @return The Z values.
390      */

391     public Number JavaDoc[] getZValues() {
392         return zValues;
393     }
394
395     /**
396      * Returns the z value for the specified series and index (zero-based indices).
397      * Required by the XYDataset
398      *
399      * @param series the series index (must be zero for this dataset).
400      * @param item the item index (zero-based).
401      *
402      * @return The Z value.
403      */

404     public Number JavaDoc getZValue(int series, int item) {
405         if (series > 0) {
406             System.out.println("Only one series for contour");
407         }
408         return zValues[item];
409     }
410
411     /**
412      * Returns an int array contain the index into the x values.
413      *
414      * @return The X values.
415      */

416     public int[] indexX() {
417         int[] index = new int[xValues.length];
418         for (int k = 0; k < index.length; k++) {
419             index[k] = indexX(k);
420         }
421         return index;
422     }
423
424     /**
425      * Given index k, returns the column index containing k.
426      *
427      * @param k index of interest.
428      *
429      * @return The column index.
430      */

431     public int indexX(int k) {
432         int i = Arrays.binarySearch(xIndex, k);
433         if (i >= 0) {
434             return i;
435         } else {
436             return -1 * i - 2;
437         }
438     }
439
440
441     /**
442      * Given index k, return the row index containing k.
443      *
444      * @param k index of interest.
445      *
446      * @return The row index.
447      */

448     public int indexY(int k) { // this may be obsolete (not used anywhere)
449
return (k / xValues.length);
450     }
451
452     /**
453      * Given column and row indices, returns the k index.
454      *
455      * @param i index of along x-axis.
456      * @param j index of along y-axis.
457      *
458      * @return The Z index.
459      */

460     public int indexZ(int i, int j) {
461         return xValues.length * j + i;
462     }
463
464     /**
465      * Returns true if axis are dates.
466      *
467      * @param axisNumber The axis where 0-x, 1-y, and 2-z.
468      *
469      * @return A boolean.
470      */

471     public boolean isDateAxis(int axisNumber) {
472         if (axisNumber < 0 || axisNumber > 2) {
473             return false; // bad axisNumber
474
}
475         return dateAxis[axisNumber];
476     }
477
478     /**
479      * Sets the names of the series in the data source.
480      *
481      * @param seriesNames The names of the series in the data source.
482      */

483     public void setSeriesNames(String JavaDoc[] seriesNames) {
484         if (seriesNames.length > 1) {
485             System.out.println("Contours only support one series");
486         }
487         seriesName = seriesNames[0];
488         fireDatasetChanged();
489     }
490
491 }
492
Popular Tags