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