Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2008, 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, 2008, 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: * 19-Feb-2008 : Implemented equals() and clone(), and added new powerLabel 39: * attribute as per Feature Request 1886036 (DG); 40: * 41: */ 42: 43: package org.jfree.chart.util; 44: 45: import java.text.DecimalFormat; 46: import java.text.FieldPosition; 47: import java.text.NumberFormat; 48: import java.text.ParsePosition; 49: 50: /** 51: * A number formatter for logarithmic values. This formatter does not support 52: * parsing. 53: * 54: * @since 1.0.7 55: */ 56: public class LogFormat extends NumberFormat { 57: 58: /** The log base value. */ 59: private double base; 60: 61: /** The natural logarithm of the base value. */ 62: private double baseLog; 63: 64: /** The label for the log base (for example, "e"). */ 65: private String baseLabel; 66: 67: /** 68: * The label for the power symbol. 69: * 70: * @since 1.0.10 71: */ 72: private String powerLabel; 73: 74: /** A flag that controls whether or not the base is shown. */ 75: private boolean showBase; 76: 77: /** The number formatter for the exponent. */ 78: private NumberFormat formatter = new DecimalFormat("0.0"); 79: 80: /** 81: * Creates a new instance. 82: * 83: * @param base the base. 84: * @param baseLabel the base label (<code>null</code> not permitted). 85: * @param showBase a flag that controls whether or not the base value is 86: * shown. 87: */ 88: public LogFormat(double base, String baseLabel, boolean showBase) { 89: this(base, baseLabel, "^", showBase); 90: } 91: 92: /** 93: * Creates a new instance. 94: * 95: * @param base the base. 96: * @param baseLabel the base label (<code>null</code> not permitted). 97: * @param powerLabel the power label (<code>null</code> not permitted). 98: * @param showBase a flag that controls whether or not the base value is 99: * shown. 100: * 101: * @since 1.0.10 102: */ 103: public LogFormat(double base, String baseLabel, String powerLabel, 104: boolean showBase) { 105: if (baseLabel == null) { 106: throw new IllegalArgumentException("Null 'baseLabel' argument."); 107: } 108: if (powerLabel == null) { 109: throw new IllegalArgumentException("Null 'powerLabel' argument."); 110: } 111: this.base = base; 112: this.baseLog = Math.log(this.base); 113: this.baseLabel = baseLabel; 114: this.showBase = showBase; 115: this.powerLabel = powerLabel; 116: } 117: 118: /** 119: * Calculates the log of a given value. 120: * 121: * @param value the value. 122: * 123: * @return The log of the value. 124: */ 125: private double calculateLog(double value) { 126: return Math.log(value) / this.baseLog; 127: } 128: 129: /** 130: * Returns a formatted representation of the specified number. 131: * 132: * @param number the number. 133: * @param toAppendTo the string buffer to append to. 134: * @param pos the position. 135: * 136: * @return A string buffer containing the formatted value. 137: */ 138: public StringBuffer format(double number, StringBuffer toAppendTo, 139: FieldPosition pos) { 140: StringBuffer result = new StringBuffer(); 141: if (this.showBase) { 142: result.append(this.baseLabel); 143: result.append(this.powerLabel); 144: } 145: result.append(this.formatter.format(calculateLog(number))); 146: return result; 147: } 148: 149: /** 150: * Formats the specified number as a hexadecimal string. The decimal 151: * fraction is ignored. 152: * 153: * @param number the number to format. 154: * @param toAppendTo the buffer to append to (ignored here). 155: * @param pos the field position (ignored here). 156: * 157: * @return The string buffer. 158: */ 159: public StringBuffer format(long number, StringBuffer toAppendTo, 160: FieldPosition pos) { 161: StringBuffer result = new StringBuffer(); 162: if (this.showBase) { 163: result.append(this.baseLabel); 164: result.append("^"); 165: } 166: result.append(this.formatter.format(calculateLog(number))); 167: return result; 168: } 169: 170: /** 171: * Parsing is not implemented, so this method always returns 172: * <code>null</code>. 173: * 174: * @param source ignored. 175: * @param parsePosition ignored. 176: * 177: * @return Always <code>null</code>. 178: */ 179: public Number parse (String source, ParsePosition parsePosition) { 180: return null; // don't bother with parsing 181: } 182: 183: /** 184: * Tests this formatter for equality with an arbitrary object. 185: * 186: * @param obj the object (<code>null</code> permitted). 187: * 188: * @return A boolean. 189: */ 190: public boolean equals(Object obj) { 191: if (obj == this) { 192: return true; 193: } 194: if (!(obj instanceof LogFormat)) { 195: return false; 196: } 197: LogFormat that = (LogFormat) obj; 198: if (this.base != that.base) { 199: return false; 200: } 201: if (!this.baseLabel.equals(that.baseLabel)) { 202: return false; 203: } 204: if (this.baseLog != that.baseLog) { 205: return false; 206: } 207: if (this.showBase != that.showBase) { 208: return false; 209: } 210: return super.equals(obj); 211: } 212: 213: /** 214: * Returns a clone of this instance. 215: * 216: * @return A clone. 217: */ 218: public Object clone() { 219: LogFormat clone = (LogFormat) super.clone(); 220: clone.formatter = (NumberFormat) this.formatter.clone(); 221: return clone; 222: } 223: 224: }