Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * -------------- 28: * LogFormat.java 29: * -------------- 30: * (C) Copyright 2007, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 02-Aug-2007 : Version 1 (DG); 38: * 39: */ 40: 41: package org.jfree.chart.util; 42: 43: import java.text.DecimalFormat; 44: import java.text.FieldPosition; 45: import java.text.NumberFormat; 46: import java.text.ParsePosition; 47: 48: /** 49: * A number formatter for logarithmic values. This formatter does not support 50: * parsing. 51: * 52: * @since 1.0.7 53: */ 54: public class LogFormat extends NumberFormat { 55: 56: /** The log base value. */ 57: private double base; 58: 59: /** The natural logarithm of the base value. */ 60: private double baseLog; 61: 62: /** The label for the log base (for example, "e"). */ 63: private String baseLabel; 64: 65: /** A flag that controls whether or not the base is shown. */ 66: private boolean showBase; 67: 68: /** The number formatter for the exponent. */ 69: private NumberFormat formatter = new DecimalFormat("0.0"); 70: 71: /** 72: * Creates a new instance. 73: * 74: * @param base the base. 75: * @param baseLabel the base label. 76: * @param showBase a flag that controls whether or not the base value is 77: * shown. 78: */ 79: public LogFormat(double base, String baseLabel, boolean showBase) { 80: this.base = base; 81: this.baseLog = Math.log(this.base); 82: this.baseLabel = baseLabel; 83: this.showBase = showBase; 84: } 85: 86: /** 87: * Calculates the log of a given value. 88: * 89: * @param value the value. 90: * 91: * @return The log of the value. 92: */ 93: private double calculateLog(double value) { 94: return Math.log(value) / this.baseLog; 95: } 96: 97: /** 98: * Returns a formatted representation of the specified number. 99: * 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: }