001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------------------ 028 * HighLowItemLabelGenerator.java 029 * ------------------------------ 030 * (C) Copyright 2001-2005, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): David Basten; 034 * 035 * $Id: HighLowItemLabelGenerator.java,v 1.7.2.1 2005/10/25 20:49:02 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 13-Dec-2001 : Version 1 (DG); 040 * 16-Jan-2002 : Completed Javadocs (DG); 041 * 23-Apr-2002 : Added date to the tooltip string (DG); 042 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 043 * 21-Mar-2003 : Implemented Serializable (DG); 044 * 13-Aug-2003 : Implemented Cloneable (DG); 045 * 17-Nov-2003 : Implemented PublicCloneable (DG); 046 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG); 047 * 25-May-2004 : Added number formatter (see patch 890496) (DG); 048 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 049 * getYValue() (DG); 050 * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG); 051 * 052 */ 053 054 package org.jfree.chart.labels; 055 056 import java.io.Serializable; 057 import java.text.DateFormat; 058 import java.text.NumberFormat; 059 import java.util.Date; 060 061 import org.jfree.data.xy.OHLCDataset; 062 import org.jfree.data.xy.XYDataset; 063 import org.jfree.util.PublicCloneable; 064 065 /** 066 * A standard item label generator for plots that use data from a 067 * {@link OHLCDataset}. 068 */ 069 public class HighLowItemLabelGenerator implements XYItemLabelGenerator, 070 XYToolTipGenerator, 071 Cloneable, 072 PublicCloneable, 073 Serializable { 074 075 /** For serialization. */ 076 private static final long serialVersionUID = 5617111754832211830L; 077 078 /** The date formatter. */ 079 private DateFormat dateFormatter; 080 081 /** The number formatter. */ 082 private NumberFormat numberFormatter; 083 084 /** 085 * Creates an item label generator using the default date and number 086 * formats. 087 */ 088 public HighLowItemLabelGenerator() { 089 this(DateFormat.getInstance(), NumberFormat.getInstance()); 090 } 091 092 /** 093 * Creates a tool tip generator using the supplied date formatter. 094 * 095 * @param dateFormatter the date formatter (<code>null</code> not 096 * permitted). 097 * @param numberFormatter the number formatter (<code>null</code> not 098 * permitted). 099 */ 100 public HighLowItemLabelGenerator(DateFormat dateFormatter, 101 NumberFormat numberFormatter) { 102 if (dateFormatter == null) { 103 throw new IllegalArgumentException( 104 "Null 'dateFormatter' argument." 105 ); 106 } 107 if (numberFormatter == null) { 108 throw new IllegalArgumentException( 109 "Null 'numberFormatter' argument." 110 ); 111 } 112 this.dateFormatter = dateFormatter; 113 this.numberFormatter = numberFormatter; 114 } 115 116 /** 117 * Generates a tooltip text item for a particular item within a series. 118 * 119 * @param dataset the dataset. 120 * @param series the series (zero-based index). 121 * @param item the item (zero-based index). 122 * 123 * @return The tooltip text. 124 */ 125 public String generateToolTip(XYDataset dataset, int series, int item) { 126 127 String result = null; 128 129 if (dataset instanceof OHLCDataset) { 130 OHLCDataset d = (OHLCDataset) dataset; 131 Number high = d.getHigh(series, item); 132 Number low = d.getLow(series, item); 133 Number open = d.getOpen(series, item); 134 Number close = d.getClose(series, item); 135 Number x = d.getX(series, item); 136 137 result = d.getSeriesKey(series).toString(); 138 139 if (x != null) { 140 Date date = new Date(x.longValue()); 141 result = result + "--> Date=" + this.dateFormatter.format(date); 142 if (high != null) { 143 result = result + " High=" 144 + this.numberFormatter.format(high.doubleValue()); 145 } 146 if (low != null) { 147 result = result + " Low=" 148 + this.numberFormatter.format(low.doubleValue()); 149 } 150 if (open != null) { 151 result = result + " Open=" 152 + this.numberFormatter.format(open.doubleValue()); 153 } 154 if (close != null) { 155 result = result + " Close=" 156 + this.numberFormatter.format(close.doubleValue()); 157 } 158 } 159 160 } 161 162 return result; 163 164 } 165 166 /** 167 * Generates a label for the specified item. The label is typically a 168 * formatted version of the data value, but any text can be used. 169 * 170 * @param dataset the dataset (<code>null</code> not permitted). 171 * @param series the series index (zero-based). 172 * @param category the category index (zero-based). 173 * 174 * @return The label (possibly <code>null</code>). 175 */ 176 public String generateLabel(XYDataset dataset, int series, int category) { 177 return null; //TODO: implement this method properly 178 } 179 180 /** 181 * Returns an independent copy of the generator. 182 * 183 * @return A clone. 184 * 185 * @throws CloneNotSupportedException if cloning is not supported. 186 */ 187 public Object clone() throws CloneNotSupportedException { 188 189 HighLowItemLabelGenerator clone 190 = (HighLowItemLabelGenerator) super.clone(); 191 192 if (this.dateFormatter != null) { 193 clone.dateFormatter = (DateFormat) this.dateFormatter.clone(); 194 } 195 if (this.numberFormatter != null) { 196 clone.numberFormatter = (NumberFormat) this.numberFormatter.clone(); 197 } 198 199 return clone; 200 201 } 202 203 /** 204 * Tests if this object is equal to another. 205 * 206 * @param obj the other object. 207 * 208 * @return A boolean. 209 */ 210 public boolean equals(Object obj) { 211 if (obj == this) { 212 return true; 213 } 214 if (!(obj instanceof HighLowItemLabelGenerator)) { 215 return false; 216 } 217 HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj; 218 if (!this.dateFormatter.equals(generator.dateFormatter)) { 219 return false; 220 } 221 if (!this.numberFormatter.equals(generator.numberFormatter)) { 222 return false; 223 } 224 return true; 225 } 226 227 }