001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.axisChart.axis.scale;
034:
035: import org.krysalis.jcharts.axisChart.axis.scale.ScaleCalculator;
036:
037: /*************************************************************************************
038: *
039: * @author Nathaniel Auvil, Mike Lissick
040: * @version $Id: AutomaticScaleCalculator.java,v 1.1 2003/05/17 16:54:36 nathaniel_auvil Exp $
041: ************************************************************************************/
042: public class AutomaticScaleCalculator extends ScaleCalculator {
043:
044: /****************************************************************************************
045: *
046: ***************************************************************************************/
047: public AutomaticScaleCalculator() {
048:
049: }
050:
051: /*********************************************************************************************
052: * Computes the axis increment taking into account the user specified criteria.
053: *
054: ********************************************************************************************/
055: public void computeIncrement() {
056: double powerOfTen = Math.pow(10.0d, Math.abs((double) super
057: .getRoundingPowerOfTen()));
058:
059: double range;
060:
061: //---if MIN >= 0, MAX is the range, if MAX < 0, -MIN is the range
062: if ((super .getMinValue() >= 0) || (super .getMaxValue() < 0)) {
063: range = Math.max(super .getMaxValue(), -super .getMinValue());
064:
065: super .increment = range
066: / (super .getNumberOfScaleItems() - 1);
067: this .roundTheIncrement(powerOfTen);
068:
069: if (super .getMinValue() >= 0) {
070: super .setMinValue(0.0d);
071: super .setMaxValue(super .increment
072: * super .getNumberOfScaleItems());
073: } else {
074: super .setMaxValue(0.0d);
075: super .setMinValue(-(super .increment * super
076: .getNumberOfScaleItems()));
077: }
078: }
079: //---else MIN is negative and MAX is positive, so add values together (minus a negative is a positive)
080: else {
081: super .setMinValue(super .round(super .getMinValue(),
082: powerOfTen));
083:
084: //---round min value down to get the start value for axis. Compute range from this value.
085: if (super .getRoundingPowerOfTen() > 0) {
086: super .setMinValue(super .getMinValue() - powerOfTen);
087: } else {
088: super .setMinValue(super .getMinValue()
089: - (1 / powerOfTen));
090: }
091:
092: //---we want the rounded Axis min for range
093: //---MIN is always negative at this point so minus a negative is a positive
094: range = super .getMaxValue() - super .getMinValue();
095:
096: super .increment = range
097: / (super .getNumberOfScaleItems() - 1);
098: super .roundTheIncrement(powerOfTen);
099:
100: //---axis starts at minValue, not zero!
101: super .setMaxValue(super .getMinValue()
102: + (this .increment * super .getNumberOfScaleItems()));
103: }
104: }
105:
106: /*********************************************************************************************
107: * Drives the computation of the axis increment and related values taking into account the
108: * user specified rounding criteria and sets it to the super class increment variable.
109: *
110: * So if you specify to round to the nearest 100 and give an increment of 2.5, the increment
111: * will become 100.
112: *
113: * @param numberOfScaleItems
114: ********************************************************************************************
115: public void roundScaleValues( double powerOfTen, int numberOfScaleItems )
116: {
117: this.roundTheIncrement( powerOfTen );
118:
119:
120: //---if MIN >= 0, MAX is the range, if MAX < 0, -MIN is the range
121: if( ( super.getMinValue() >= 0 ) || ( super.getMaxValue() < 0 ) )
122: {
123: if( super.getMinValue() >= 0 )
124: {
125: super.setMinValue( 0.0d );
126: super.setMaxValue( super.increment * numberOfScaleItems );
127: }
128: else
129: {
130: super.setMaxValue( 0.0d );
131: super.setMinValue( -( super.increment * numberOfScaleItems ) );
132: }
133: }
134: //---else MIN is negative and MAX is positive, so add values together (minus a negative is a positive)
135: else
136: {
137: super.setMinValue( super.round( super.getMinValue(), powerOfTen ) );
138:
139: //---round min value down to get the start value for axis. Compute range from this value.
140: if( super.getRoundingPowerOfTen() > 0 )
141: {
142: super.setMinValue( super.getMinValue() - powerOfTen );
143: }
144: else
145: {
146: super.setMinValue( super.getMinValue()- ( 1 / powerOfTen ) );
147: }
148:
149: //---we want the rounded Axis min for range
150: //---MIN is always negative at this point so minus a negative is a positive
151: range = super.getMaxValue() - super.getMinValue();
152:
153: super.increment = range / numberOfScaleItems;
154: super.roundTheIncrement( powerOfTen );
155:
156: //---axis starts at minValue, not zero!
157: super.setMaxValue( super.getMinValue() + ( this.increment * numberOfScaleItems ) );
158: }
159:
160:
161: }
162:
163: */
164:
165: /*
166:
167: public static void main( String[] args )
168: {
169: AutomaticScaleCalculator s= new AutomaticScaleCalculator();
170:
171: }
172: */
173: }
|