KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.util.Vector JavaDoc;
31
32
33 /**
34  * A data structure for holding the properties common to all Chart2D charts.
35  * Chart2D charts are all capable of having a legend.
36  * Pass this to any number of Chart2D objects.
37  */

38 public final class Chart2DProperties extends Properties {
39
40
41   /**
42    * The number is 38.
43    */

44   public static final int MAX_INTEGER = 38;
45
46   /**
47    * The number is -38.
48    */

49   public static final int MAX_FLOAT = -38;
50
51   /**
52    * The default is MAX_INTEGER.
53    */

54   public final static int CHART_DATA_LABELS_PRECISION_DEFAULT = MAX_INTEGER;
55
56   /**
57    * The default is true.
58    */

59   public final static boolean CHART_BETWEEN_CHART_AND_LEGEND_GAP_EXISTENCE_DEFAULT = true;
60
61   /**
62    * The default is 5.
63    */

64   public final static int CHART_BETWEEN_CHART_AND_LEGEND_GAP_THICKNESS_MODEL_DEFAULT = 5;
65
66
67   private int chartDataLabelsPrecision;
68   private boolean chartBetweenChartAndLegendGapExistence;
69   private int chartBetweenChartAndLegendGapThicknessModel;
70
71   private boolean needsUpdate = true;
72   private final Vector JavaDoc chart2DVector = new Vector JavaDoc (5, 5);
73   private final Vector JavaDoc needsUpdateVector = new Vector JavaDoc (5, 5);
74
75
76   /**
77    * Creates a Chart2DProperties object with the documented default values.
78    */

79   public Chart2DProperties() {
80
81     needsUpdate = true;
82     setChart2DPropertiesToDefaults();
83   }
84
85
86   /**
87    * Creates a Chart2DProperties object with property values copied from another object.
88    * The copying is a deep copy.
89    * @param chart2DProps The properties to copy.
90    */

91   public Chart2DProperties (Chart2DProperties chart2DProps) {
92
93     needsUpdate = true;
94     setChart2DProperties (chart2DProps);
95   }
96
97
98   /**
99    * Sets all properties to their default values.
100    */

101   public final void setChart2DPropertiesToDefaults() {
102
103     needsUpdate = true;
104     setChartDataLabelsPrecision (CHART_DATA_LABELS_PRECISION_DEFAULT);
105     setChartBetweenChartAndLegendGapExistence (
106       CHART_BETWEEN_CHART_AND_LEGEND_GAP_EXISTENCE_DEFAULT);
107     setChartBetweenChartAndLegendGapThicknessModel (
108       CHART_BETWEEN_CHART_AND_LEGEND_GAP_THICKNESS_MODEL_DEFAULT);
109   }
110
111
112   /**
113    * Sets all properties to be the values of another Chart2DProperties object.
114    * The copying is a deep copy.
115    * @param chart2DProps The properties to copy.
116    */

117   public final void setChart2DProperties (Chart2DProperties chart2DProps) {
118
119     needsUpdate = true;
120     setChartDataLabelsPrecision (chart2DProps.getChartDataLabelsPrecision());
121     setChartBetweenChartAndLegendGapExistence (
122       chart2DProps.getChartBetweenChartAndLegendGapExistence());
123     setChartBetweenChartAndLegendGapThicknessModel (
124       chart2DProps.getChartBetweenChartAndLegendGapThicknessModel());
125   }
126
127
128   /**
129    * Sets the look of labels that are supposed to describe the value of the
130    * data. For example, for pie charts this method will influence
131    * the labels that surround the chart, and for charts with axes this method
132    * will influence the labels on the numbers axis. The specific labels are
133    * first determined from the greatest and least value in the data set and
134    * other factors. This method then lets you adjust whether you want integer
135    * or floating point labels and how exact you want those labels to be. You
136    * do this using the precision parameter, and passing values between 38 and -38.
137    * Nonnegative ints indicate the labels will not contain
138    * a decimal (ie will be integer labels), and the specific nonnegative
139    * number indicates how many zeroes will be to the left of the decimal if
140    * it were there (ex. for the number 1976, a precision of 0 would return 1976,
141    * a precision of 1 would return 1980, a precision of 3 would return 2000, and
142    * a precision greater than 3 would also return 2000). If the number of
143    * desired zeroes is greater than the label, then the number of zeroes is
144    * automatically scaled back to be 1 less than the number of digits. Negative
145    * ints for precision indicate the labels will contain a decimal, and the
146    * specific negative number indicates how many digits to the right of the
147    * decimal are wanted (ex. for the number 1.935, a precision of -1 would
148    * return 1.9, a precision of -2 would return 1.94, a precision of -4 would
149    * return 1.9350). If the number of desired decimal places is larger than the
150    * number of places present in the number, then zeroes are added to form as
151    * many places as are desired up to -38. MAX_INTEGER or MAX_FLOAT will always
152    * indicate the maximum number of zeroes and the maximum number of decimal
153    * places possible by Chart2D's algorithm for adjusting these labels.
154    * @param precision The adjustement of the precision of numbers labels.
155    */

156   public final void setChartDataLabelsPrecision (int precision) {
157
158     needsUpdate = true;
159     chartDataLabelsPrecision = precision;
160   }
161
162
163   /**
164    * Sets whether the gap between the chart and the legend exists.
165    * If the legend doesn't exist, this gap will automatically not exist.
166    * @param existence If true, the gap exists.
167    */

168   public final void setChartBetweenChartAndLegendGapExistence (boolean existence) {
169
170     needsUpdate = true;
171     chartBetweenChartAndLegendGapExistence = existence;
172   }
173
174
175   /**
176    * Sets the thickness of the gap between the chart and the legend for the chart's model size.
177    * @param thickness The model thickness of the gap.
178    */

179   public final void setChartBetweenChartAndLegendGapThicknessModel (int thickness) {
180
181     needsUpdate = true;
182     chartBetweenChartAndLegendGapThicknessModel = thickness;
183   }
184
185
186   /**
187    * Gets the look of labels that are supposed to describe the value of the
188    * data. For example, for pie charts this method will influence
189    * the labels that surround the chart, and for charts with axes this method
190    * will influence the labels on the numbers axis. The specific labels are
191    * first determined from the greatest and least value in the data set and
192    * other factors. This method then lets you adjust whether you want integer
193    * or floating point labels and how exact you want those labels to be. You
194    * do this using the precision parameter, and passing values between 8 and -8.
195    * Nonnegative ints indicate the labels will not contain
196    * a decimal (ie will be integer labels), and the specific nonnegative
197    * number indicates how many zeroes will be to the left of the decimal if
198    * it were there (ex. for the number 1976, a precision of 0 would return 1976,
199    * a precision of 1 would return 1980, a precision of 3 would return 2000, and
200    * a precision greater than 3 would also return 2000). If the number of
201    * desired zeroes is greater than the label, then the number of zeroes is
202    * automatically scaled back to be 1 less than the number of digits. Negative
203    * ints for precision indicate the labels will contain a decimal, and the
204    * specific negative number indicates how many digits to the right of the
205    * decimal are wanted (ex. for the number 1.935, a precision of -1 would
206    * return 1.9, a precision of -2 would return 1.94, a precision of -4 would
207    * return 1.9350). If the number of desired decimal places is larger than the
208    * number of places present in the number, then zeroes are added to form as
209    * many places as are desired up to -8. MAX_INTEGER or MAX_FLOAT will always
210    * indicate the maximum number of zeroes and the maximum number of decimal
211    * places possible by Chart2D's algorithm for adjusting these labels.
212    * @return The adjustement of the precision of numbers labels.
213    */

214   public final int getChartDataLabelsPrecision() {
215     return chartDataLabelsPrecision;
216   }
217
218
219   /**
220    * Gets whether the gap between the chart and the legend exists.
221    * If the legend doesn't exist, this gap will automatically not exist.
222    * @return If true, the gap exists.
223    */

224   public final boolean getChartBetweenChartAndLegendGapExistence() {
225     return chartBetweenChartAndLegendGapExistence;
226   }
227
228
229   /**
230    * Gets the thickness of the gap between the chart and the legend for the chart's model size.
231    * @return The model thickness of the gap.
232    */

233   public final int getChartBetweenChartAndLegendGapThicknessModel() {
234     return chartBetweenChartAndLegendGapThicknessModel;
235   }
236
237
238   /**
239    * Gets whether this object needs to be updated with new properties.
240    * @param chart2D The object that may need to be updated.
241    * @return If true then needs update.
242    */

243   final boolean getChart2DNeedsUpdate (Chart2D chart2D) {
244
245     if (needsUpdate) return true;
246     int index = -1;
247     if ((index = chart2DVector.indexOf (chart2D)) != -1) {
248       return ((Boolean JavaDoc)needsUpdateVector.get (index)).booleanValue();
249     }
250     return false;
251   }
252
253
254   /**
255    * Adds a Chart2D to the set of objects using these properties.
256    * @param chart2D The object to add.
257    */

258   final void addChart2D (Chart2D chart2D) {
259     if (!chart2DVector.contains (chart2D)) {
260       chart2DVector.add (chart2D);
261       needsUpdateVector.add (new Boolean JavaDoc (true));
262     }
263   }
264
265
266   /**
267    * Removes a Chart2D from the set of objects using these properties.
268    * @param chart2D The object to remove.
269    */

270   final void removeChart2D (Chart2D chart2D) {
271     int index = -1;
272     if ((index = chart2DVector.indexOf (chart2D)) != -1) {
273       chart2DVector.remove (index);
274       needsUpdateVector.remove (index);
275     }
276   }
277
278
279   /**
280    * Validates the properties of this object.
281    * If debug is true then prints a messages indicating whether each property is valid.
282    * Returns true if all the properties were valid and false otherwise.
283    * @param debug If true then will print status messages.
284    * @return If true then valid.
285    */

286   final boolean validate (boolean debug) {
287
288     if (debug) System.out.println ("Validating Chart2DProperties");
289
290     boolean valid = true;
291
292     if (chartDataLabelsPrecision > MAX_INTEGER || chartDataLabelsPrecision < MAX_FLOAT) {
293       valid = false;
294       if (debug) System.out.println ("Problem with ChartDataLabelsPrecision");
295     }
296     if (chartBetweenChartAndLegendGapThicknessModel < 0) {
297       valid = false;
298       if (debug) System.out.println ("ChartBetweenChartAndLegendGapThicknessModel < 0");
299     }
300
301     if (debug) {
302       if (valid) System.out.println ("Chart2DProperties was valid");
303       else System.out.println ("Chart2DProperties was invalid");
304     }
305
306     return valid;
307   }
308
309
310   /**
311    * Updates the properties of this Chart2D.
312    * @param chart2D The Chart2D to update.
313    */

314   final void updateChart2D (Chart2D chart2D) {
315
316     if (getChart2DNeedsUpdate (chart2D)) {
317
318       if (needsUpdate) {
319         for (int i = 0; i < needsUpdateVector.size(); ++i) {
320           needsUpdateVector.set (i, new Boolean JavaDoc (true));
321         }
322         needsUpdate = false;
323       }
324
325       int index = -1;
326       if ((index = chart2DVector.indexOf (chart2D)) != -1) {
327         needsUpdateVector.set (index, new Boolean JavaDoc (false));
328       }
329     }
330   }
331 }
Popular Tags