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, 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: public String generateToolTip(XYDataset dataset, int series,
121: 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="
138: + this .dateFormatter.format(date);
139: if (high != null) {
140: result = result
141: + " High="
142: + this .numberFormatter.format(high
143: .doubleValue());
144: }
145: if (low != null) {
146: result = result
147: + " Low="
148: + this .numberFormatter.format(low
149: .doubleValue());
150: }
151: if (open != null) {
152: result = result
153: + " Open="
154: + this .numberFormatter.format(open
155: .doubleValue());
156: }
157: if (close != null) {
158: result = result
159: + " Close="
160: + this .numberFormatter.format(close
161: .doubleValue());
162: }
163: }
164:
165: }
166:
167: return result;
168:
169: }
170:
171: /**
172: * Generates a label for the specified item. The label is typically a
173: * formatted version of the data value, but any text can be used.
174: *
175: * @param dataset the dataset (<code>null</code> not permitted).
176: * @param series the series index (zero-based).
177: * @param category the category index (zero-based).
178: *
179: * @return The label (possibly <code>null</code>).
180: */
181: public String generateLabel(XYDataset dataset, int series,
182: int category) {
183: return null; //TODO: implement this method properly
184: }
185:
186: /**
187: * Returns an independent copy of the generator.
188: *
189: * @return A clone.
190: *
191: * @throws CloneNotSupportedException if cloning is not supported.
192: */
193: public Object clone() throws CloneNotSupportedException {
194:
195: HighLowItemLabelGenerator clone = (HighLowItemLabelGenerator) super
196: .clone();
197:
198: if (this .dateFormatter != null) {
199: clone.dateFormatter = (DateFormat) this .dateFormatter
200: .clone();
201: }
202: if (this .numberFormatter != null) {
203: clone.numberFormatter = (NumberFormat) this .numberFormatter
204: .clone();
205: }
206:
207: return clone;
208:
209: }
210:
211: /**
212: * Tests if this object is equal to another.
213: *
214: * @param obj the other object.
215: *
216: * @return A boolean.
217: */
218: public boolean equals(Object obj) {
219: if (obj == this ) {
220: return true;
221: }
222: if (!(obj instanceof HighLowItemLabelGenerator)) {
223: return false;
224: }
225: HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj;
226: if (!this .dateFormatter.equals(generator.dateFormatter)) {
227: return false;
228: }
229: if (!this .numberFormatter.equals(generator.numberFormatter)) {
230: return false;
231: }
232: return true;
233: }
234:
235: }
|