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: * HighLowItemLabelGenerator.java 29: * ------------------------------ 30: * (C) Copyright 2001-2007, 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: * 50: */ 51: 52: package org.jfree.chart.labels; 53: 54: import java.io.Serializable; 55: import java.text.DateFormat; 56: import java.text.NumberFormat; 57: import java.util.Date; 58: 59: import org.jfree.data.xy.OHLCDataset; 60: import org.jfree.data.xy.XYDataset; 61: import org.jfree.util.PublicCloneable; 62: 63: /** 64: * A standard item label generator for plots that use data from a 65: * {@link OHLCDataset}. 66: */ 67: public class HighLowItemLabelGenerator implements XYItemLabelGenerator, 68: XYToolTipGenerator, 69: Cloneable, 70: PublicCloneable, 71: Serializable { 72: 73: /** For serialization. */ 74: private static final long serialVersionUID = 5617111754832211830L; 75: 76: /** The date formatter. */ 77: private DateFormat dateFormatter; 78: 79: /** The number formatter. */ 80: private NumberFormat numberFormatter; 81: 82: /** 83: * Creates an item label generator using the default date and number 84: * formats. 85: */ 86: public HighLowItemLabelGenerator() { 87: this(DateFormat.getInstance(), NumberFormat.getInstance()); 88: } 89: 90: /** 91: * Creates a tool tip generator using the supplied date formatter. 92: * 93: * @param dateFormatter the date formatter (<code>null</code> not 94: * permitted). 95: * @param numberFormatter the number formatter (<code>null</code> not 96: * permitted). 97: */ 98: public HighLowItemLabelGenerator(DateFormat dateFormatter, 99: NumberFormat numberFormatter) { 100: if (dateFormatter == null) { 101: throw new IllegalArgumentException( 102: "Null 'dateFormatter' argument."); 103: } 104: if (numberFormatter == null) { 105: throw new IllegalArgumentException( 106: "Null 'numberFormatter' argument."); 107: } 108: this.dateFormatter = dateFormatter; 109: this.numberFormatter = numberFormatter; 110: } 111: 112: /** 113: * Generates a tooltip text item for a particular item within a series. 114: * 115: * @param dataset the dataset. 116: * @param series the series (zero-based index). 117: * @param item the item (zero-based index). 118: * 119: * @return The tooltip text. 120: */ 121: public String generateToolTip(XYDataset dataset, int series, int item) { 122: 123: String result = null; 124: 125: if (dataset instanceof OHLCDataset) { 126: OHLCDataset d = (OHLCDataset) dataset; 127: Number high = d.getHigh(series, item); 128: Number low = d.getLow(series, item); 129: Number open = d.getOpen(series, item); 130: Number close = d.getClose(series, item); 131: Number x = d.getX(series, item); 132: 133: result = d.getSeriesKey(series).toString(); 134: 135: if (x != null) { 136: Date date = new Date(x.longValue()); 137: result = result + "--> Date=" + this.dateFormatter.format(date); 138: if (high != null) { 139: result = result + " High=" 140: + this.numberFormatter.format(high.doubleValue()); 141: } 142: if (low != null) { 143: result = result + " Low=" 144: + this.numberFormatter.format(low.doubleValue()); 145: } 146: if (open != null) { 147: result = result + " Open=" 148: + this.numberFormatter.format(open.doubleValue()); 149: } 150: if (close != null) { 151: result = result + " Close=" 152: + this.numberFormatter.format(close.doubleValue()); 153: } 154: } 155: 156: } 157: 158: return result; 159: 160: } 161: 162: /** 163: * Generates a label for the specified item. The label is typically a 164: * formatted version of the data value, but any text can be used. 165: * 166: * @param dataset the dataset (<code>null</code> not permitted). 167: * @param series the series index (zero-based). 168: * @param category the category index (zero-based). 169: * 170: * @return The label (possibly <code>null</code>). 171: */ 172: public String generateLabel(XYDataset dataset, int series, int category) { 173: return null; //TODO: implement this method properly 174: } 175: 176: /** 177: * Returns an independent copy of the generator. 178: * 179: * @return A clone. 180: * 181: * @throws CloneNotSupportedException if cloning is not supported. 182: */ 183: public Object clone() throws CloneNotSupportedException { 184: 185: HighLowItemLabelGenerator clone 186: = (HighLowItemLabelGenerator) super.clone(); 187: 188: if (this.dateFormatter != null) { 189: clone.dateFormatter = (DateFormat) this.dateFormatter.clone(); 190: } 191: if (this.numberFormatter != null) { 192: clone.numberFormatter = (NumberFormat) this.numberFormatter.clone(); 193: } 194: 195: return clone; 196: 197: } 198: 199: /** 200: * Tests if this object is equal to another. 201: * 202: * @param obj the other object. 203: * 204: * @return A boolean. 205: */ 206: public boolean equals(Object obj) { 207: if (obj == this) { 208: return true; 209: } 210: if (!(obj instanceof HighLowItemLabelGenerator)) { 211: return false; 212: } 213: HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj; 214: if (!this.dateFormatter.equals(generator.dateFormatter)) { 215: return false; 216: } 217: if (!this.numberFormatter.equals(generator.numberFormatter)) { 218: return false; 219: } 220: return true; 221: } 222: 223: }