001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------------------ 028 * HighLowItemLabelGenerator.java 029 * ------------------------------ 030 * (C) Copyright 2001-2013, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): David Basten; 034 * 035 * Changes 036 * ------- 037 * 13-Dec-2001 : Version 1 (DG); 038 * 16-Jan-2002 : Completed Javadocs (DG); 039 * 23-Apr-2002 : Added date to the tooltip string (DG); 040 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 041 * 21-Mar-2003 : Implemented Serializable (DG); 042 * 13-Aug-2003 : Implemented Cloneable (DG); 043 * 17-Nov-2003 : Implemented PublicCloneable (DG); 044 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG); 045 * 25-May-2004 : Added number formatter (see patch 890496) (DG); 046 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 047 * getYValue() (DG); 048 * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG); 049 * 31-Mar-2008 : Added hashCode() method to appease FindBugs (DG); 050 * 051 */ 052 053package org.jfree.chart.labels; 054 055import java.io.Serializable; 056import java.text.DateFormat; 057import java.text.NumberFormat; 058import java.util.Date; 059 060import org.jfree.chart.HashUtilities; 061import org.jfree.data.xy.OHLCDataset; 062import org.jfree.data.xy.XYDataset; 063import org.jfree.util.PublicCloneable; 064 065/** 066 * A standard item label generator for plots that use data from a 067 * {@link OHLCDataset}. 068 */ 069public class HighLowItemLabelGenerator implements XYItemLabelGenerator, 070 XYToolTipGenerator, Cloneable, PublicCloneable, Serializable { 071 072 /** For serialization. */ 073 private static final long serialVersionUID = 5617111754832211830L; 074 075 /** The date formatter. */ 076 private DateFormat dateFormatter; 077 078 /** The number formatter. */ 079 private NumberFormat numberFormatter; 080 081 /** 082 * Creates an item label generator using the default date and number 083 * formats. 084 */ 085 public HighLowItemLabelGenerator() { 086 this(DateFormat.getInstance(), NumberFormat.getInstance()); 087 } 088 089 /** 090 * Creates a tool tip generator using the supplied date formatter. 091 * 092 * @param dateFormatter the date formatter (<code>null</code> not 093 * permitted). 094 * @param numberFormatter the number formatter (<code>null</code> not 095 * permitted). 096 */ 097 public HighLowItemLabelGenerator(DateFormat dateFormatter, 098 NumberFormat numberFormatter) { 099 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 @Override 121 public String generateToolTip(XYDataset dataset, int series, int item) { 122 if (!(dataset instanceof OHLCDataset)) { 123 return null; 124 } 125 StringBuilder sb = new StringBuilder(); 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 sb.append(d.getSeriesKey(series).toString()); 133 if (x != null) { 134 Date date = new Date(x.longValue()); 135 sb.append("--> Date=").append(this.dateFormatter.format(date)); 136 if (high != null) { 137 sb.append(" High="); 138 sb.append(this.numberFormatter.format(high.doubleValue())); 139 } 140 if (low != null) { 141 sb.append(" Low="); 142 sb.append(this.numberFormatter.format(low.doubleValue())); 143 } 144 if (open != null) { 145 sb.append(" Open="); 146 sb.append(this.numberFormatter.format(open.doubleValue())); 147 } 148 if (close != null) { 149 sb.append(" Close="); 150 sb.append(this.numberFormatter.format(close.doubleValue())); 151 } 152 } 153 return sb.toString(); 154 } 155 156 /** 157 * Generates a label for the specified item. The label is typically a 158 * formatted version of the data value, but any text can be used. 159 * 160 * @param dataset the dataset (<code>null</code> not permitted). 161 * @param series the series index (zero-based). 162 * @param category the category index (zero-based). 163 * 164 * @return The label (possibly <code>null</code>). 165 */ 166 @Override 167 public String generateLabel(XYDataset dataset, int series, int category) { 168 return null; //TODO: implement this method properly 169 } 170 171 /** 172 * Returns an independent copy of the generator. 173 * 174 * @return A clone. 175 * 176 * @throws CloneNotSupportedException if cloning is not supported. 177 */ 178 @Override 179 public Object clone() throws CloneNotSupportedException { 180 HighLowItemLabelGenerator clone 181 = (HighLowItemLabelGenerator) super.clone(); 182 if (this.dateFormatter != null) { 183 clone.dateFormatter = (DateFormat) this.dateFormatter.clone(); 184 } 185 if (this.numberFormatter != null) { 186 clone.numberFormatter = (NumberFormat) this.numberFormatter.clone(); 187 } 188 return clone; 189 } 190 191 /** 192 * Tests if this object is equal to another. 193 * 194 * @param obj the other object. 195 * 196 * @return A boolean. 197 */ 198 @Override 199 public boolean equals(Object obj) { 200 if (obj == this) { 201 return true; 202 } 203 if (!(obj instanceof HighLowItemLabelGenerator)) { 204 return false; 205 } 206 HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj; 207 if (!this.dateFormatter.equals(generator.dateFormatter)) { 208 return false; 209 } 210 if (!this.numberFormatter.equals(generator.numberFormatter)) { 211 return false; 212 } 213 return true; 214 } 215 216 /** 217 * Returns a hash code for this instance. 218 * 219 * @return A hash code. 220 */ 221 @Override 222 public int hashCode() { 223 int result = 127; 224 result = HashUtilities.hashCode(result, this.dateFormatter); 225 result = HashUtilities.hashCode(result, this.numberFormatter); 226 return result; 227 } 228 229}