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: * CompassFormat.java 29: * ------------------ 30: * (C) Copyright 2003-2007, by Sylvain Vieujot and Contributors. 31: * 32: * Original Author: Sylvain Vieujot; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * Changes 36: * ------- 37: * 18-Feb-2004 : Version 1 contributed by Sylvain Vieujot (DG); 38: * 39: */ 40: 41: package org.jfree.chart.axis; 42: 43: import java.text.FieldPosition; 44: import java.text.NumberFormat; 45: import java.text.ParsePosition; 46: 47: /** 48: * A formatter that displays numbers as directions. 49: */ 50: public class CompassFormat extends NumberFormat { 51: 52: /** North. */ 53: private static final String N = "N"; 54: 55: /** East. */ 56: private static final String E = "E"; 57: 58: /** South. */ 59: private static final String S = "S"; 60: 61: /** West. */ 62: private static final String W = "W"; 63: 64: /** The directions. */ 65: public static final String[] DIRECTIONS = { 66: N, N + N + E, N + E, E + N + E, E, E + S + E, S + E, S + S + E, S, 67: S + S + W, S + W, W + S + W, W, W + N + W, N + W, N + N + W, N 68: }; 69: 70: /** 71: * Creates a new formatter. 72: */ 73: public CompassFormat() { 74: super(); 75: } 76: 77: /** 78: * Returns a string representing the direction. 79: * 80: * @param direction the direction. 81: * 82: * @return A string. 83: */ 84: public String getDirectionCode(double direction) { 85: 86: direction = direction % 360; 87: if (direction < 0.0) { 88: direction = direction + 360.0; 89: } 90: int index = ((int) Math.floor(direction / 11.25) + 1) / 2; 91: return DIRECTIONS[index]; 92: 93: } 94: 95: /* (non-Javadoc) 96: * @see java.text.NumberFormat#format(double, java.lang.StringBuffer, 97: * java.text.FieldPosition) 98: */ 99: public StringBuffer format(double number, StringBuffer toAppendTo, 100: FieldPosition pos) { 101: return toAppendTo.append(getDirectionCode(number)); 102: } 103: 104: /* (non-Javadoc) 105: * @see java.text.NumberFormat#format(long, java.lang.StringBuffer, 106: * java.text.FieldPosition) 107: */ 108: public StringBuffer format(long number, StringBuffer toAppendTo, 109: FieldPosition pos) { 110: return toAppendTo.append(getDirectionCode(number)); 111: } 112: 113: /** 114: * This method returns <code>null</code> for all inputs. This class cannot 115: * be used for parsing. 116: * 117: * @param source the source string. 118: * @param parsePosition the parse position. 119: * 120: * @return <code>null</code>. 121: */ 122: public Number parse(String source, ParsePosition parsePosition) { 123: return null; 124: } 125: 126: }