KickJava   Java API By Example, From Geeks To Geeks.

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


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 x 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 horizontal bar charts or between the labels as
34  * int vertical 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 withing the
38  * width of the axis, left, right or center.
39  * Supports all of bordered areas 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 xAxis must be
43  * butted up against a graph area, this is best done manually.
44  * This is its default.
45  */

46 final class XAxisArea extends AxisArea {
47
48
49   private HorizontalTextListArea textList;
50   private Color ticksColor;
51   private int numTicks;
52   private boolean needsUpdate;
53
54   /**
55    * Creates a new xAxis Area with the default settings.
56    */

57   XAxisArea() {
58
59     textList = new HorizontalTextListArea();
60
61     setAutoSizes (false, false);
62     setAutoJustifys (false, false);
63     setTitleJustifications (CENTER, BOTTOM);
64     setBackgroundExistence (false);
65     setBorderExistence (false);
66     setGapExistence (false);
67
68     setTicksSizeModel (new Dimension (3, 3));
69     setType (LABELSBOTTOM);
70     setTicksColor (Color.black);
71     setNumTicks (0);
72
73     textList.setBulletsRelation (TOP);
74     textList.setAutoJustifys (false, false);
75     textList.setBorderExistence (false);
76     textList.setGapExistence (false);
77     textList.setBackgroundExistence (false);
78     textList.setBulletsOutline (false);
79
80     resetXAxisModel (true);
81     needsUpdate = true;
82   }
83
84
85   /**
86    * Specifies the type of the x Axis. The type refers to what type of chart
87    * it supports. In any chart the objects are plotted against one side,
88    * the values side, the values side has raw numbers like 0, 100, 200, etc.
89    * The other side, the labels side, has the labels for the data being plotted,
90    * like September, November, etc. If the labels side is along the bottom
91    * of the graph, then set the type to LABELSBOTTOM. If the labels side is
92    * along the left/right side of the graph then set the type to LABELSLEFT.
93    * @param type Whether this axis is used for labels bottom or labels left
94    * applications.
95    */

96   final void setType (int type) {
97
98     needsUpdate = true;
99     if (type == LABELSBOTTOM) textList.setBulletsAlignment (BETWEEN);
100     else textList.setBulletsAlignment (CENTERED);
101   }
102
103
104   /**
105    * The color for the ticks.
106    * @param color The x Axis tick color.
107    */

108   final void setTicksColor (Color color) {
109
110     needsUpdate = true;
111     ticksColor = color;
112   }
113
114
115   /**
116    * The horizontal alignment of the ticks respective to the labels. The
117    * bullets can either be place in line with each label, or in in line with
118    * the middle of the space between each label. That is, bullets can be
119    * centered in the middle of the label, or placed between each label.
120    * @param alignment With values of either Area.CENTERED or Area.BETWEEN
121    */

122   final void setTicksAlignment (int alignment) {
123
124     textList.setBulletsAlignment (alignment);
125   }
126
127
128  /**
129    * The number of ticks should be equal to the number of x axis labels at all
130    * times, EXCEPT with a type of chart with a (LABELSBOTTOM x axis and a
131    * graph components alignment is true).
132    * @param num The number of x axis ticks.
133    */

134   final void setNumTicks (int num) {
135
136     needsUpdate = true;
137     numTicks = num;
138   }
139
140
141   /**
142    * The model size for the ticks. If auto maximum sizing is enabled,
143    * then the actual tick size can grow and shrink; in this case a ratio based
144    * on the maximum area size / model area size is computed and applied to the
145    * model size in order to find the actual size. With maximum sizing
146    * disabled, the actual size is the model size.
147    * @param size The model size for the ticks. [Do not pass null]
148    */

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

164   final Dimension getTicksSizeModel() {
165     return textList.getBulletsSizeModel();
166   }
167
168
169   /**
170    * The existence of a gap between each tick and the next. If the gap does
171    * not exist, then it will not be used in calculations.
172    * @param existence The existence of a gap between each tick and the next. If
173    * true, then they do.
174    */

175   final void setBetweenTicksGapExistence (boolean existence) {
176
177     needsUpdate = true;
178     textList.setBetweenBulletsGapExistence (existence);
179   }
180
181
182   /**
183    * The model thickness for the gap between each tick. If auto maximum sizing
184    * is enabled,
185    * then the actual thickness size can grow and shrink; in this case a ratio
186    * based
187    * on the maximum area size / model area size is computed and applied to the
188    * model thickness in order to find the actual thickness. With maximum sizing
189    * disabled, the actual thickness is the model thickness.
190    * @param gap The model thickness for the gap between each tick.
191    */

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

206   final void setBetweenTicksAndLabelsGapExistence (boolean existence) {
207
208     needsUpdate = true;
209     textList.setBetweenBulletsAndLabelsGapExistence (existence);
210   }
211
212
213   /**
214    * The model thickness for the gap between the row of labels and the row of
215    * ticks. If auto maximum sizing
216    * is enabled,
217    * then the actual thickness size can grow and shrink; in this case a ratio
218    * based
219    * on the maximum area size / model area size is computed and applied to the
220    * model thickness in order to find the actual thickness. With maximum sizing
221    * disabled, the actual thickness is the model thickness. This thickness
222    * is not used in calculations if either the ticks or labels do not exist.
223    * @param gap The model thickness for the gap between the labels and ticks.
224    */

225   final void setBetweenTicksAndLabelsGapThicknessModel (int gap) {
226
227     needsUpdate = true;
228     textList.setBetweenBulletsAndLabelsGapThicknessModel (gap);
229   }
230
231
232   /**
233    * Returns the HorizontalTextListArea for this X Axis.
234    * Get for standard configuration of the x axis.
235    * @return This x axis component.
236    */

237   final HorizontalTextListArea getTextList() {
238
239     return textList;
240   }
241
242
243   /**
244    * The labels of the axis. The lowest order array label is the top
245    * most label.
246    * @param g2D The graphics context to use for calculations.
247    * @return The text labels; this will never be null.
248    */

249   final TextArea[] getLabels (Graphics2D g2D) {
250
251     updateXAxisArea (g2D);
252     return textList.getLabels (g2D);
253   }
254
255
256   /**
257    * The bounds of the ticks. The bounds of the ticks specify the locations
258    * and sizes of each actual tick. The lowest order array tick is the left
259    * most tick.
260    * @param g2D The graphics context to use for calculations.
261    * @return The bounds of the ticks. This will never be null.
262    */

263   final Rectangle[] getTicks (Graphics2D g2D) {
264
265     updateXAxisArea (g2D);
266     return textList.getBullets (g2D);
267   }
268
269
270   /**
271    * Returns the model thickness of the gap between the ticks.
272    * @return The thickness.
273    */

274   final int getBetweenTicksGapThicknessModel() {
275
276     return textList.getBetweenBulletsGapThicknessModel();
277   }
278
279
280   /**
281    * Returns the color of the ticks.
282    * @return The color.
283    */

284   final Color getTicksColor() {
285
286     return ticksColor;
287   }
288
289
290   /**
291    * Returns how the ticks are aligned with respect to the labels.
292    * @return int With values of either Area.CENTERED or Area.BETWEEN
293    */

294   final int getTicksAlignment() {
295
296     return textList.getBulletsAlignment();
297   }
298
299
300   /**
301    * Indicates whether some property of this class has changed.
302    * @return True if some property has changed.
303    */

304   final boolean getXAxisAreaNeedsUpdate() {
305
306     return (needsUpdate || getTitledAreaNeedsUpdate() ||
307       textList.getHorizontalTextListAreaNeedsUpdate());
308   }
309
310
311   /**
312    * Updates this parent's variables, and this' variables.
313    * @param g2D The graphics context to use for calculations.
314    */

315   final void updateXAxisArea (Graphics2D g2D) {
316
317     if (getXAxisAreaNeedsUpdate()) {
318       updateTitledArea (g2D);
319       update (g2D);
320       textList.updateHorizontalTextListArea(g2D);
321     }
322     needsUpdate = false;
323   }
324
325
326   /**
327    * Resets the model for this class. The model is used for shrinking and
328    * growing of its components based on the maximum size of this class. If this
329    * method is called, then the next time the maximum size is set, this classes
330    * model maximum size will be made equal to the new maximum size. Effectively
331    * what this does is ensure that whenever this objects maximum size is equal
332    * to the one given, then all of the components will take on their default
333    * model sizes. Note: This is only useful when auto model max sizing is
334    * disabled.
335    * @param reset True causes the max model size to be set upon the next max
336    * sizing.
337    */

338   final void resetXAxisModel (boolean reset) {
339
340     needsUpdate = true;
341     resetTitledAreaModel (reset);
342     textList.resetHorizontalTextListAreaModel (reset);
343   }
344
345
346   /**
347    * Paints all the components of this class. First all variables are updated.
348    * @param g2D The graphics context for calculations and painting.
349    */

350   final void paintComponent (Graphics2D g2D) {
351
352     updateXAxisArea (g2D);
353     super.paintComponent (g2D);
354     textList.paintComponent (g2D);
355   }
356
357
358   private void update (Graphics2D g2D) {
359     textList.setCustomRatio (WIDTH, true, getRatio (WIDTH));
360     textList.setCustomRatio (HEIGHT, true, getRatio (HEIGHT));
361     updateTickColors();
362     updateMaxTextList (g2D);
363     textList.updateHorizontalTextListArea (g2D);
364     updateMinSizes (g2D);
365     updateMinTextList (g2D);
366   }
367
368
369   private void updateTickColors() {
370
371     Color[] tickColors = new Color[numTicks];
372     for (int i = 0; i < numTicks; ++i) {
373
374       tickColors[i] = ticksColor;
375     }
376
377     textList.setBulletColors (tickColors);
378   }
379
380
381   private void updateMaxTextList (Graphics2D g2D) {
382
383     textList.setAllowSelfSize(true);
384     textList.setAutoSizes (getAutoSize(MAXMODEL), false);
385     Rectangle maxBounds = getMaxEntitledSpaceBounds (g2D);
386     textList.setSize (MAX, maxBounds.getSize());
387     textList.setSizeLocation (MAX, maxBounds.getLocation());
388   }
389
390
391   private void updateMinSizes (Graphics2D g2D) {
392
393     if (!getAutoSize(MIN)) {
394       Dimension titleSize = getTitleSize (MIN, g2D);
395       Dimension textListSize = textList.getSize (MIN);
396       int minWidth = titleSize.width > textListSize.width ?
397         titleSize.width : textListSize.width;
398       int minHeight;
399       if (titleSize.height > 0) {
400         minHeight = titleSize.height +
401           getBetweenTitleAndSpaceGapThickness (g2D) + textListSize.height;
402       }
403       else minHeight = titleSize.height + textListSize.height;
404       setSpaceSize (MIN, new Dimension (minWidth, minHeight));
405     }
406   }
407
408
409   private void updateMinTextList (Graphics2D g2D) {
410
411     int spaceWidth = textList.getSize (MIN).width;
412     textList.setAllowSelfSize (false);
413     spaceWidth = spaceWidth < getSpaceSize(MIN).width ?
414       getSpaceSize(MIN).width : spaceWidth;
415     int betweenWidth =
416       textList.getSize (MIN).width - textList.getSpaceSize (MIN).width;
417     spaceWidth = spaceWidth - betweenWidth;
418     textList.setSpaceSize (MIN,
419       new Dimension (spaceWidth, textList.getSpaceSize(MIN).height));
420
421     int spaceX = getSpaceSizeLocation(MIN).x;
422     int spaceY;
423     int betweenHeight =
424       textList.getSize (MIN).height - textList.getSpaceSize (MIN).height;
425     if (textList.getJustifications (VERTICAL) == TOP)
426       spaceY = getSpaceSizeLocation (MIN).y + betweenHeight / 2;
427     else
428       spaceY = getSpaceSizeLocation (MIN).y + getSpaceSize (MIN).height -
429         getTitleSize (MIN, g2D).height -
430         getBetweenTitleAndSpaceGapThickness (g2D) -
431         textList.getSize (MIN).height + betweenHeight / 2;
432
433     textList.setSpaceSizeLocation (MIN, new Point (spaceX, spaceY));
434   }
435 }
Popular Tags