KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SymbolicXYToolTipGenerator.java
24  * -------------------------------
25  * (C) Copyright 2001-2003, by Anthony Boulestreau and Contributors.
26  *
27  * Original Author: Anthony Boulestreau;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $ Id $
31  *
32  * Changes
33  * -------
34  * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
35  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
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
46 import org.jfree.data.XYDataset;
47 import org.jfree.data.XisSymbolic;
48 import org.jfree.data.YisSymbolic;
49 import org.jfree.data.time.RegularTimePeriod;
50 import org.jfree.data.time.TimeSeriesCollection;
51 import org.jfree.util.PublicCloneable;
52
53 /**
54  * A standard tooltip generator for plots that use data from an {@link XYDataset}.
55  *
56  * @author Anthony Boulestreau
57  */

58 public class SymbolicXYToolTipGenerator implements XYToolTipGenerator,
59                                                    Cloneable JavaDoc, PublicCloneable,
60                                                    Serializable JavaDoc {
61
62     /**
63      * Generates a tool tip text item for a particular item within a series.
64      *
65      * @param data the dataset.
66      * @param series the series number (zero-based index).
67      * @param item the item number (zero-based index).
68      *
69      * @return the tool tip text.
70      */

71     public String JavaDoc generateToolTip(XYDataset data, int series, int item) {
72
73         String JavaDoc x, y;
74         if (data instanceof YisSymbolic) {
75             y = ((YisSymbolic) data).getYSymbolicValue(series, item);
76         }
77         else {
78             Number JavaDoc n = data.getYValue(series, item);
79             y = Double.toString(round(n.doubleValue(), 2));
80         }
81         if (data instanceof XisSymbolic) {
82             x = ((XisSymbolic) data).getXSymbolicValue(series, item);
83         }
84         else if (data instanceof TimeSeriesCollection) {
85             RegularTimePeriod p
86                 = ((TimeSeriesCollection) data).getSeries(series).getTimePeriod(item);
87             x = p.toString();
88         }
89         else {
90             Number JavaDoc n = data.getXValue(series, item);
91             x = Double.toString(round(n.doubleValue(), 2));
92         }
93         return "X: " + x + ", Y: " + y;
94     }
95
96     /**
97     * Round a double value.
98     *
99     * @param value the value.
100     * @param nb the exponent.
101     *
102     * @return the rounded value.
103     */

104     private static double round(double value, int nb) {
105         if (nb <= 0) {
106             return Math.floor(value + 0.5d);
107         }
108         double p = Math.pow(10, nb);
109         double tempval = Math.floor(value * p + 0.5d);
110         return tempval / p;
111     }
112     
113     /**
114      * Returns an independent copy of the generator.
115      *
116      * @return A clone.
117      *
118      * @throws CloneNotSupportedException if cloning is not supported.
119      */

120     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
121         
122         return super.clone();
123         
124     }
125
126     /**
127      * Tests if this object is equal to another.
128      *
129      * @param o the other object.
130      *
131      * @return A boolean.
132      */

133     public boolean equals(Object JavaDoc o) {
134
135         if (o == null) {
136             return false;
137         }
138         if (o == this) {
139             return true;
140         }
141
142         if (o instanceof SymbolicXYToolTipGenerator) {
143             return true;
144         }
145         return false;
146
147     }
148     
149 }
150
Popular Tags