KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ----------------------------
23  * HighLowToolTipGenerator.java
24  * ----------------------------
25  * (C) Copyright 2001-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: HighLowToolTipGenerator.java,v 1.6 2003/11/17 09:46:22 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 13-Dec-2001 : Version 1 (DG);
35  * 16-Jan-2002 : Completed Javadocs (DG);
36  * 23-Apr-2002 : Added date to the tooltip string (DG);
37  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
38  * 21-Mar-2003 : Implemented Serializable (DG);
39  * 13-Aug-2003 : Implemented Cloneable (DG);
40  * 17-Nov-2003 : Implemented PublicCloneable (DG);
41  *
42  */

43
44 package org.jfree.chart.labels;
45
46 import java.io.Serializable JavaDoc;
47 import java.text.DateFormat JavaDoc;
48 import java.util.Date JavaDoc;
49
50 import org.jfree.data.HighLowDataset;
51 import org.jfree.data.XYDataset;
52 import org.jfree.util.PublicCloneable;
53
54 /**
55  * A standard tooltip generator for plots that use data from a {@link HighLowDataset}.
56  *
57  * @author David Gilbert
58  */

59 public class HighLowToolTipGenerator implements XYToolTipGenerator,
60                                                 Cloneable JavaDoc, PublicCloneable,
61                                                 Serializable JavaDoc {
62
63     /** The date formatter. */
64     private DateFormat JavaDoc dateFormatter;
65
66     /**
67      * Creates a tool tip generator using the default date format.
68      */

69     public HighLowToolTipGenerator() {
70         this(DateFormat.getInstance());
71     }
72
73     /**
74      * Creates a tool tip generator using the supplied date formatter.
75      *
76      * @param formatter the date formatter.
77      */

78     public HighLowToolTipGenerator(DateFormat JavaDoc formatter) {
79         this.dateFormatter = formatter;
80     }
81
82     /**
83      * Generates a tooltip text item for a particular item within a series.
84      *
85      * @param data the dataset.
86      * @param series the series (zero-based index).
87      * @param item the item (zero-based index).
88      *
89      * @return the tooltip text.
90      */

91     public String JavaDoc generateToolTip(XYDataset data, int series, int item) {
92
93         String JavaDoc result = null;
94
95         if (data instanceof HighLowDataset) {
96             HighLowDataset d = (HighLowDataset) data;
97             Number JavaDoc high = d.getHighValue(series, item);
98             Number JavaDoc low = d.getLowValue(series, item);
99             Number JavaDoc open = d.getOpenValue(series, item);
100             Number JavaDoc close = d.getCloseValue(series, item);
101             Number JavaDoc x = d.getXValue(series, item);
102
103             result = d.getSeriesName(series);
104
105             if (x != null) {
106                 Date JavaDoc date = new Date JavaDoc(x.longValue());
107                 result = result + "--> Date=" + dateFormatter.format(date);
108                 if (high != null) {
109                     result = result + " High=" + high.toString();
110                 }
111                 if (low != null) {
112                     result = result + " Low=" + low.toString();
113                 }
114                 if (open != null) {
115                     result = result + " Open=" + open.toString();
116                 }
117                 if (close != null) {
118                     result = result + " Close=" + close.toString();
119                 }
120             }
121
122         }
123
124         return result;
125
126     }
127
128     /**
129      * Returns an independent copy of the generator.
130      *
131      * @return A clone.
132      *
133      * @throws CloneNotSupportedException if cloning is not supported.
134      */

135     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
136         
137         HighLowToolTipGenerator clone = (HighLowToolTipGenerator) super.clone();
138
139         if (this.dateFormatter != null) {
140             clone.dateFormatter = (DateFormat JavaDoc) this.dateFormatter.clone();
141         }
142         
143         return clone;
144         
145     }
146     
147     /**
148      * Tests if this object is equal to another.
149      *
150      * @param o the other object.
151      *
152      * @return A boolean.
153      */

154     public boolean equals(Object JavaDoc o) {
155
156         if (o == null) {
157             return false;
158         }
159         if (o == this) {
160             return true;
161         }
162
163         if (o instanceof HighLowToolTipGenerator) {
164             HighLowToolTipGenerator generator = (HighLowToolTipGenerator) o;
165             return this.dateFormatter.equals(generator.dateFormatter);
166         }
167
168         return false;
169
170     }
171 }
172
Popular Tags