KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > axisChart > axis > scale > ScaleCalculator


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

39
40
41 package org.krysalis.jcharts.axisChart.axis.scale;
42
43
44 /***************************************************************************************
45  * Base class for the logic used to compute the scale on the charts. There are two
46  * implementations provided and you are free to implement your own if you do not like
47  * the default implementations provided.
48  *
49  *
50  ***************************************************************************************/

51 public abstract class ScaleCalculator
52 {
53     private double minValue;
54     private double maxValue;
55     private int roundingPowerOfTen;
56     private int numberOfScaleItems;
57
58     protected double increment;
59
60
61
62     /***************************************************************************************
63      * Computes the scale increment.
64      *
65      **************************************************************************************/

66     protected abstract void computeIncrement();
67
68
69     /*********************************************************************************************
70      * Drives the computation of the axis increment and related values taking into account the
71      * user specified rounding criteria.
72      *
73      * So if you specify to round to the nearest 100 and give an increment of 2.5, the increment
74      * will become 100.
75      *
76      ********************************************************************************************/

77     public final void computeScaleValues()
78     {
79         this.computeIncrement();
80
81         //double powerOfTen = Math.pow( 10, Math.abs( this.getRoundingPowerOfTen() ) );
82
//this.roundScaleValues( powerOfTen, numberOfScaleItems );
83
}
84
85
86     public final void setMinValue( double minValue )
87     {
88         this.minValue = minValue;
89     }
90
91
92     public final double getMinValue()
93     {
94         return this.minValue;
95     }
96
97
98     public final void setMaxValue( double maxValue )
99     {
100         this.maxValue = maxValue;
101     }
102
103
104     public final double getMaxValue()
105     {
106         return this.maxValue;
107     }
108
109
110     public final double getIncrement()
111     {
112         return increment;
113     }
114
115
116     public int getNumberOfScaleItems()
117     {
118         return numberOfScaleItems;
119     }
120
121
122     public void setNumberOfScaleItems( int numberOfScaleItems )
123     {
124         this.numberOfScaleItems = numberOfScaleItems;
125     }
126
127
128     /***********************************************************************************************
129      * Sets the exponent power of ten to round values to.
130      *
131      * @param powerOfTen exponent of ten to round to: 1=10, 2=100, -2=.01
132      ***********************************************************************************************/

133     public final void setRoundingPowerOfTen( int powerOfTen )
134     {
135         this.roundingPowerOfTen = powerOfTen;
136     }
137
138
139     public final int getRoundingPowerOfTen()
140     {
141         return this.roundingPowerOfTen;
142     }
143
144
145     /***********************************************************************************************
146      * Rounds the passed value by the power of ten specified
147      *
148      * @param value the value to round
149      * @param powerOfTen the product of 10 times the rounding property.
150      * @return double the rounded result
151      ************************************************************************************************/

152     protected double round( double value, double powerOfTen )
153     {
154         if( this.roundingPowerOfTen > 0 )
155         {
156             return ( Math.round( value / powerOfTen ) * powerOfTen );
157         }
158         else if( this.roundingPowerOfTen < 0 )
159         {
160             return ( Math.round( value * powerOfTen ) / powerOfTen );
161         }
162         else
163         {
164             return ( Math.round( value ) );
165         }
166     }
167
168
169     /***********************************************************************************************
170      * Rounds the scale increment up by the power of ten specified in the properties.
171      *
172      * @param powerOfTen the value of 10 times the rounding property.
173      ************************************************************************************************/

174     protected void roundTheIncrement( double powerOfTen )
175     {
176         this.increment = this.round( this.increment, powerOfTen );
177
178         //---round the increment up or down
179
if( this.roundingPowerOfTen > 0 )
180         {
181             this.increment += powerOfTen;
182         }
183         else
184         {
185             this.increment += ( 1 / powerOfTen );
186         }
187     }
188
189
190    /*******************************************************************************
191      *
192      * @return String
193      *******************************************************************************/

194     public String JavaDoc toString()
195     {
196         StringBuffer JavaDoc s= new StringBuffer JavaDoc( 90 );
197         s.append( "ScaleCalculator-> min= " );
198         s.append( this.minValue );
199         s.append( " max= " );
200         s.append( this.maxValue );
201         s.append( " increment= " );
202         s.append( this.increment );
203         return s.toString();
204     }
205 }
206
Popular Tags