KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chart2d > GraphChartArea


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 package net.sourceforge.chart2d;
25
26
27 import java.awt.*;
28 import java.util.*;
29
30
31 /**
32  * A container containing information and components for cartesian coordinate
33  * charts. That is, charts that are plotted on a parralelogram surface.
34  */

35 class GraphChartArea extends ChartArea {
36
37
38   private Vector datasetVector;
39   private Vector graphVector;
40
41   private XAxisArea xAxis;
42   private YAxisArea yAxis;
43   private Rectangle maxBounds;
44   private Rectangle minBounds;
45
46   private int numPlotAxisLabels;
47
48   private float yAxisToWidthRatio;
49   private float yAxisToHeightRatio;
50
51   private float xAxisToWidthRatio;
52   private float xAxisToHeightRatio;
53
54   private float graphToWidthRatio;
55   private float graphToHeightRatio;
56   private float graphableToAvailableRatio;
57
58   private boolean graphLinesThicknessAssociation;
59   private boolean graphHorizontalLinesExistence;
60   private int graphHorizontalLinesThicknessModel;
61   private float[] graphHorizontalLinesStyle;
62   private Color graphHorizontalLinesColor;
63
64   private boolean graphVerticalLinesExistence;
65   private int graphVerticalLinesThicknessModel;
66   private float[] graphVerticalLinesStyle;
67   private Color graphVerticalLinesColor;
68
69   private boolean customizeGreatestValue;
70   private float customGreatestValue;
71   private boolean customizeLeastValue;
72   private float customLeastValue;
73
74   private float numbersAxisRangeHigh;
75   private float numbersAxisRangeLow;
76
77   private int numbersAxisLabelsType;
78   private Vector warningRegions;
79
80   private boolean graphComponentsColoringByCat;
81   private Color[] graphComponentsColorsByCat;
82
83   private boolean needsUpdate;
84
85
86   /**
87    * Creates a Graph Chart Area with the default values of a TitledArea,
88    * and its own default values.
89    */

90   GraphChartArea() {
91
92     xAxis = new XAxisArea();
93     yAxis = new YAxisArea();
94     maxBounds = new Rectangle();
95     minBounds = new Rectangle();
96     needsUpdate = true;
97
98     setNumPlotAxisLabels (5);
99     setCustomGreatestValue (false, 0f);
100     setCustomLeastValue (false, 0f);
101     setGraphableToAvailableRatio (.95f);
102
103     setYAxisToWidthRatio (.25f);
104     setXAxisToWidthRatio (.50f);
105     setGraphToWidthRatio (.50f);
106
107     setYAxisToHeightRatio (.75f);
108     setGraphToHeightRatio (.75f);
109     setXAxisToHeightRatio (.25f);
110
111     setWarningRegions (new Vector (0, 0));
112
113     setGraphComponentsColoringByCat(false);
114     setGraphComponentsColorsByCat (new Color[0]);
115
116     resetGraphChartAreaModel (true);
117   }
118
119
120   /**
121    * Sets whether the graph components are colored by category or by set.
122    * @param b If true, then colored by category.
123    */

124   final void setGraphComponentsColoringByCat (boolean b) {
125
126     needsUpdate = true;
127     graphComponentsColoringByCat = b;
128   }
129
130
131   /**
132    * Sets the color array for the graph component coloring.
133    * @param colors The color array for the category coloring.
134    */

135   final void setGraphComponentsColorsByCat (Color[] colors) {
136
137     needsUpdate = true;
138     graphComponentsColorsByCat = colors;
139   }
140
141
142   /**
143    * Sets the warning regions vector for applying to each graph area.
144    * @param v the warning regions vector.
145    */

146   final void setWarningRegions (Vector v) {
147
148     warningRegions = v;
149     needsUpdate = true;
150   }
151
152
153
154   /**
155    * Sets the dataset vector.
156    * @param vector A vector of datasets.
157    */

158   final void setDatasetVector (Vector vector) {
159
160     needsUpdate = true;
161     datasetVector = vector;
162   }
163
164
165   /**
166    * Sets the number of labels for the plot axis. For every graph chart, there
167    * are two axes. One axis you specify the labels (ex. June, July, August);
168    * the other axis, this library figures out the labels (ex. 250 or 1.95).
169    * The axis the library labels is the plot axis.
170    * @param num The number of plot axis labels.
171    */

172   final void setNumPlotAxisLabels (int num) {
173
174     needsUpdate = true;
175     numPlotAxisLabels = num;
176   }
177
178
179   /**
180    * Specifies how much of the maximum width less borders and gaps to make
181    * availalble to the y axis maximum size.
182    * @param ratio The ratio to the width, to make available to the y axis.
183    */

184   final void setYAxisToWidthRatio (float ratio) {
185
186     needsUpdate = true;
187     yAxisToWidthRatio = ratio;
188   }
189
190
191   /**
192    * Specifies how much of the maximum height less borders, gaps, and title to
193    * make availalble to the y axis maximum size.
194    * @param ratio The ratio to the height, to make available to the y axis.
195    */

196   final void setYAxisToHeightRatio (float ratio) {
197
198     needsUpdate = true;
199     yAxisToHeightRatio = ratio;
200   }
201
202
203   /**
204    * Specifies how much of the maximum width less borders and gaps to make
205    * availalble to the x axis maximum size.
206    * @param ratio The ratio to the width, to make available to the x axis.
207    */

208   final void setXAxisToWidthRatio (float ratio) {
209
210     needsUpdate = true;
211     xAxisToWidthRatio = ratio;
212   }
213
214
215   /**
216    * Specifies how much of the maximum height less borders, gaps, and title to
217    * make availalble to the x axis maximum size.
218    * @param ratio The ratio to the height, to make available to the x axis.
219    */

220   final void setXAxisToHeightRatio (float ratio) {
221
222     needsUpdate = true;
223     xAxisToHeightRatio = ratio;
224   }
225
226
227   /**
228    * Specifies how much of the maximum width less borders and gaps to make
229    * availalble to the graph maximum size.
230    * @param ratio The ratio to the width, to make available to the graph.
231    */

232   final void setGraphToWidthRatio (float ratio) {
233
234     needsUpdate = true;
235     graphToWidthRatio = ratio;
236   }
237
238
239   /**
240    * Specifies how much of the maximum height less borders, gaps, and title to
241    * make availalble to the graph maximum size.
242    * @param ratio The ratio to the height, to make available to the graph.
243    */

244   final void setGraphToHeightRatio (float ratio) {
245
246     needsUpdate = true;
247     graphToHeightRatio = ratio;
248   }
249
250
251   /**
252    * Influences the plot axis' label with the highest value.
253    * Does this by tricking chart2d into thinking that the passed value is the
254    * largest value in the dataset.
255    * Then if setGraphableToAvailableRatio (1f), the plot axis' label with the
256    * the highest value will be the custom greatest value.
257    * So, in order to control what the highest value plot axis label is, then
258    * use this method, the setGraphableToAvailableRatio method, and the
259    * setLabelsPrecisionNum method.
260    * Note: If there are both positive and negative numbers in the dataset
261    * then you may need to set both custom greatest and custom least methods
262    * since labels are always equally far from zero.
263    * @param customize Whether this value is used.
264    * @param value The value to use.
265    */

266   final void setCustomGreatestValue (boolean customize, float value) {
267
268     needsUpdate = true;
269     customizeGreatestValue = customize;
270     customGreatestValue = value;
271   }
272
273
274   /**
275    * Influences the plot axis' label with the lowest value.
276    * Does this by tricking chart2d into thinking that the passed value is the
277    * largest value in the dataset.
278    * Then if setGraphableToAvailableRatio (1f), the plot axis' label with the
279    * the highest value will be the custom greatest value.
280    * So, in order to control what the highest value plot axis label is, then
281    * use this method, the setGraphableToAvailableRatio method, and the
282    * setLabelsPrecisionNum method.
283    * Note: If there are both positive and negative numbers in the dataset
284    * then you may need to set both custom greatest and custom least methods
285    * since labels are always equally far from zero.
286    * @param customize Whether this value is used.
287    * @param value The value to use.
288    */

289   final void setCustomLeastValue (boolean customize, float value) {
290
291     needsUpdate = true;
292     customizeLeastValue = customize;
293     customLeastValue = value;
294   }
295
296
297   /**
298    * Specifies how much of he height <b>of the graph</b> to make available to
299    * the components plot area. If this ratio is set to one, then the highest
300    * value of all the data sets will touch the top of the graph area.
301    * @param ratio The ratio of the graph height to the greatest value in the
302    * data set. [Must be between 0.0 and 1.0]
303    */

304   final void setGraphableToAvailableRatio (float ratio) {
305
306     needsUpdate = true;
307     graphableToAvailableRatio = ratio;
308   }
309
310
311   /**
312    * Gets whether the graph components are colored by category or by set.
313    * @return If true, then colored by category.
314    */

315   final boolean getGraphComponentsColoringByCat() {
316     return graphComponentsColoringByCat;
317   }
318
319
320   /**
321    * Gets the color array for the graph component coloring.
322    * @return The color array for the category coloring.
323    */

324   final Color[] getGraphComponentsColorsByCat() {
325     return graphComponentsColorsByCat;
326   }
327
328
329   /**
330    * Gets the warning regions vector for applying to each graph area.
331    * @return the warning regions vector.
332    */

333   final Vector getWarningRegions() {
334     return warningRegions;
335   }
336
337
338   /**
339    * Returns all the Dataset objects added to this object.
340    * @return Vector The Dataset objects.
341    */

342   final Vector getDatasetVector() {
343     return datasetVector;
344   }
345
346
347   /**
348    * Returns the x Axis in order to allow customizations of it.
349    * @return The x axis of this chart.
350    */

351   final XAxisArea getXAxis() {
352
353     return xAxis;
354   }
355
356
357   /**
358    * Returns the y axis in order to allow customization of it.
359    * @return The y axis of this chart.
360    */

361   final YAxisArea getYAxis() {
362
363     return yAxis;
364   }
365
366
367   /**
368    * Sets the vector of graphs for this chart.
369    * @param vector The vector of graphs.
370    */

371   final void setGraphVector (Vector vector) {
372
373     needsUpdate = true;
374     graphVector = vector;
375   }
376
377
378   /**
379    * Returns a vector with all the graphs that were added by the addGraph method.
380    * @return Vector A vector of graphs.
381    */

382   final Vector getGraphVector() {
383
384     return graphVector;
385   }
386
387
388   /**
389    * Gets the number of labels for the plot axis. For every graph chart, there
390    * are two axes. One axis you specify the labels (ex. June, July, August);
391    * the other axis, this library figures out the labels (ex. 250 or 1.95).
392    * The axis the library labels is the plot axis.
393    * @return The number of plot axis labels.
394    */

395   final int getNumPlotAxisLabels() {
396
397     return numPlotAxisLabels;
398   }
399
400
401   /**
402    * Returns this property in order for subclasses to have access to it.
403    * @return The specified property.
404    */

405   final float getYAxisToWidthRatio() {
406
407     return yAxisToWidthRatio;
408   }
409
410
411   /**
412    * Returns this property in order for subclasses to have access to it.
413    * @return The specified property.
414    */

415   final float getYAxisToHeightRatio() {
416
417     return yAxisToHeightRatio;
418   }
419
420
421   /**
422    * Returns this property in order for subclasses to have access to it.
423    * @return The specified property.
424    */

425   final float getXAxisToWidthRatio() {
426
427     return xAxisToWidthRatio;
428   }
429
430
431   /**
432    * Returns this property in order for subclasses to have access to it.
433    * @return The specified property.
434    */

435   final float getXAxisToHeightRatio() {
436
437     return xAxisToHeightRatio;
438   }
439
440
441   /**
442    * Returns this property in order for subclasses to have access to it.
443    * @return The specified property.
444    */

445   float getGraphToWidthRatio() {
446
447     return graphToWidthRatio;
448   }
449
450
451   /**
452    * Returns this property in order for subclasses to have access to it.
453    * @return The specified property.
454    */

455   final float getGraphToHeightRatio() {
456
457     return graphToHeightRatio;
458   }
459
460
461   /**
462    * Gets the boolean value passed to setCustomGreatestValue (boolean, float).
463    * @return True if the max value is being customized.
464    */

465   final boolean getCustomizeGreatestValue() {
466
467     return customizeGreatestValue;
468   }
469
470
471   /**
472    * Gets the float value passed to setCustomGreatestValue (boolean, float).
473    * @return The max value.
474    */

475   final float getCustomGreatestValue() {
476
477     return customGreatestValue;
478   }
479
480
481   /**
482    * Gets the boolean value passed to setCustomLeastValue (boolean, float).
483    * @return True if the min value is being customized.
484    */

485   final boolean getCustomizeLeastValue() {
486
487     return customizeLeastValue;
488   }
489
490
491   /**
492    * Gets the float value passed to setCustomLeastValue (boolean, float).
493    * @return The min value.
494    */

495   final float getCustomLeastValue() {
496
497     return customLeastValue;
498   }
499
500
501   /**
502    * Returns this property in order for subclasses to have access to it.
503    * @return The specified property.
504    */

505   final float getGraphableToAvailableRatio() {
506
507     return graphableToAvailableRatio;
508   }
509
510
511   /**
512    * Indicates whether some property of this class has changed.
513    * @return boolean true if some property has changed.
514    */

515   final boolean getGraphChartAreaNeedsUpdate() {
516
517     for (int i = 0; i < warningRegions.size(); ++i) {
518       needsUpdate = needsUpdate ||
519         ((WarningRegion)warningRegions.get (i)).getWarningRegionNeedsUpdate();
520     }
521     return (needsUpdate || getChartAreaNeedsUpdate() ||
522     xAxis.getXAxisAreaNeedsUpdate() || yAxis.getYAxisAreaNeedsUpdate());
523   }
524
525
526   /**
527    * Resets the model for this class. The model is used for shrinking and
528    * growing of its components based on the maximum size of this class. If this
529    * method is called, then the next time the maximum size is set, this classes
530    * model maximum size will be made equal to the new maximum size. Effectively
531    * what this does is ensure that whenever this objects maximum size is equal
532    * to the one given, then all of the components will take on their default
533    * model sizes. Note: This is only useful when auto model max sizing is
534    * disabled.
535    * @param reset True resets the max model upon the next max sizing.
536    */

537   final void resetGraphChartAreaModel (boolean reset) {
538
539     needsUpdate = true;
540     resetChartAreaModel (reset);
541     xAxis.resetXAxisModel (reset);
542     yAxis.resetYAxisModel (reset);
543   }
544
545
546   /**
547    * Updates this parent's variables, and this' variables.
548    * @param g2D The graphics context to use for calculations.
549    */

550   final void updateGraphChartArea (Graphics2D g2D) {
551
552     if (getGraphChartAreaNeedsUpdate()) {
553       updateChartArea (g2D);
554       //update warning regions after graphs are updated
555
}
556     needsUpdate = false;
557   }
558
559
560   /**
561    * Updates this parent's variables, and this' variables.
562    * @param g2D The graphics context to use for calculations.
563    */

564   void paintComponent (Graphics2D g2D) {
565
566     updateGraphChartArea (g2D);
567     super.paintComponent (g2D);
568     for (int i = 0; i < warningRegions.size(); ++i) {
569       ((WarningRegion)warningRegions.get (i)).paintComponent (g2D);
570     }
571     xAxis.paintComponent (g2D);
572     yAxis.paintComponent (g2D);
573   }
574 }
Popular Tags