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: * HighLowItemLabelGenerator.java 29: * ------------------------------ 30: * (C) Copyright 2001-2008, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): David Basten; 34: * 35: * Changes 36: * ------- 37: * 13-Dec-2001 : Version 1 (DG); 38: * 16-Jan-2002 : Completed Javadocs (DG); 39: * 23-Apr-2002 : Added date to the tooltip string (DG); 40: * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 41: * 21-Mar-2003 : Implemented Serializable (DG); 42: * 13-Aug-2003 : Implemented Cloneable (DG); 43: * 17-Nov-2003 : Implemented PublicCloneable (DG); 44: * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG); 45: * 25-May-2004 : Added number formatter (see patch 890496) (DG); 46: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 47: * getYValue() (DG); 48: * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG); 49: * 31-Mar-2008 : Added hashCode() method to appease FindBugs (DG); 50: * 51: */ 52: 53: package org.jfree.chart.labels; 54: 55: import java.io.Serializable; 56: import java.text.DateFormat; 57: import java.text.NumberFormat; 58: import java.util.Date; 59: 60: import org.jfree.chart.HashUtilities; 61: import org.jfree.data.xy.OHLCDataset; 62: import org.jfree.data.xy.XYDataset; 63: import org.jfree.util.PublicCloneable; 64: 65: /** 66: * A standard item label generator for plots that use data from a 67: * {@link OHLCDataset}. 68: */ 69: public class HighLowItemLabelGenerator implements XYItemLabelGenerator, 70: XYToolTipGenerator, Cloneable, PublicCloneable, Serializable { 71: 72: /** For serialization. */ 73: private static final long serialVersionUID = 5617111754832211830L; 74: 75: /** The date formatter. */ 76: private DateFormat dateFormatter; 77: 78: /** The number formatter. */ 79: private NumberFormat numberFormatter; 80: 81: /** 82: * Creates an item label generator using the default date and number 83: * formats. 84: */ 85: public HighLowItemLabelGenerator() { 86: this(DateFormat.getInstance(), NumberFormat.getInstance()); 87: } 88: 89: /** 90: * Creates a tool tip generator using the supplied date formatter. 91: * 92: * @param dateFormatter the date formatter (<code>null</code> not 93: * permitted). 94: * @param numberFormatter the number formatter (<code>null</code> not 95: * permitted). 96: */ 97: public HighLowItemLabelGenerator(DateFormat dateFormatter, 98: NumberFormat numberFormatter) { 99: if (dateFormatter == null) { 100: throw new IllegalArgumentException( 101: "Null 'dateFormatter' argument."); 102: } 103: if (numberFormatter == null) { 104: throw new IllegalArgumentException( 105: "Null 'numberFormatter' argument."); 106: } 107: this.dateFormatter = dateFormatter; 108: this.numberFormatter = numberFormatter; 109: } 110: 111: /** 112: * Generates a tooltip text item for a particular item within a series. 113: * 114: * @param dataset the dataset. 115: * @param series the series (zero-based index). 116: * @param item the item (zero-based index). 117: * 118: * @return The tooltip text. 119: */ 120: public String generateToolTip(XYDataset dataset, int series, int item) { 121: 122: String result = null; 123: 124: if (dataset instanceof OHLCDataset) { 125: OHLCDataset d = (OHLCDataset) dataset; 126: Number high = d.getHigh(series, item); 127: Number low = d.getLow(series, item); 128: Number open = d.getOpen(series, item); 129: Number close = d.getClose(series, item); 130: Number x = d.getX(series, item); 131: 132: result = d.getSeriesKey(series).toString(); 133: 134: if (x != null) { 135: Date date = new Date(x.longValue()); 136: result = result + "--> Date=" + this.dateFormatter.format(date); 137: if (high != null) { 138: result = result + " High=" 139: + this.numberFormatter.format(high.doubleValue()); 140: } 141: if (low != null) { 142: result = result + " Low=" 143: + this.numberFormatter.format(low.doubleValue()); 144: } 145: if (open != null) { 146: result = result + " Open=" 147: + this.numberFormatter.format(open.doubleValue()); 148: } 149: if (close != null) { 150: result = result + " Close=" 151: + this.numberFormatter.format(close.doubleValue()); 152: } 153: } 154: 155: } 156: 157: return result; 158: 159: } 160: 161: /** 162: * Generates a label for the specified item. The label is typically a 163: * formatted version of the data value, but any text can be used. 164: * 165: * @param dataset the dataset (<code>null</code> not permitted). 166: * @param series the series index (zero-based). 167: * @param category the category index (zero-based). 168: * 169: * @return The label (possibly <code>null</code>). 170: */ 171: public String generateLabel(XYDataset dataset, int series, int category) { 172: return null; //TODO: implement this method properly 173: } 174: 175: /** 176: * Returns an independent copy of the generator. 177: * 178: * @return A clone. 179: * 180: * @throws CloneNotSupportedException if cloning is not supported. 181: */ 182: public Object clone() throws CloneNotSupportedException { 183: 184: HighLowItemLabelGenerator clone 185: = (HighLowItemLabelGenerator) super.clone(); 186: 187: if (this.dateFormatter != null) { 188: clone.dateFormatter = (DateFormat) this.dateFormatter.clone(); 189: } 190: if (this.numberFormatter != null) { 191: clone.numberFormatter = (NumberFormat) this.numberFormatter.clone(); 192: } 193: 194: return clone; 195: 196: } 197: 198: /** 199: * Tests if this object is equal to another. 200: * 201: * @param obj the other object. 202: * 203: * @return A boolean. 204: */ 205: public boolean equals(Object obj) { 206: if (obj == this) { 207: return true; 208: } 209: if (!(obj instanceof HighLowItemLabelGenerator)) { 210: return false; 211: } 212: HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj; 213: if (!this.dateFormatter.equals(generator.dateFormatter)) { 214: return false; 215: } 216: if (!this.numberFormatter.equals(generator.numberFormatter)) { 217: return false; 218: } 219: return true; 220: } 221: 222: /** 223: * Returns a hash code for this instance. 224: * 225: * @return A hash code. 226: */ 227: public int hashCode() { 228: int result = 127; 229: result = HashUtilities.hashCode(result, this.dateFormatter); 230: result = HashUtilities.hashCode(result, this.numberFormatter); 231: return result; 232: } 233: 234: }