KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > axisChart > axis > Axis


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35
36 package org.krysalis.jcharts.axisChart.axis;
37
38
39 import org.krysalis.jcharts.axisChart.AxisChart;
40 import org.krysalis.jcharts.axisChart.axis.scale.ScaleCalculator;
41 import org.krysalis.jcharts.chartText.TextTagGroup;
42 import org.krysalis.jcharts.properties.util.ChartFont;
43 import org.krysalis.jcharts.properties.AxisTypeProperties;
44 import org.krysalis.jcharts.properties.DataAxisProperties;
45 import org.krysalis.jcharts.test.HTMLGenerator;
46 import org.krysalis.jcharts.test.HTMLTestable;
47
48 import java.awt.font.TextLayout JavaDoc;
49 import java.io.Serializable JavaDoc;
50 import java.lang.reflect.Field JavaDoc;
51
52
53 /*************************************************************************************
54  *
55  * @author Nathaniel Auvil
56  * @version $Id: Axis.java,v 1.3 2003/08/27 04:37:44 kiwicmc Exp $
57  ************************************************************************************/

58 abstract public class Axis implements HTMLTestable, Serializable JavaDoc
59 {
60     //---reference to containing chart
61
private AxisChart axisChart;
62
63     //---number of pixels between each label
64
private float scalePixelWidth;
65
66     //---the length of the axis
67
private float pixelLength;
68
69     //---the pixel coordinate of the start of the axis
70
private float origin;
71
72
73     private float titleHeight;
74     private float titleWidth;
75
76     private float minimumWidthNeeded;
77     private float minimumHeightNeeded;
78
79     //---Note, if no labels are displayed, axisLabelsGroup will be NULL!
80
private TextTagGroup axisLabelsGroup;
81
82         // Dual Y axis changes integrated CMC 25Aug03
83
// Variable to use to display the labels of the right Y axis
84
// the corresponding methods are also implemented
85
private TextTagGroup axisLabelsGroupRight;
86
87     //---this number of items to plot on the Axis. Note, if no labels are displayed, axisLabelsGroup will be NULL so we can not depend on that.
88
private int numberOfScaleItems;
89
90     //---pixel location of tick start needed for reference when start drawing charts.
91
private float tickStart;
92
93
94     //---multiplication value used to determine the coordinate location of values on YAxis
95
private double oneUnitPixelSize;
96
97     private float zeroLineCoordinate;
98
99
100     private ScaleCalculator scaleCalculator;
101
102
103     /**************************************************************************************************
104      * Constructor
105      *
106      * @param axisChart
107      * @param numberOfScaleItems
108      ***************************************************************************************************/

109     public Axis( AxisChart axisChart, int numberOfScaleItems )
110     {
111         this.axisChart = axisChart;
112         this.numberOfScaleItems= numberOfScaleItems;
113     }
114
115
116     /**************************************************************************************************
117      * Returns reference to AxisChart Object.
118      *
119      * @return axisChart
120      ***************************************************************************************************/

121     public final AxisChart getAxisChart()
122     {
123         return this.axisChart;
124     }
125
126
127     public int getNumberOfScaleItems()
128     {
129         return numberOfScaleItems;
130     }
131
132
133     public TextTagGroup getAxisLabelsGroup()
134     {
135         return axisLabelsGroup;
136     }
137
138
139     public void setAxisLabelsGroup( TextTagGroup axisLabelsGroup )
140     {
141         this.axisLabelsGroup = axisLabelsGroup;
142     }
143
144
145         // Dual Y axis changes integrated CMC 25Aug03
146
public TextTagGroup getAxisLabelsGroupRight()
147     {
148         return axisLabelsGroupRight;
149     }
150
151     public void setAxisLabelsGroupRight( TextTagGroup axisLabelsGroupRight )
152     {
153         this.axisLabelsGroupRight = axisLabelsGroupRight;
154     }
155
156         public final float getTitleWidth()
157     {
158         return this.titleWidth;
159     }
160
161
162     public final float getTitleHeight()
163     {
164         return this.titleHeight;
165     }
166
167
168     public final float getPixelLength()
169     {
170         return this.pixelLength;
171     }
172
173
174     public final void setPixelLength( float pixelLength )
175     {
176         this.pixelLength = pixelLength;
177     }
178
179
180     public final float getOrigin()
181     {
182         return this.origin;
183     }
184
185
186     public final void setOrigin( float origin )
187     {
188         this.origin = origin;
189     }
190
191
192     public final float getMinimumWidthNeeded()
193     {
194         return this.minimumWidthNeeded;
195     }
196
197
198     public final void setMinimumWidthNeeded( float minimumWidthNeeded )
199     {
200         this.minimumWidthNeeded = minimumWidthNeeded;
201     }
202
203
204     public final float getMinimumHeightNeeded()
205     {
206         return this.minimumHeightNeeded;
207     }
208
209
210     public final void setMinimumHeightNeeded( float minimumHeightNeeded )
211     {
212         this.minimumHeightNeeded = minimumHeightNeeded;
213     }
214
215
216     public final float getScalePixelWidth()
217     {
218         return this.scalePixelWidth;
219     }
220
221
222     public final void setScalePixelWidth( float scalePixelWidth )
223     {
224         this.scalePixelWidth = scalePixelWidth;
225     }
226
227
228     public float getTickStart()
229     {
230         return tickStart;
231     }
232
233
234     public void setTickStart( float tickStart )
235     {
236         this.tickStart = tickStart;
237     }
238
239
240     public ScaleCalculator getScaleCalculator()
241     {
242         return scaleCalculator;
243     }
244
245
246     public void setScaleCalculator( ScaleCalculator scaleCalculator )
247     {
248         this.scaleCalculator = scaleCalculator;
249     }
250
251
252     /*********************************************************************************************
253      *
254      *
255      * @param title
256      * @param axisTitleFont
257      **********************************************************************************************/

258     public final void computeAxisTitleDimensions( String JavaDoc title, ChartFont axisTitleFont )
259     {
260         TextLayout JavaDoc textLayout = new TextLayout JavaDoc( title,
261                                                              axisTitleFont.getFont(),
262                                                              this.getAxisChart().getGraphics2D().getFontRenderContext() );
263
264         this.titleWidth = textLayout.getAdvance();
265         this.titleHeight = textLayout.getAscent() + textLayout.getDescent();
266     }
267
268
269     /*********************************************************************************************
270      * Enables the testing routines to display the contents of this Object.
271      *
272      * Can only see 'private' fields from this class.
273      *
274      * @param htmlGenerator
275      **********************************************************************************************/

276     public void toHTML( HTMLGenerator htmlGenerator )
277     {
278         String JavaDoc name = this.getClass().getSuperclass().getName() + "->";
279
280         //---calling on instance of YAxis or XAxis
281
Field JavaDoc[] fields = this.getClass().getSuperclass().getDeclaredFields();
282         for( int i = 0; i < fields.length; i++ )
283         {
284             try
285             {
286                 htmlGenerator.addField( name + fields[ i ].getName(), fields[ i ].get( this ) );
287             }
288             catch( IllegalAccessException JavaDoc illegalAccessException )
289             {
290                 illegalAccessException.printStackTrace();
291             }
292         }
293     }
294
295
296     /*************************************************************************************************
297      * Computes the number of pixels between each value on the axis.
298      *
299      **************************************************************************************************/

300     public void computeScalePixelWidth( AxisTypeProperties axisTypeProperties )
301     {
302         setScalePixelWidth( getPixelLength() / this.getNumberOfScaleItems() );
303     }
304
305
306     /*************************************************************************************************
307      * Computes the number of pixels between each value on the axis.
308      *
309      **************************************************************************************************/

310     public void computeScalePixelWidthDataAxis( AxisTypeProperties axisTypeProperties )
311     {
312         //---if we are plotting data items, allow for a padding beyond the tallest value so we are not
313
//--- hitting the top of the axis
314
if( axisTypeProperties instanceof DataAxisProperties ) {
315             setScalePixelWidth( (getPixelLength() - 5) / ( this.numberOfScaleItems - 1) );
316         }
317         else {
318             setScalePixelWidth( (getPixelLength() ) / ( this.numberOfScaleItems - 1 ) );
319         }
320     }
321
322
323     public void setOneUnitPixelSize( double oneUnitPixelSize )
324     {
325         this.oneUnitPixelSize = oneUnitPixelSize;
326     }
327
328
329     /**************************************************************************************************
330      * Returns the number of pixels one value unit occupies.
331      *
332      * @return double the number of pixels one value unit occupies.
333      ***************************************************************************************************/

334     public double getOneUnitPixelSize()
335     {
336         return this.oneUnitPixelSize;
337     }
338
339
340     /**************************************************************************************************
341      * Returns the screen coordinate of the zero line. This will not always be the same as the origin
342      * as not all charts start at zero. This is needed not only by the Axis, but some of the Chart
343      * implementations as well.
344      *
345      * @return float the screen pixel location of the zero line.
346      ***************************************************************************************************/

347     public float getZeroLineCoordinate()
348     {
349         return this.zeroLineCoordinate;
350     }
351
352
353     /**************************************************************************************************
354      * Sets the screen coordinate of the zero line. This will not always be the same as the origin
355      * as not all charts start at zero.
356      *
357      * @param value the screen pixel location of the zero line.
358      ***************************************************************************************************/

359     public void setZeroLineCoordinate( float value )
360     {
361         this.zeroLineCoordinate = value;
362     }
363
364
365     /*************************************************************************************************
366      * Computes the relationship of data point values to pixel values so know where along the axis
367      * a value is.
368      *
369      * @param scalePixelLength
370      * @param increment
371      **************************************************************************************************/

372     public void computeOneUnitPixelSize( float scalePixelLength, double increment )
373     {
374         this.oneUnitPixelSize = scalePixelLength / increment;
375     }
376
377
378 }
379
Popular Tags