KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesToolTipGenerator.java
24  * -------------------------------
25  * (C) Copyright 2001, 2002, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Richard Atkinson;
29  *
30  * $Id: TimeSeriesToolTipGenerator.java,v 1.5 2003/11/17 09:46:22 mungady Exp $
31  *
32  * Changes (since 30-May-2002):
33  * ----------------------------
34  * 30-May-2002 : Added series name to tool tip (DG);
35  * 29-Aug-2002 : Modified so that series name is not shown if null (RA);
36  * 23-Mar-2003 : Implemented Serializable (DG);
37  * 13-Aug-2003 : Implemented Cloneable (DG);
38  * 17-Nov-2003 : Implemented PublicCloneable (DG);
39  *
40  */

41
42 package org.jfree.chart.labels;
43
44 import java.io.Serializable JavaDoc;
45 import java.text.DateFormat JavaDoc;
46 import java.text.DecimalFormat JavaDoc;
47 import java.text.NumberFormat JavaDoc;
48 import java.text.SimpleDateFormat JavaDoc;
49 import java.util.Date JavaDoc;
50
51 import org.jfree.data.XYDataset;
52 import org.jfree.util.PublicCloneable;
53
54 /**
55  * A standard tool tip generator for time series plots.
56  *
57  * @author David Gilbert
58  */

59 public class TimeSeriesToolTipGenerator implements XYToolTipGenerator,
60                                                    Cloneable JavaDoc, PublicCloneable,
61                                                    Serializable JavaDoc {
62
63     /** A formatter for the time. */
64     private DateFormat JavaDoc dateFormat;
65
66     /** A formatter for the value. */
67     private NumberFormat JavaDoc numberFormat;
68
69     /**
70      * Default constructor.
71      */

72     public TimeSeriesToolTipGenerator() {
73
74         this(DateFormat.getInstance(), NumberFormat.getNumberInstance());
75
76     }
77
78     /**
79      * Creates a tool tip generator with the specified date and number format strings.
80      *
81      * @param dateFormat the date format.
82      * @param valueFormat the value format.
83      */

84     public TimeSeriesToolTipGenerator(String JavaDoc dateFormat, String JavaDoc valueFormat) {
85         this(new SimpleDateFormat JavaDoc(dateFormat), new DecimalFormat JavaDoc(valueFormat));
86     }
87
88     /**
89      * Constructs a new tooltip generator using the specified number formats.
90      *
91      * @param dateFormat the date formatter.
92      * @param numberFormat the number formatter.
93      */

94     public TimeSeriesToolTipGenerator(DateFormat JavaDoc dateFormat, NumberFormat JavaDoc numberFormat) {
95         this.dateFormat = dateFormat;
96         this.numberFormat = numberFormat;
97     }
98
99     /**
100      * Returns the date formatter.
101      *
102      * @return the date formatter.
103      */

104     public DateFormat JavaDoc getDateFormat() {
105         return this.dateFormat;
106     }
107
108     /**
109      * Returns the number formatter.
110      *
111      * @return the number formatter.
112      */

113     public NumberFormat JavaDoc getNumberFormat() {
114         return this.numberFormat;
115     }
116
117     /**
118      * Generates a tool tip text item for a particular item within a series.
119      *
120      * @param data the dataset.
121      * @param series the series number (zero-based index).
122      * @param item the item number (zero-based index).
123      *
124      * @return the tool tip text.
125      */

126     public String JavaDoc generateToolTip(XYDataset data, int series, int item) {
127
128         String JavaDoc result = "";
129         String JavaDoc seriesName = data.getSeriesName(series);
130         if (seriesName != null) {
131             result += seriesName + ": ";
132         }
133         long x = data.getXValue(series, item).longValue();
134         result = result + "date = " + this.dateFormat.format(new Date JavaDoc(x));
135
136         Number JavaDoc y = data.getYValue(series, item);
137         if (y != null) {
138             result = result + ", value = " + this.numberFormat.format(y);
139         }
140         else {
141             result = result + ", value = null";
142         }
143
144         return result;
145     }
146
147     /**
148      * Returns an independent copy of the generator.
149      *
150      * @return A clone.
151      *
152      * @throws CloneNotSupportedException if cloning is not supported.
153      */

154     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
155         
156         TimeSeriesToolTipGenerator clone = (TimeSeriesToolTipGenerator) super.clone();
157
158         if (this.dateFormat != null) {
159             clone.dateFormat = (DateFormat JavaDoc) this.dateFormat.clone();
160         }
161         
162         if (this.numberFormat != null) {
163             clone.numberFormat = (NumberFormat JavaDoc) this.numberFormat.clone();
164         }
165         
166         return clone;
167         
168     }
169     
170     /**
171      * Tests if this object is equal to another.
172      *
173      * @param o the other object.
174      *
175      * @return A boolean.
176      */

177     public boolean equals(Object JavaDoc o) {
178
179         if (o == null) {
180             return false;
181         }
182         if (o == this) {
183             return true;
184         }
185
186         if (o instanceof TimeSeriesToolTipGenerator) {
187             TimeSeriesToolTipGenerator generator = (TimeSeriesToolTipGenerator) o;
188             return (this.dateFormat.equals(generator.getDateFormat())
189                     && this.numberFormat.equals(generator.getNumberFormat()));
190         }
191         return false;
192
193     }
194
195 }
196
Popular Tags