KickJava   Java API By Example, From Geeks To Geeks.

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


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  * NonGridContourDataset.java
24  * --------------------------
25  * (C) Copyright 2002, 2003, by David M. O'Donnell.
26  *
27  * Original Author: David M. O'Donnell;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: NonGridContourDataset.java,v 1.6 2003/09/03 15:08:51 mungady Exp $
31  *
32  * Changes (from 24-Jul-2003)
33  * --------------------------
34  * 24-Jul-2003 : Added standard header (DG);
35  *
36  */

37
38 package org.jfree.data;
39
40 /**
41  * A convenience class that extends the {@link DefaultContourDataset} to acommadate non-grid data.
42  */

43 public class NonGridContourDataset extends DefaultContourDataset {
44
45     /** Default number of x values. */
46     final int DEFAULT_NUM_X = 50;
47     
48     /** Default number of y values. */
49     final int DEFAULT_NUM_Y = 50;
50     
51     /** Default power. */
52     final int DEFAULT_POWER = 4;
53
54     /**
55      * Default constructor.
56      */

57     public NonGridContourDataset() {
58         super();
59     }
60
61     /**
62      * Constructor for NonGridContourDataset. Uses default values for grid dimensions and
63      * weighting.
64      *
65      * @param seriesName the series name.
66      * @param xData the x values.
67      * @param yData the y values.
68      * @param zData the z values.
69      */

70     public NonGridContourDataset(String JavaDoc seriesName,
71                                  Object JavaDoc[] xData, Object JavaDoc[] yData, Object JavaDoc[] zData) {
72         super(seriesName, xData, yData, zData);
73         buildGrid(DEFAULT_NUM_X, DEFAULT_NUM_Y, DEFAULT_POWER);
74     }
75
76     /**
77      * Constructor for NonGridContourDataset.
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      * @param numX number grid cells in along the x-axis
84      * @param numY number grid cells in along the y-axis
85      * @param power exponent for inverse distance weighting
86      */

87     public NonGridContourDataset(String JavaDoc seriesName, Object JavaDoc[] xData, Object JavaDoc[] yData, Object JavaDoc[] zData,
88                                   int numX, int numY, int power) {
89         super(seriesName, xData, yData, zData);
90         buildGrid(numX, numY, power);
91     }
92
93     /**
94      * Builds a regular grid. Maps the non-grid data into the regular grid using an
95      * inverse distance between grid and non-grid points. Weighting of distance can
96      * be controlled by setting through the power parameter that controls the exponent
97      * used on the distance weighting (e.g., distance^power).
98      * @param numX number grid points in along the x-axis
99      * @param numY number grid points in along the y-axis
100      * @param power exponent for inverse distance weighting
101      */

102     protected void buildGrid(int numX, int numY, int power) {
103
104         double[] xGrid = null;
105         double[] yGrid = null;
106         double[] zGrid = null;
107
108         int numValues = numX * numY;
109         xGrid = new double[numValues];
110         yGrid = new double [numValues];
111         zGrid = new double [numValues];
112
113 // Find min, max for the x and y axes
114
double xMin = 1.e20;
115         for (int k = 0; k < xValues.length; k++) {
116             xMin = Math.min(xMin, xValues[k].doubleValue());
117         }
118
119         double xMax = -1.e20;
120         for (int k = 0; k < xValues.length; k++) {
121             xMax = Math.max(xMax, xValues[k].doubleValue());
122         }
123
124         double yMin = 1.e20;
125         for (int k = 0; k < yValues.length; k++) {
126             yMin = Math.min(yMin, yValues[k].doubleValue());
127         }
128
129         double yMax = -1.e20;
130         for (int k = 0; k < yValues.length; k++) {
131             yMax = Math.max(yMax, yValues[k].doubleValue());
132         }
133
134         Range xRange = new Range(xMin, xMax);
135         Range yRange = new Range(yMin, yMax);
136
137         xRange.getLength();
138         yRange.getLength();
139
140 // Determine the cell size
141
double dxGrid = xRange.getLength() / (numX - 1);
142         double dyGrid = yRange.getLength() / (numY - 1);
143
144 // Generate the grid
145
double x = 0.0;
146         for (int i = 0; i < numX; i++) {
147             if (i == 0) {
148                 x = xMin;
149             }
150             else {
151                 x += dxGrid;
152             }
153             double y = 0.0;
154             for (int j = 0; j < numY; j++) {
155                 int k = numY * i + j;
156                 xGrid[k] = x;
157                 if (j == 0) {
158                     y = yMin;
159                 }
160                 else {
161                     y += dyGrid;
162                 }
163                 yGrid[k] = y;
164             }
165         }
166
167 // Map the nongrid data into the new regular grid
168
for (int kGrid = 0; kGrid < xGrid.length; kGrid++) {
169             double dTotal = 0.0;
170             zGrid[kGrid] = 0.0;
171             for (int k = 0; k < xValues.length; k++) {
172                 double xPt = xValues[k].doubleValue();
173                 double yPt = yValues[k].doubleValue();
174                 double d = distance(xPt, yPt, xGrid[kGrid], yGrid[kGrid]);
175                 if (power != 1) {
176                     d = Math.pow(d, power);
177                 }
178                 d = Math.sqrt(d);
179                 if (d > 0.0) {
180                     d = 1.0 / d;
181                 }
182                 else { // if d is real small set the inverse to a large number to avoid INF
183
d = 1.e20;
184                 }
185                 if (zValues[k] != null) {
186                     // scale by the inverse of distance^power
187
zGrid[kGrid] += zValues[k].doubleValue() * d;
188                 }
189                 dTotal += d;
190             }
191             zGrid[kGrid] = zGrid[kGrid] / dTotal; //remove distance of the sum
192
}
193
194 //initalize xValues, yValues, and zValues arrays.
195
initialize(formObjectArray(xGrid), formObjectArray(yGrid), formObjectArray(zGrid));
196
197     }
198
199     /**
200      * Calculates the distance between two points.
201      *
202      * @param xDataPt the x coordinate.
203      * @param yDataPt the y coordinate.
204      * @param xGrdPt the x grid coordinate.
205      * @param yGrdPt the y grid coordinate.
206      *
207      * @return The distance between two points.
208      */

209     protected double distance(double xDataPt, double yDataPt, double xGrdPt, double yGrdPt) {
210         double dx = xDataPt - xGrdPt;
211         double dy = yDataPt - yGrdPt;
212         return Math.sqrt(dx * dx + dy * dy);
213     }
214
215 }
216
Popular Tags