/Users/lyon/j4p/src/bookExamples/ch27BusinessGraphics/charts/DoubleDataBean.java
|
1 package bookExamples.ch27BusinessGraphics.charts;
2
3
4 /**
5 * DoubleDataBean is the base class which is required in order to draw any graphs.
6 * It contains all the utility methods to calculate the necessary graphics.graph
7 * data for drawing.
8 * Constants include:
9 * <UL>
10 * <LI> BIGNUM: an upper bound used in calculating the minimum value in passed data
11 * <LI> LITTLENUM: a lower bound used in calculating the maximum value in passed data
12 * <LI> X_OFFSET: a constant used to correct for the portion of the image frame taken
13 * up by the GUI elements
14 * <LI> Y_OFFSET: a constant used to correct for the portion of the image frame taken up
15 * by the GUI elements (with offsets set to 0, the axes are drawn off-screen)
16 * </UL>
17 * Passed parameters set with defaults include:
18 * <UL>
19 * <LI> width: the width of the drawn image
20 * <LI> height: the height of the drawn image
21 * <LI> xVals: the X data to be graphed; the primary data for the pie graphics.graph
22 * <LI> yVals: the Y data to be graphed; the primary data for the bar graphics.graph
23 * <LI> labels: the labels for the pie graphics.graph data elements
24 * <LI> isGridOn: whether or not a grid should be drawn
25 * <LI> isTicksOn: whether or not ticks should be drawn along the axes
26 * <LI> xLabel: title for the X axis
27 * <LI> yLabel: title for the Y axis
28 * <LI> title: title for the graphics.graph
29 * </UL>
30 *
31 * @author Allison McHenry
32 * @author Douglas Lyon, PhD
33 * @since JDK 1.3
34 */
35
36 public class DoubleDataBean {
37
38 //BEGIN GLOBAL VARIABLE DECLARATIONS
39
40 private static final double BIGNUM = 32768;
41 private static final double LITTLENUM = -32768;
42 protected static final int X_OFFSET = 10;
43 protected static final int Y_OFFSET = 10;
44
45 private int width = 300;
46 private int height = 300;
47
48 private double xVals[] = {1, 2, 3, 4};
49 private double yVals[] = {10, -20, -40, 80};
50
51
52 private boolean isGridOn = true;
53 private boolean isTicksOn = true;
54 private String xLabel = "This is the X axis";
55 private String yLabel = "This is the Y axis";
56 private String title = "This is the Title";
57
58 /**
59 * Default constructor, used particularly to instantiate an instance of DoubleDataBean by
60 * the LineGraph, PieGraph and BarGraph classes
61 */
62 public DoubleDataBean(int w, int h) {
63 setWidth(w);
64 setHeight(h);
65 }
66
67 /**
68 * Sets image width.
69 *
70 * @param _width image width (not frame width)
71 */
72 public void setWidth(int _width) {
73 width = _width;
74 }
75
76 /**
77 * Sets image height.
78 *
79 * @param _height image height (not frame height)
80 */
81 public void setHeight(int _height) {
82 height = _height;
83 }
84
85 /**
86 * Sets x-axis label on bar graphs and line graphs.
87 *
88 * @param _xLabel x-axis label
89 */
90 public void setXLabel(String _xLabel) {
91 xLabel = _xLabel;
92 }
93
94 /**
95 * Sets y-axis label on bar graphs and line graphs.
96 *
97 * @param _yLabel y-axis label
98 */
99 public void setYLabel(String _yLabel) {
100 yLabel = _yLabel;
101 }
102
103 /**
104 * Sets title of all graphs.
105 *
106 * @param _title Graph title
107 */
108 public void setTitle(String _title) {
109 title = _title;
110 }
111
112
113 /**
114 * Sets whether or not a grid (lines the height and breadth of the image indicating
115 * increment size) should be drawn on line graphs and bar graphs.
116 *
117 * @param _isGridOn whether a grid should be drawn
118 */
119 public void setGrid(boolean _isGridOn) {
120 isGridOn = _isGridOn;
121 }
122
123 /**
124 * Sets whether ticks (small marks on the x and y axis to indicate increment size)
125 * should be drawn on line graphs and bar graphs.
126 *
127 * @param _isTicksOn whether ticks should be drawn
128 */
129 public void setTicks(boolean _isTicksOn) {
130 isTicksOn = _isTicksOn;
131
132 }
133
134 /**
135 *
136 * @param _xVals array of x data values
137 */
138 public void setXVals(double _xVals[]) {
139 xVals = _xVals;
140 }
141
142 /**
143 * Sets Y data values - indicate height of bars for bar graphics.graph. Not set for pie.
144 *
145 * @param _yVals array of y data values
146 */
147 public void setYVals(double _yVals[]) {
148 yVals = _yVals;
149 }
150
151
152
153
154 //BEGIN GETTER METHODS
155
156 /**
157 * Gets image width.
158 *
159 * @return width image width (not frame width)
160 */
161
162 public int getWidth() {
163 return width;
164 }
165
166 /**
167 * Gets image height.
168 *
169 * @return height image height (not frame height)
170 */
171 public int getHeight() {
172 return height;
173 }
174
175
176 /**
177 * Gets x-axis label on bar graphs and line graphs.
178 *
179 * @return xLabel x-axis label
180 */
181 public String getXLabel() {
182 return xLabel;
183 }
184
185 /**
186 * Gets y-axis label on bar graphs and line graphs.
187 *
188 * @return yLabel y-axis label
189 */
190 public String getYLabel() {
191 return yLabel;
192 }
193
194 /**
195 * Gets title of all graphs.
196 *
197 * @return title Graph title
198 */
199 public String getTitle() {
200 return title;
201 }
202
203 /**
204 * Gets whether or not a grid (lines the height and breadth of the image indicating
205 * increment size) should be drawn on line graphs and bar graphs.
206 *
207 * @return isGridOn whether a grid should be drawn
208 */
209 public boolean isGridOn() {
210 return isGridOn;
211 }
212
213 /**
214 * Gets whether ticks (small marks on the x and y axis to indicate increment size)
215 * should be drawn on line graphs and bar graphs.
216 *
217 * @return isTicksOn whether ticks should be drawn
218 */
219 public boolean isTicksOn() {
220 return isTicksOn;
221 }
222
223 /**
224 * Gets X data values - the only values which are set for pie graphs.
225 *
226 * @return xVals array of x data values
227 */
228 public double[] getXVals() {
229 return xVals;
230 }
231
232 /**
233 * Gets Y data values - indicate height of bars for bar graphics.graph. Not set for pie.
234 *
235 * @return yVals array of y data values
236 */
237 public double[] getYVals() {
238 return yVals;
239 }
240
241
242 //BEGIN UTILITY METHODS
243
244 /**
245 * Calculates the maximum value in a given array.
246 *
247 * The array to find the maximum number of
248 * @return largestVal The maximum value contained in the array
249 * @see #getYAxisCoord
250 * @see #getXAxisCoord
251 */
252 protected double getMax(double vals[]) {
253 double largestVal = LITTLENUM;
254 for (int i = 0; i < vals.length; i++) {
255 if (vals[i] > largestVal) {
256 largestVal = vals[i];
257 }
258 }
259 return largestVal;
260 }
261
262
263 /**
264 * Calculates the maximum absolute value contained in a given array.
265 *
266 * @param vals The array whose absolute value maximum should be found
267 * @return largestVal The maximum absolute value contained in the array
268 * @see #getIncrementNew
269 */
270 protected double getAbsMax(double vals[]) {
271 double largestVal = LITTLENUM;
272 for (int i = 0; i < vals.length; i++) {
273 if (Math.abs(vals[i]) > largestVal) {
274 largestVal = Math.abs(vals[i]);
275 }
276 }
277
278 return largestVal;
279 }
280
281 /**
282 * Calculates the minimum value in a given array.
283 *
284 * @param vals The array to find the minimum number of
285 * @return smallestVal The minimum value contained in the array
286 * @see #getYAxisCoord
287 * @see #getXAxisCoord
288 */
289 protected double getMin(double vals[]) {
290 double smallestVal = BIGNUM;
291 for (int i = 0; i < vals.length; i++) {
292 if (vals[i] < smallestVal) {
293 smallestVal = vals[i];
294 }
295 }
296 return smallestVal;
297 }
298
299 /**
300 * Calculates the minimum absolute value contained in a given array.
301 *
302 * @param vals The array whose absolute value minimum should be found
303 * @return smallestVal The minimum absolute value contained in the array
304 * @see #getYAxisCoord
305 */
306 protected double getAbsMin(double vals[]) {
307 double smallestVal = BIGNUM;
308 for (int i = 0; i < vals.length; i++) {
309 if (Math.abs(vals[i]) < smallestVal) {
310 smallestVal = vals[i];
311 }
312 }
313 return smallestVal;
314 }
315
316 /**
317 * Calculates the difference between the two numbers passed to the function.
318 *
319 * @param min The number to subtract
320 * @param max The number to subtract from
321 * @return delta The difference between the two numbers
322 * @see #getIncrementNew
323 * @see #getNumTicks
324 * @see #getDeltaY
325 * @see #getDeltaX
326 */
327 protected double getDelta(double min, double max) {
328 double delta;
329 delta = max - min;
330 return delta;
331 }
332
333 /**
334 * Calculates the difference between the smallest and largest values in the
335 * array initialized as xVals
336 *
337 * @return deltaX The difference between the smallest X value and the largest
338 * @see #getXOrigin
339 * @see LineGraph
340 */
341 protected double getDeltaX() {
342 double deltaX = getDelta(getMin(getXVals()), getMax(getXVals()));
343 return deltaX;
344 }
345
346
347 /**
348 * Calculates the difference between the smallest and largest values in the
349 * array initialized as yVals
350 *
351 * @return deltaY The difference between the smallest Y value and the largest
352
353 * @see #getYOrigin
354 */
355 protected double getDeltaY() {
356 double deltaY = getDelta(getMin(yVals), getMax(yVals));
357 return deltaY;
358 }
359
360 /**
361 * Calculates the increment (building block) size for a given image size and delta
362 *
363 * @param size The available image size in this direction (width OR height)
364 * @param delta Distance between largest and smallest values along this axis
365 * @return increment The "building block" size
366
367 * @see #getXIncrement
368 * @see #getYIncrement
369 * @see #getNumTicks
370 */
371
372 protected double getIncrement(int size, double delta) {
373 double increment;
374 if (delta == 0) {
375 increment = 0;
376 } else {
377 increment = (size / delta);
378 }
379 return increment;
380 }
381
382 /**
383 * Calculates the increment (building block) size for a given image size and delta
384 * Overridden with an int delta for convenience' sake in drawing bar graphics.graph
385 *
386 * @param size The available image size in this direction (width OR height)
387 * @param delta Distance between largest and smallest values along this axis
388 * @return increment The "building block" size
389
390 */
391
392 protected int getIncrement(int size, int delta) {
393 int increment;
394 if (delta == 0) {
395 increment = 0;
396 } else {
397 increment = (size / delta);
398 }
399 return increment;
400 }
401
402
403 /**
404 * Calculates the increment (building block) size for a given image size and array of values
405 *
406 * @param vals The values to find the increment for
407 * @param size Image size in this direction
408 * @return increment The "building block" size
409 * @see LineGraph.#getXScreenCoords2
410 */
411
412 protected double getIncrementNew(double[] vals, int size) {
413 double absMax = getAbsMax(vals);
414 double max = getMax(vals);
415 double minNum = getMin(vals);
416 double increment;
417
418 if (absMax == 0) {
419 increment = 0;
420 } else if (minNum < 0 && max > 0) {
421 double delta = getDelta(minNum, getMax(vals));
422 increment = getIncrement(size, delta);
423 } else if (max < 0) {
424 increment = -(size / absMax);
425 } else {
426 increment = (size / absMax);
427 }
428 return increment;
429
430 }
431
432 /**
433 * Calculates the Y increment (building block) size, given the
434 * values for Y held in yVals and the image size held in height
435 *
436 * @return yIncrement The "building block" size
437 * @see LineGraph.#getYScreenCoords2
438 * @see #getYAxisCoord
439 * @see BarGraph.#drawGraph
440 * @see DrawUtil.#drawTicks
441 */
442 protected double getYIncrement() {
443 double yIncrement = getIncrement(getHeight(), getDeltaY());
444 return yIncrement;
445 }
446
447 /**
448 * Calculates the X increment (building block) size, given the
449 * values for X held in xVals and the image size held in width
450 *
451 * @return xIncrement The "building block" size
452 * @see LineGraph.#getYScreenCoords2
453 * @see #getXAxisCoord
454 */
455 protected double getXIncrement() {
456 double xIncrement = getIncrement(getWidth(), getDeltaX());
457 return xIncrement;
458 }
459
460
461 /**
462 * Calculates the x value of the x axis, correcting for the
463 * GUI offset. If the smallest X value is greater than zero, the X axis is simply
464 * placed at zero plus the offset. If the smallest X value is less than zero,
465 * x axis is placed to the right the amount of the smallest value.
466 *
467 * @param xVals The data which should be used to calculate the origin
468 * @return xOrigin The x-axis location
469 * @see LineGraph.#getXScreenCoords2
470 * @see #getXOrigin
471 * @see BarGraph.#drawGraph
472 * @see DrawUtil.#drawTicks
473 * @see DrawUtil.#drawAxes2
474 * @see DrawUtil.#drawYLabel
475 */
476
477 protected double getXAxisCoord() {
478 double deltaX = getDeltaX();
479 double xIncrement = getXIncrement();
480 double getMinX = getMin(xVals);
481 double getMaxX = getMax(xVals);
482
483 double xAxisCoord = (getMin(xVals) * xIncrement);
484 if (xAxisCoord >= 0) {
485 xAxisCoord = 0 + X_OFFSET;
486 } else if (getMinX < 0 && getMaxX > 0) {
487 xAxisCoord = Math.abs(xAxisCoord); //+X_OFFSET
488 } else {
489 xAxisCoord = width - X_OFFSET;
490 }
491 return xAxisCoord;
492
493 }
494
495
496 /**
497 * Calculates the y value of the y axis, correcting for the
498 * GUI offset. If the smallest Y value is greater than zero, the Y axis is simply
499 * placed at the height of the maximum data point times the increment (which
500 * works out to approximately the image height). If the smallest Y value is
501 * less than zero, the y axis is placed towards the center of the image
502 * the amount of the smallest value.
503 *
504 * @param yVals The data which should be used to calculate the origin
505 * @return yOrigin The y-axis location
506 * @see LineGraph.#getYScreenCoords2
507 * @see BarGraph.#getYCoord
508 * @see BarGraph.#drawGraph
509 * @see DrawUtil.#drawXLabel
510 */
511
512 protected double getYAxisCoord() {
513 double deltaY = getDeltaY();
514 double yIncrement = getYIncrement();
515 double minY = getMin(yVals);
516 double absMaxY = getAbsMax(yVals);
517 double maxY = getMax(yVals);
518
519 double yAxisCoord = (absMaxY * yIncrement);
520 if (minY < 0 && maxY > 0) {
521 yAxisCoord = yAxisCoord - getAbsMin(yVals);
522 } else if (minY < 0 && maxY < 0) {
523 yAxisCoord = 30;
524 }
525 return yAxisCoord;
526 }
527
528
529 /**
530 * Integer version of the getXOrigin method; used in draw methods which take
531 * integer arguments
532 * @return xOrigin the value of the x-axis x coordinate
533 * @see #getXOrigin(double)
534 * @see DrawUtil.#drawTicks
535 * @see DrawUtil.#drawAxes2
536 */
537 protected int getXOrigin() {
538 double xOrigin = getXAxisCoord();
539 return (int) xOrigin;
540 }
541
542 /**
543 * Integer version of the getYOrigin method; used in draw methods which take
544 * integer arguments
545 * @return yOrigin the value of the y-axis y coordinate
546 * @see #getYOrigin(double)
547 * @see DrawUtil.#drawTicks
548 * @see DrawUtil.#drawAxes2
549 */
550 protected int getYOrigin() {
551 double yOrigin = getYAxisCoord();
552 return (int) yOrigin;
553 }
554
555 /**
556 * Method used to calculate how many increments there are altogether, used
557 * for drawing ticks and grid
558 * @param vals The data that is being measured
559 * @param size The size (avaialble height OR width) of the image
560 * @return numIncrements The number of increments in this data in this size image
561 * @see DrawUtil.#drawTicks
562 * @see DrawUtil.#drawGrid
563 */
564
565 protected int getNumTicks(double[] vals, int size) {
566 double delta = getDelta(getMin(vals), getMax(vals));
567 double increment = getIncrement(size, delta);
568 int numIncrements = (int) (size / increment);
569
570 return numIncrements;
571
572 }
573
574
575 }
576
577
578