001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------------------------
028 * SymbolicXYItemLabelGenerator.java
029 * ---------------------------------
030 * (C) Copyright 2001-2008, by Anthony Boulestreau and Contributors.
031 *
032 * Original Author:  Anthony Boulestreau;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
038 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
039 * 23-Mar-2003 : Implemented Serializable (DG);
040 * 13-Aug-2003 : Implemented Cloneable (DG);
041 * 17-Nov-2003 : Implemented PublicCloneable (DG);
042 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
043 * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
044 * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
045 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
046 * 31-Mar-2008 : Added hashCode() method to appease FindBugs (DG);
047 *
048 */
049
050package org.jfree.chart.labels;
051
052import java.io.Serializable;
053
054import org.jfree.data.time.RegularTimePeriod;
055import org.jfree.data.time.TimeSeriesCollection;
056import org.jfree.data.xy.XYDataset;
057import org.jfree.data.xy.XisSymbolic;
058import org.jfree.data.xy.YisSymbolic;
059import org.jfree.util.PublicCloneable;
060
061/**
062 * A standard item label generator for plots that use data from an
063 * {@link XYDataset}.
064 */
065public class SymbolicXYItemLabelGenerator implements XYItemLabelGenerator,
066        XYToolTipGenerator, Cloneable, PublicCloneable, Serializable {
067
068    /** For serialization. */
069    private static final long serialVersionUID = 3963400354475494395L;
070
071    /**
072     * Generates a tool tip text item for a particular item within a series.
073     *
074     * @param data  the dataset.
075     * @param series  the series number (zero-based index).
076     * @param item  the item number (zero-based index).
077     *
078     * @return The tool tip text (possibly <code>null</code>).
079     */
080    @Override
081    public String generateToolTip(XYDataset data, int series, int item) {
082
083        String xStr, yStr;
084        if (data instanceof YisSymbolic) {
085            yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
086        }
087        else {
088            double y = data.getYValue(series, item);
089            yStr = Double.toString(round(y, 2));
090        }
091        if (data instanceof XisSymbolic) {
092            xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
093        }
094        else if (data instanceof TimeSeriesCollection) {
095            RegularTimePeriod p
096                = ((TimeSeriesCollection) data).getSeries(series)
097                    .getTimePeriod(item);
098            xStr = p.toString();
099        }
100        else {
101            double x = data.getXValue(series, item);
102            xStr = Double.toString(round(x, 2));
103        }
104        return "X: " + xStr + ", Y: " + yStr;
105    }
106
107    /**
108     * Generates a label for the specified item. The label is typically a
109     * formatted version of the data value, but any text can be used.
110     *
111     * @param dataset  the dataset (<code>null</code> not permitted).
112     * @param series  the series index (zero-based).
113     * @param category  the category index (zero-based).
114     *
115     * @return The label (possibly <code>null</code>).
116     */
117    @Override
118    public String generateLabel(XYDataset dataset, int series, int category) {
119        return null;  //TODO: implement this method properly
120    }
121
122    /**
123    * Round a double value.
124    *
125    * @param value  the value.
126    * @param nb  the exponent.
127    *
128    * @return The rounded value.
129    */
130    private static double round(double value, int nb) {
131        if (nb <= 0) {
132            return Math.floor(value + 0.5d);
133        }
134        double p = Math.pow(10, nb);
135        double tempval = Math.floor(value * p + 0.5d);
136        return tempval / p;
137    }
138
139    /**
140     * Returns an independent copy of the generator.
141     *
142     * @return A clone.
143     *
144     * @throws CloneNotSupportedException if cloning is not supported.
145     */
146    @Override
147    public Object clone() throws CloneNotSupportedException {
148        return super.clone();
149    }
150
151    /**
152     * Tests if this object is equal to another.
153     *
154     * @param obj  the other object.
155     *
156     * @return A boolean.
157     */
158    @Override
159    public boolean equals(Object obj) {
160        if (obj == this) {
161            return true;
162        }
163        if (obj instanceof SymbolicXYItemLabelGenerator) {
164            return true;
165        }
166        return false;
167    }
168
169    /**
170     * Returns a hash code for this instance.
171     *
172     * @return A hash code.
173     */
174    @Override
175    public int hashCode() {
176        int result = 127;
177        return result;
178    }
179
180}