001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * --------------
028 * LogFormat.java
029 * --------------
030 * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 02-Aug-2007 : Version 1 (DG);
038 * 
039 */
040
041package org.jfree.chart.util;
042
043import java.text.DecimalFormat;
044import java.text.FieldPosition;
045import java.text.NumberFormat;
046import java.text.ParsePosition;
047
048/**
049 * A number formatter for logarithmic values.  This formatter does not support
050 * parsing.
051 * 
052 * @since 1.0.7
053 */
054public class LogFormat extends NumberFormat {
055    
056    /** The log base value. */
057    private double base;
058    
059    /** The natural logarithm of the base value. */
060    private double baseLog;
061    
062    /** The label for the log base (for example, "e"). */
063    private String baseLabel;
064    
065    /** A flag that controls whether or not the base is shown. */
066    private boolean showBase;
067    
068    /** The number formatter for the exponent. */
069    private NumberFormat formatter = new DecimalFormat("0.0");
070    
071    /**
072     * Creates a new instance.
073     * 
074     * @param base  the base.
075     * @param baseLabel  the base label.
076     * @param showBase  a flag that controls whether or not the base value is
077     *                  shown.
078     */
079    public LogFormat(double base, String baseLabel, boolean showBase) {
080        this.base = base;
081        this.baseLog = Math.log(this.base);
082        this.baseLabel = baseLabel;
083        this.showBase = showBase;
084    }
085    
086    /**
087     * Calculates the log of a given value.
088     * 
089     * @param value  the value.
090     * 
091     * @return The log of the value.
092     */
093    private double calculateLog(double value) {
094        return Math.log(value) / this.baseLog;
095    }
096    
097    /**
098     * Returns a formatted representation of the specified number.
099     * 
100     * @param number  the number.
101     * @param toAppendTo  the string buffer to append to.
102     * @param pos  the position.
103     * 
104     * @return A string buffer containing the formatted value.
105     */
106    public StringBuffer format(double number, StringBuffer toAppendTo,
107            FieldPosition pos) {
108        StringBuffer result = new StringBuffer();
109        if (this.showBase) {
110            result.append(this.baseLabel);
111            result.append("^");
112        }
113        result.append(this.formatter.format(calculateLog(number)));
114        return result;
115    }
116
117    /**
118     * Formats the specified number as a hexadecimal string.  The decimal 
119     * fraction is ignored.
120     * 
121     * @param number  the number to format.
122     * @param toAppendTo  the buffer to append to (ignored here).
123     * @param pos  the field position (ignored here).
124     * 
125     * @return The string buffer.
126     */
127    public StringBuffer format(long number, StringBuffer toAppendTo, 
128            FieldPosition pos) {
129        StringBuffer result = new StringBuffer();
130        if (this.showBase) {
131            result.append(this.baseLabel);
132            result.append("^");
133        }
134        result.append(this.formatter.format(calculateLog(number)));
135        return result;
136    }
137
138    /**
139     * Parsing is not implemented, so this method always returns 
140     * <code>null</code>.
141     * 
142     * @param source  ignored.
143     * @param parsePosition  ignored.
144     * 
145     * @return Always <code>null</code>.
146     */
147    public Number parse (String source, ParsePosition parsePosition) {
148        return null; // don't bother with parsing
149    }
150
151}