KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > HighLowItemLabelGenerator


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): David Basten;
34  *
35  * $Id: HighLowItemLabelGenerator.java,v 1.7.2.1 2005/10/25 20:49:02 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-Dec-2001 : Version 1 (DG);
40  * 16-Jan-2002 : Completed Javadocs (DG);
41  * 23-Apr-2002 : Added date to the tooltip string (DG);
42  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
43  * 21-Mar-2003 : Implemented Serializable (DG);
44  * 13-Aug-2003 : Implemented Cloneable (DG);
45  * 17-Nov-2003 : Implemented PublicCloneable (DG);
46  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
47  * 25-May-2004 : Added number formatter (see patch 890496) (DG);
48  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
49  * getYValue() (DG);
50  * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
51  *
52  */

53
54 package org.jfree.chart.labels;
55
56 import java.io.Serializable JavaDoc;
57 import java.text.DateFormat JavaDoc;
58 import java.text.NumberFormat JavaDoc;
59 import java.util.Date JavaDoc;
60
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,
71                                                   Cloneable JavaDoc,
72                                                   PublicCloneable,
73                                                   Serializable JavaDoc {
74
75     /** For serialization. */
76     private static final long serialVersionUID = 5617111754832211830L;
77     
78     /** The date formatter. */
79     private DateFormat JavaDoc dateFormatter;
80
81     /** The number formatter. */
82     private NumberFormat JavaDoc numberFormatter;
83
84     /**
85      * Creates an item label generator using the default date and number
86      * formats.
87      */

88     public HighLowItemLabelGenerator() {
89         this(DateFormat.getInstance(), NumberFormat.getInstance());
90     }
91
92     /**
93      * Creates a tool tip generator using the supplied date formatter.
94      *
95      * @param dateFormatter the date formatter (<code>null</code> not
96      * permitted).
97      * @param numberFormatter the number formatter (<code>null</code> not
98      * permitted).
99      */

100     public HighLowItemLabelGenerator(DateFormat JavaDoc dateFormatter,
101                                      NumberFormat JavaDoc numberFormatter) {
102         if (dateFormatter == null) {
103             throw new IllegalArgumentException JavaDoc(
104                 "Null 'dateFormatter' argument."
105             );
106         }
107         if (numberFormatter == null) {
108             throw new IllegalArgumentException JavaDoc(
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 JavaDoc generateToolTip(XYDataset dataset, int series, int item) {
126
127         String JavaDoc result = null;
128
129         if (dataset instanceof OHLCDataset) {
130             OHLCDataset d = (OHLCDataset) dataset;
131             Number JavaDoc high = d.getHigh(series, item);
132             Number JavaDoc low = d.getLow(series, item);
133             Number JavaDoc open = d.getOpen(series, item);
134             Number JavaDoc close = d.getClose(series, item);
135             Number JavaDoc x = d.getX(series, item);
136
137             result = d.getSeriesKey(series).toString();
138
139             if (x != null) {
140                 Date JavaDoc date = new Date JavaDoc(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 JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
188         
189         HighLowItemLabelGenerator clone
190             = (HighLowItemLabelGenerator) super.clone();
191
192         if (this.dateFormatter != null) {
193             clone.dateFormatter = (DateFormat JavaDoc) this.dateFormatter.clone();
194         }
195         if (this.numberFormatter != null) {
196             clone.numberFormatter = (NumberFormat JavaDoc) 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 JavaDoc 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 }
228
Popular Tags