Source code: jplot/DataArray.java
1 /*
2 * JPLOT -- Java Plotting Interface for any programme or
3 * as an independent GUI
4 *
5 * Copyright (C) 1999 Jan van der Lee
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 *
22 * Send bugs, suggestions or queries to <jplot@cig.ensmp.fr>
23 * The latest releases are found at
24 * http://www.cig.ensmp.fr/~vanderlee/jplot.html
25 *
26 * Initally developed for use by the Centre d'Informatique Geologique
27 * Ecole des Mines de Paris, Fontainebleau, France.
28 */
29
30 package jplot;
31 import java.util.*;
32 import java.awt.*;
33
34 /**
35 * The <code>DataArray</code> class is a special-purpose array class
36 * for use with the graphical plotting program JPlot. All the plotting
37 * attributes are defined in the base-class LinePars, here we set the
38 * actual data array, i.e. a vector of points (PlotPoints) defining
39 * X,Y.
40 *
41 * @version 24/08/99
42 * @author J. van der Lee
43 */
44 public class DataArray extends LinePars {
45
46 protected Vector points;
47 protected double[] maxValue = new double [GraphPars.NAXES];
48 protected double[] minValue = new double [GraphPars.NAXES];
49 protected double[] lowestNonZeroValue = new double [GraphPars.NAXES];
50
51 /**
52 * Default constructor, initializes the class with nothing.
53 */
54 public DataArray() {
55 this(0);
56 }
57
58 /**
59 * Constructor, allocates memory for the data points.
60 * @param s name of this column
61 * @param n maximum number of datapoints.
62 */
63 public DataArray(int n) {
64 allocate(n);
65 }
66
67 /**
68 * Constructor, allocates memory for the data points.
69 * @param n maximum number of datapoints.
70 * @param s name of this column
71 * @param lp line parameters class, contains info of how to draw the line.
72 */
73 public DataArray(int n, LinePars lp) {
74 super(lp);
75 allocate(n);
76 }
77
78 /**
79 * Allocation of memory for the data points.
80 * @param n maximum number of datapoints.
81 */
82 public void allocate(int n) {
83 if (n < 10) n = 10;
84 points = new Vector(n,10);
85 for (int k=0; k<GraphPars.NAXES; k++) {
86 maxValue[k] = -GraphPars.INF;
87 minValue[k] = GraphPars.INF;
88 lowestNonZeroValue[k] =GraphPars.INF;
89 }
90 }
91
92 /**
93 * Updates maximum and minimum values.
94 * @param x just entered x-value of the point
95 * @param y just entered y-value of the point
96 */
97 public void updateMinMax(double x, double y) {
98 if (x > maxValue[GraphPars.X_AXIS]) maxValue[GraphPars.X_AXIS] = x;
99 if (x < minValue[GraphPars.X_AXIS]) minValue[GraphPars.X_AXIS] = x;
100 if (x < lowestNonZeroValue[GraphPars.X_AXIS] && x > 0.0) {
101 lowestNonZeroValue[GraphPars.X_AXIS] = x;
102 }
103 if (y > maxValue[GraphPars.Y_AXIS]) maxValue[GraphPars.Y_AXIS] = y;
104 if (y < minValue[GraphPars.Y_AXIS]) minValue[GraphPars.Y_AXIS] = y;
105 if (y < lowestNonZeroValue[GraphPars.Y_AXIS] && y > 0.0) {
106 lowestNonZeroValue[GraphPars.Y_AXIS] = y;
107 }
108 }
109
110 /**
111 * Adds the values of a plot-point pair (x,y).
112 * This point is added at the end of the array.
113 * @param i index of the plot-point
114 * @param x x-value of the plot-point
115 * @param y y-value of the plot-point
116 */
117 public void addPoint(double x, double y) {
118 y *= multiplier;
119 y += additioner;
120 updateMinMax(x,y);
121 points.add(new PlotPoint(x,y));
122 }
123
124 /**
125 * Adds the values of a plot-point pair (x,y).
126 * This point is added at the end of the array.
127 * @param i index of the plot-point
128 * @param x x-value of the plot-point
129 * @param y y-value of the plot-point
130 * @param liftPen true if the pen should lift after this point
131 */
132 public void addPoint(double x, double y, boolean liftPen) {
133 y *= multiplier;
134 y += additioner;
135 updateMinMax(x,y);
136 points.add(new PlotPoint(x,y,liftPen));
137 }
138
139 /**
140 * Sets the values of a plot-point pair (x,y).
141 * @param i index of the plot-point
142 * @param x x-value of the plot-point
143 * @param y y-value of the plot-point
144 */
145 public void setPoint(int i, double x, double y) {
146 if (i >= 0 && i < points.size()) {
147 updateMinMax(x,y);
148 points.add(i,new PlotPoint(x,y));
149 }
150 }
151
152 /**
153 * Returns the plot-point of the specified index.
154 * @param i index of the plot-point
155 * @return plotpoint at index i
156 */
157 public PlotPoint getPoint(int i) {
158 if (i >= 0 && i < points.size()) return (PlotPoint) points.get(i);
159 return null;
160 }
161
162 /**
163 * Defines that, for this plotpoint, the pen should lift.
164 * This allows to draw discontinuous graphs.
165 * @param i index of the plot-point
166 */
167 public void setLiftPen(int i) {
168 if (i >= 0 && i < points.size()) {
169 ((PlotPoint) points.get(i)).setLiftPen(true);
170 }
171 }
172
173 public boolean liftPen(int i) {
174 return ((PlotPoint) points.get(i)).liftPen();
175 }
176
177 /**
178 * Return the length of the data vector.
179 * @return length of the PlotPoint vector
180 */
181 public int size() {
182 return points.size();
183 }
184
185 /**
186 * Return the data vector
187 * @return data vector with all the plot points
188 */
189 public Vector getData() {
190 return points;
191 }
192
193 /**
194 * Return a specific X-value. This function returns POSINF (1e300)
195 * if index i falls beyond the valid range.
196 * @param i index of the array
197 * @return the value of x at index i
198 */
199 double getX(int i) {
200 if (i >= 0 && i < size()) {
201 return ((PlotPoint)points.get(i)).getX();
202 }
203 return GraphPars.INF;
204 }
205
206 /**
207 * Return a specific Y-value. This function returns POSINF (1e300)
208 * if index i falls beyond the valid range.
209 * @param i index of the array
210 * @return the value of y at index i
211 */
212 double getY(int i) {
213 if (i >= 0 && i < size()) {
214 return ((PlotPoint)points.get(i)).getY();
215 }
216 return GraphPars.INF;
217 }
218
219 /**
220 * Returns the maximum value in the range. Careful, no error
221 * checking on the value of axis, which should be less than
222 * NAXES, defined in GraphPars.
223 * @param axis defines to which axis this function applies.
224 * @return the maximum value.
225 */
226 double getMaxValue(int axis) {
227 return maxValue[axis];
228 }
229
230 /**
231 * Returns the minimum value in the range. Careful, no error
232 * checking on the value of axis, which should be less than
233 * NAXES, defined in GraphPars.
234 * @param axis defines to which axis this function applies.
235 * @return the minimum value.
236 */
237 double getMinValue(int axis) {
238 return minValue[axis];
239 }
240
241 /**
242 * Returns the lowest, but non-zero value in the range. This is a
243 * a usefull 'minimum' value for e.g. logarithmic ranges.
244 * @param axis defines to which axis this function applies.
245 * @return the minimum value.
246 */
247 double getLowestNonZeroValue(int axis) {
248 return lowestNonZeroValue[axis];
249 }
250
251 /**
252 * Returns the stroke used to plot these points.
253 * By default, a solid line of width 1 is assumed.
254 * @return the actual stroke.
255 */
256 BasicStroke getStroke() {
257 float[] dash = {dashLength,dashLength};
258 if (dashLength == 0.0f) return new BasicStroke(penWidth);
259 return new BasicStroke(penWidth,BasicStroke.CAP_BUTT,
260 BasicStroke.JOIN_BEVEL,0,dash,0);
261 }
262
263 /**
264 * Sets the drawing style parameters at once.
265 * @param l line and point drawing parameters.
266 */
267 void setLinePars(LinePars l) {
268 copy(l);
269 }
270
271 /**
272 * Set the name of this dataset.
273 * @param s string which contains the name of the dataset
274 */
275 /*
276 void setName(String s) {
277 name = s;
278 }*/
279
280 /**
281 * Sorts the arrays in ascending or descending order.
282 * @param ascending true if we should sort from small to large
283 */
284 public void sort(boolean ascending) {
285 if (ascending) {
286 // sort-routing in ascending order
287 }
288 else {
289 // sort-routing in descending order
290 }
291 }
292 }
293