KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.krysalis.jcharts.axisChart.axis.scale;
36
37
38 import org.krysalis.jcharts.axisChart.axis.scale.ScaleCalculator;
39
40
41 /*************************************************************************************
42  *
43  * @author Nathaniel Auvil, Mike Lissick
44  * @version $Id: AutomaticScaleCalculator.java,v 1.1 2003/05/17 16:54:36 nathaniel_auvil Exp $
45  ************************************************************************************/

46 public class AutomaticScaleCalculator extends ScaleCalculator
47 {
48
49     /****************************************************************************************
50      *
51      ***************************************************************************************/

52     public AutomaticScaleCalculator()
53     {
54
55     }
56
57
58     /*********************************************************************************************
59      * Computes the axis increment taking into account the user specified criteria.
60      *
61      ********************************************************************************************/

62     public void computeIncrement()
63     {
64         double powerOfTen = Math.pow( 10.0d, Math.abs( ( double ) super.getRoundingPowerOfTen() ) );
65
66         double range;
67
68
69         //---if MIN >= 0, MAX is the range, if MAX < 0, -MIN is the range
70
if( ( super.getMinValue() >= 0 ) || ( super.getMaxValue() < 0 ) )
71         {
72             range = Math.max( super.getMaxValue(), -super.getMinValue() );
73
74             super.increment = range / ( super.getNumberOfScaleItems() - 1 );
75             this.roundTheIncrement( powerOfTen );
76
77             if( super.getMinValue() >= 0 )
78             {
79                 super.setMinValue( 0.0d );
80                 super.setMaxValue( super.increment * super.getNumberOfScaleItems() );
81             }
82             else
83             {
84                 super.setMaxValue( 0.0d );
85                 super.setMinValue( -( super.increment * super.getNumberOfScaleItems() ) );
86             }
87         }
88         //---else MIN is negative and MAX is positive, so add values together (minus a negative is a positive)
89
else
90         {
91             super.setMinValue( super.round( super.getMinValue(), powerOfTen ) );
92
93             //---round min value down to get the start value for axis. Compute range from this value.
94
if( super.getRoundingPowerOfTen() > 0 )
95             {
96                 super.setMinValue( super.getMinValue() - powerOfTen );
97             }
98             else
99             {
100                 super.setMinValue( super.getMinValue()- ( 1 / powerOfTen ) );
101             }
102
103             //---we want the rounded Axis min for range
104
//---MIN is always negative at this point so minus a negative is a positive
105
range = super.getMaxValue() - super.getMinValue();
106
107             super.increment = range / ( super.getNumberOfScaleItems() - 1 );
108             super.roundTheIncrement( powerOfTen );
109
110             //---axis starts at minValue, not zero!
111
super.setMaxValue( super.getMinValue() + ( this.increment * super.getNumberOfScaleItems() ) );
112         }
113     }
114
115
116     /*********************************************************************************************
117      * Drives the computation of the axis increment and related values taking into account the
118      * user specified rounding criteria and sets it to the super class increment variable.
119      *
120      * So if you specify to round to the nearest 100 and give an increment of 2.5, the increment
121      * will become 100.
122      *
123      * @param numberOfScaleItems
124      ********************************************************************************************
125     public void roundScaleValues( double powerOfTen, int numberOfScaleItems )
126     {
127         this.roundTheIncrement( powerOfTen );
128
129
130         //---if MIN >= 0, MAX is the range, if MAX < 0, -MIN is the range
131         if( ( super.getMinValue() >= 0 ) || ( super.getMaxValue() < 0 ) )
132         {
133             if( super.getMinValue() >= 0 )
134             {
135                 super.setMinValue( 0.0d );
136                 super.setMaxValue( super.increment * numberOfScaleItems );
137             }
138             else
139             {
140                 super.setMaxValue( 0.0d );
141                 super.setMinValue( -( super.increment * numberOfScaleItems ) );
142             }
143         }
144         //---else MIN is negative and MAX is positive, so add values together (minus a negative is a positive)
145         else
146         {
147             super.setMinValue( super.round( super.getMinValue(), powerOfTen ) );
148
149             //---round min value down to get the start value for axis. Compute range from this value.
150             if( super.getRoundingPowerOfTen() > 0 )
151             {
152                 super.setMinValue( super.getMinValue() - powerOfTen );
153             }
154             else
155             {
156                 super.setMinValue( super.getMinValue()- ( 1 / powerOfTen ) );
157             }
158
159             //---we want the rounded Axis min for range
160             //---MIN is always negative at this point so minus a negative is a positive
161             range = super.getMaxValue() - super.getMinValue();
162
163             super.increment = range / numberOfScaleItems;
164             super.roundTheIncrement( powerOfTen );
165
166             //---axis starts at minValue, not zero!
167             super.setMaxValue( super.getMinValue() + ( this.increment * numberOfScaleItems ) );
168         }
169
170
171     }
172
173     */

174
175
176 /*
177
178     public static void main( String[] args )
179     {
180         AutomaticScaleCalculator s= new AutomaticScaleCalculator();
181
182     }
183 */

184 }
185
Popular Tags