KickJava   Java API By Example, From Geeks To Geeks.

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


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
29
30 /**
31  * An y Axis for chart graphs. Supports both vertical and horizontal bar
32  * charts, low number of points line and scatter charts. Bullets can be either
33  * aligned with the labels as in vertical bar charts or between the labels as
34  * int horizontal bar charts. Label font size, type, style, and color are
35  * adjustable. Minimum gaps between labels, ticks, and labels and bullets
36  * may be specified. Ticks size and color may be specified. A title and
37  * its font may also be customized. The title may be justified within the
38  * width of the axis, bottom, top or center. Supports all of bordered areas
39  * customizability except for auto min sizing.
40  * If auto minimum sizing, then the title will likely be far apart from the
41  * axis. It does support everything with auto min sizing disabled. It is
42  * recommended that the auto justification be disabled. The yAxis must be
43  * butted up against a graph area, this is best done manually.
44  * This is its default.
45  */

46 final class YAxisArea extends AxisArea {
47
48
49   private VerticalTextListArea textList;
50   private Color ticksColor;
51   private int numTicks;
52   private boolean needsUpdate;
53
54
55   /**
56    * Creates a new YAxisArea with the following default settings:<br>
57    * setAutoSizes (false, false);<br>
58    * setAutoJustifys (false, false);<br>
59    * setTitleJustifications (LEFT, CENTER);<br>
60    * setTicksSizeModel (new Dimension (3, 3));<br>
61    * setType (LABELSBOTTOM);<br>
62    * setTextListJustifications (RIGHT, CENTER);<br>
63    * setLabels (new String[0]);<br>
64    * setTicksColor (Color.black);<br>
65    * setNumTicks (0);<br>
66    * setTitleRotateLeft (true);<br>
67    * setBackgroundExistence (false);<br>
68    * setBorderExistences (false, false, false, false);<br>
69    * setGapExistences (false, false, false, false);<br>
70    * textList.setBulletsRelation (TOP);<br>
71    * textList.setAutoJustifys (false, false);<br>
72    * textList.setBorderExistences (false, false, false, false);<br>
73    * textList.setGapExistences (false, false, false, false);<br>
74    * textList.setBackgroundExistence (false);<br>
75    * resetYAxisModel (true);<br>
76    */

77   YAxisArea() {
78
79     textList = new VerticalTextListArea();
80
81     setAutoSizes (false, false);
82     setAutoJustifys (false, false);
83     setTitleJustifications (LEFT, CENTER);
84     setTitleRotateLeft (true);
85     setBackgroundExistence (false);
86     setBorderExistence (false);
87     setGapExistence (false);
88     setTicksSizeModel (new Dimension (3, 3));
89     setType (LABELSBOTTOM);
90     setTicksColor (Color.black);
91     setNumTicks (0);
92     textList.setBulletsRelation (TOP);
93     textList.setAutoJustifys (false, false);
94     textList.setBorderExistence (false);
95     textList.setGapExistence (false);
96     textList.setBackgroundExistence (false);
97     textList.setBulletsOutline (false);
98     resetYAxisModel (true);
99     needsUpdate = true;
100   }
101
102
103   /**
104    * Specifies the type of the y Axis. The type refers to what type of chart
105    * it supports. In any chart the objects are plotted against one side,
106    * the values side, the values side has raw numbers like 0, 100, 200, etc.
107    * The other side, the labels side, has the labels for the data being plotted,
108    * like September, November, etc. If the labels side is along the bottom
109    * of the graph, then set the type to LABELSBOTTOM. If the labels side is
110    * along the left/right side of the graph then set the type to LABELSLEFT.
111    * @param type Whether this axis is used for labels bottom or labels left
112    * applications.
113    */

114   final void setType (int type) {
115
116     needsUpdate = true;
117     if (type == LABELSBOTTOM) textList.setBulletsAlignment (CENTERED);
118     else textList.setBulletsAlignment (BETWEEN);
119   }
120
121
122   /**
123    * The color for the ticks.
124    * @param color The y Axis tick color.
125    */

126   final void setTicksColor (Color color) {
127
128     needsUpdate = true;
129     ticksColor = color;
130   }
131
132
133   /**
134    * The vertical alignment of the ticks respective to the labels. The
135    * bullets can either be place in line with each label, or in in line with
136    * the middle of the space between each label. That is, bullets can be
137    * centered in the middle of the label, or placed between each label.
138    * @param alignment With values of either Area.CENTERED or Area.BETWEEN
139    */

140   final void setTicksAlignment (int alignment) {
141
142     textList.setBulletsAlignment (alignment);
143   }
144
145
146  /**
147    * The number of ticks should be equal to the number of x axis labels at all
148    * times, EXCEPT with a type of chart with a (LABELSBOTTOM x axis and a
149    * graph components alignment is true).
150    * @param num The number of y axis ticks.
151    */

152   final void setNumTicks (int num) {
153
154     needsUpdate = true;
155     numTicks = num;
156   }
157
158
159   /**
160    * The model size for the ticks. If auto maximum sizing is enabled,
161    * then the actual tick size can grow and shrink; in this case a ratio based
162    * on the maximum area size / model area size is computed and applied to the
163    * model size in order to find the actual size. With maximum sizing
164    * disabled, the actual size is the model size.
165    * @param size The model size for the ticks. [Do not pass null]
166    */

167   final void setTicksSizeModel (Dimension size) {
168
169     needsUpdate = true;
170     textList.setBulletsSizeModel (size);
171   }
172
173
174   /**
175    * The model size for the ticks. If auto maximum sizing is enabled,
176    * then the actual tick size can grow and shrink; in this case a ratio based
177    * on the maximum area size / model area size is computed and applied to the
178    * model size in order to find the actual size. With maximum sizing
179    * disabled, the actual size is the model size.
180    * @return The model size for the ticks. [Do not pass null]
181    */

182   final Dimension getTicksSizeModel() {
183     return textList.getBulletsSizeModel();
184   }
185
186
187   /**
188    * The existence of a gap between each label and the next. If the gap does
189    * not exist, then it will not be used in calculations.
190    * @param existence The existence of a gap between each label and the next. If
191    * true, then they do.
192    */

193   final void setBetweenLabelsGapExistence (boolean existence) {
194
195     needsUpdate = true;
196     textList.setBetweenLabelsGapExistence (existence);
197   }
198
199
200   /**
201    * The existence of a gap between each tick and the next. If the gap does
202    * not exist, then it will not be used in calculations.
203    * @param existence The existence of a gap between each tick and the next. If
204    * true, then they do.
205    */

206   final void setBetweenTicksGapExistence (boolean existence) {
207
208     needsUpdate = true;
209     textList.setBetweenBulletsGapExistence (existence);
210   }
211
212
213   /**
214    * The model thickness for the gap between each tick. If auto maximum sizing
215    * is enabled,
216    * then the actual thickness size can grow and shrink; in this case a ratio
217    * based
218    * on the maximum area size / model area size is computed and applied to the
219    * model thickness in order to find the actual thickness. With maximum sizing
220    * disabled, the actual thickness is the model thickness.
221    * @param gap The model thickness for the gap between each tick.
222    */

223   final void setBetweenTicksGapThicknessModel (int gap) {
224
225     needsUpdate = true;
226     textList.setBetweenBulletsGapThicknessModel (gap);
227   }
228
229
230   /**
231    * The existence of a gap between the row of labels and the row of ticks. If
232    * the gap does
233    * not exist, then it will not be used in calculations.
234    * @param existence The existence of a gap between the labels and ticks. If
235    * true, then they do.
236    */

237   final void setBetweenTicksAndLabelsGapExistence (boolean existence) {
238
239     needsUpdate = true;
240     textList.setBetweenBulletsAndLabelsGapExistence (existence);
241   }
242
243
244   /**
245    * The model thickness for the gap between the row of labels and the row of
246    * ticks. If auto maximum sizing
247    * is enabled,
248    * then the actual thickness size can grow and shrink; in this case a ratio
249    * based
250    * on the maximum area size / model area size is computed and applied to the
251    * model thickness in order to find the actual thickness. With maximum sizing
252    * disabled, the actual thickness is the model thickness. This thickness
253    * is not used in calculations if either the ticks or labels do not exist.
254    * @param gap The model thickness for the gap between the labels and ticks.
255    */

256   final void setBetweenTicksAndLabelsGapThicknessModel (int gap) {
257
258     needsUpdate = true;
259     textList.setBetweenBulletsAndLabelsGapThicknessModel (gap);
260   }
261
262
263   /**
264    * Returns the Text List Area for this Y Axis.
265    * Get for advanced configuration of the y axis.
266    * @return This y axis component.
267    */

268   final VerticalTextListArea getTextList() {
269
270     return textList;
271   }
272
273
274   /**
275    * The labels of the axis. The lowest order array label is the top
276    * most label.
277    * @param g2D The graphics context to use for calculations.
278    * @return The text labels. This will never be null.
279    */

280   final TextArea[] getLabels (Graphics2D g2D) {
281
282     updateYAxisArea (g2D);
283     return textList.getLabels (g2D);
284   }
285
286
287   /**
288    * The bounds of the ticks. The bounds of the ticks specify the locations
289    * and sizes of each actual tick. The lowest order array tick is the top
290    * most tick.
291    * @param g2D The graphics context to use for calculations.
292    * @return The bounds of the ticks. This will never be null.
293    */

294   final Rectangle[] getTicks (Graphics2D g2D) {
295
296     updateYAxisArea (g2D);
297     return textList.getBullets (g2D);
298   }
299
300
301   /**
302    * Returns the model thickness of the gap between the ticks.
303    * @return The thickness.
304    */

305   final int getBetweenTicksGapThicknessModel() {
306
307     return textList.getBetweenBulletsGapThicknessModel();
308   }
309
310
311   /**
312    * Returns the color of the ticks.
313    * @return The color.
314    */

315   final Color getTicksColor() {
316
317     return ticksColor;
318   }
319
320
321   /**
322    * Returns how the ticks are aligned with respect to the labels.
323    * @return int With values of either Area.CENTERED or Area.BETWEEN
324    */

325   final int getTicksAlignment() {
326
327     return textList.getBulletsAlignment();
328   }
329
330
331   /**
332    * Indicates whether some property of this class has changed.
333    * @return True if some property has changed.
334    */

335   final boolean getYAxisAreaNeedsUpdate() {
336
337     return (needsUpdate || getTitledAreaNeedsUpdate() ||
338       textList.getVerticalTextListAreaNeedsUpdate());
339   }
340
341
342   /**
343    * Updates this parent's variables, and this' variables.
344    * @param g2D The graphics context to use for calculations.
345    */

346   final void updateYAxisArea (Graphics2D g2D) {
347
348     if (getYAxisAreaNeedsUpdate()) {
349       updateTitledArea (g2D);
350       update (g2D);
351       textList.updateVerticalTextListArea(g2D);
352     }
353     needsUpdate = false;
354   }
355
356
357   /**
358    * Resets the model for this class. The model is used for shrinking and
359    * growing of its components based on the maximum size of this class. If this
360    * method is called, then the next time the maximum size is set, this classes
361    * model maximum size will be made equal to the new maximum size. Effectively
362    * what this does is ensure that whenever this objects maximum size is equal
363    * to the one given, then all of the components will take on their default
364    * model sizes. Note: This is only useful when auto model max sizing is
365    * disabled.
366    * @param reset True causes the max model size to be set upon the next max
367    * sizing.
368    */

369   final void resetYAxisModel (boolean reset) {
370
371     needsUpdate = true;
372     resetTitledAreaModel (reset);
373     textList.resetVerticalTextListAreaModel (reset);
374   }
375
376
377   /**
378    * Paints all the components of this class. First all variables are updated.
379    * @param g2D The graphics context for calculations and painting.
380    */

381   final void paintComponent (Graphics2D g2D) {
382
383     updateYAxisArea (g2D);
384     super.paintComponent (g2D);
385     textList.paintComponent (g2D);
386   }
387
388
389   private void update (Graphics2D g2D) {
390     textList.setCustomRatio (WIDTH, true, getRatio (WIDTH));
391     textList.setCustomRatio (HEIGHT, true, getRatio (HEIGHT));
392     updateTickColors();
393     updateMaxTextList (g2D);
394     textList.updateVerticalTextListArea (g2D);
395     updateMinSizes (g2D);
396     updateMinTextList (g2D);
397   }
398
399
400   private void updateTickColors() {
401
402     Color[] tickColors = new Color[numTicks];
403     for (int i = 0; i < numTicks; ++i) {
404
405       tickColors[i] = ticksColor;
406     }
407
408     textList.setBulletColors (tickColors);
409   }
410
411
412   private void updateMaxTextList (Graphics2D g2D) {
413
414     textList.setAllowSelfSize (true);
415     textList.setAutoSizes (getAutoSize (MAXMODEL), false);
416     Rectangle maxBounds = getMaxEntitledSpaceBounds (g2D);
417     textList.setSize (MAX, maxBounds.getSize());
418     textList.setSizeLocation (MAX, maxBounds.getLocation());
419   }
420
421
422   private void updateMinSizes (Graphics2D g2D) {
423
424     if (!getAutoSize(MIN)) {
425       Dimension titleSize = getTitleSize (MIN, g2D);
426       Dimension textListSize = textList.getSize (MIN);
427       int minWidth;
428       if (titleSize.width > 0) {
429         minWidth = titleSize.width + getBetweenTitleAndSpaceGapThickness (g2D) +
430           textListSize.width;
431       }
432       else minWidth = titleSize.width + textListSize.width;
433       int minHeight = titleSize.height > textListSize.height ?
434         titleSize.height : textListSize.height;
435       setSpaceSize (MIN, new Dimension (minWidth, minHeight));
436     }
437   }
438
439
440   private void updateMinTextList (Graphics2D g2D) {
441
442     int spaceHeight = textList.getSize (MIN).height;
443     textList.setAllowSelfSize (false);
444     spaceHeight = spaceHeight < getSpaceSize(MIN).height ?
445       getSpaceSize(MIN).height : spaceHeight;
446     int betweenHeight =
447       textList.getSize (MIN).height - textList.getSpaceSize (MIN).height;
448     spaceHeight = spaceHeight - betweenHeight;
449     textList.setSpaceSize (MIN,
450       new Dimension (textList.getSpaceSize(MIN).width, spaceHeight));
451
452     int spaceY = getSpaceSizeLocation(MIN).y;
453     int spaceX;
454     int betweenWidth =
455       textList.getSize (MIN).width - textList.getSpaceSize (MIN).width;
456     if (textList.getJustifications (HORIZONTAL) == LEFT)
457       spaceX = getSpaceSizeLocation (MIN).x + getTitleSize (MIN, g2D).width +
458         getBetweenTitleAndSpaceGapThickness (g2D) +
459         textList.getSpaceSize (MIN).width + betweenWidth / 2;
460     else
461       spaceX = getSpaceSizeLocation (MIN).x + getSpaceSize (MIN).width -
462         textList.getSize (MIN).width + betweenWidth / 2;
463
464     textList.setSpaceSizeLocation (MIN, new Point (spaceX, spaceY));
465   }
466 }
Popular Tags