KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SymbolicXYItemLabelGenerator.java
29  * ---------------------------------
30  * (C) Copyright 2001-2005, by Anthony Boulestreau and Contributors.
31  *
32  * Original Author: Anthony Boulestreau;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: SymbolicXYItemLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
40  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
41  * 23-Mar-2003 : Implemented Serializable (DG);
42  * 13-Aug-2003 : Implemented Cloneable (DG);
43  * 17-Nov-2003 : Implemented PublicCloneable (DG);
44  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
45  * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
46  * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
47  *
48  */

49
50 package org.jfree.chart.labels;
51
52 import java.io.Serializable JavaDoc;
53
54 import org.jfree.data.time.RegularTimePeriod;
55 import org.jfree.data.time.TimeSeriesCollection;
56 import org.jfree.data.xy.XYDataset;
57 import org.jfree.data.xy.XisSymbolic;
58 import org.jfree.data.xy.YisSymbolic;
59 import org.jfree.util.PublicCloneable;
60
61 /**
62  * A standard item label generator for plots that use data from an
63  * {@link XYDataset}.
64  *
65  * @author Anthony Boulestreau
66  */

67 public class SymbolicXYItemLabelGenerator implements XYItemLabelGenerator,
68                                                      XYToolTipGenerator,
69                                                      Cloneable JavaDoc,
70                                                      PublicCloneable,
71                                                      Serializable JavaDoc {
72
73     /** For serialization. */
74     private static final long serialVersionUID = 3963400354475494395L;
75     
76     /**
77      * Generates a tool tip text item for a particular item within a series.
78      *
79      * @param data the dataset.
80      * @param series the series number (zero-based index).
81      * @param item the item number (zero-based index).
82      *
83      * @return The tool tip text (possibly <code>null</code>).
84      */

85     public String JavaDoc generateToolTip(XYDataset data, int series, int item) {
86
87         String JavaDoc xStr, yStr;
88         if (data instanceof YisSymbolic) {
89             yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
90         }
91         else {
92             double y = data.getYValue(series, item);
93             yStr = Double.toString(round(y, 2));
94         }
95         if (data instanceof XisSymbolic) {
96             xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
97         }
98         else if (data instanceof TimeSeriesCollection) {
99             RegularTimePeriod p
100                 = ((TimeSeriesCollection) data).getSeries(series)
101                     .getTimePeriod(item);
102             xStr = p.toString();
103         }
104         else {
105             double x = data.getXValue(series, item);
106             xStr = Double.toString(round(x, 2));
107         }
108         return "X: " + xStr + ", Y: " + yStr;
109     }
110
111     /**
112      * Generates a label for the specified item. The label is typically a
113      * formatted version of the data value, but any text can be used.
114      *
115      * @param dataset the dataset (<code>null</code> not permitted).
116      * @param series the series index (zero-based).
117      * @param category the category index (zero-based).
118      *
119      * @return The label (possibly <code>null</code>).
120      */

121     public String JavaDoc generateLabel(XYDataset dataset, int series, int category) {
122         return null; //TODO: implement this method properly
123
}
124
125     /**
126     * Round a double value.
127     *
128     * @param value the value.
129     * @param nb the exponent.
130     *
131     * @return The rounded value.
132     */

133     private static double round(double value, int nb) {
134         if (nb <= 0) {
135             return Math.floor(value + 0.5d);
136         }
137         double p = Math.pow(10, nb);
138         double tempval = Math.floor(value * p + 0.5d);
139         return tempval / p;
140     }
141     
142     /**
143      * Returns an independent copy of the generator.
144      *
145      * @return A clone.
146      *
147      * @throws CloneNotSupportedException if cloning is not supported.
148      */

149     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
150         return super.clone();
151     }
152
153     /**
154      * Tests if this object is equal to another.
155      *
156      * @param obj the other object.
157      *
158      * @return A boolean.
159      */

160     public boolean equals(Object JavaDoc obj) {
161         if (obj == this) {
162             return true;
163         }
164         if (obj instanceof SymbolicXYItemLabelGenerator) {
165             return true;
166         }
167         return false;
168     }
169     
170 }
171
Popular Tags