001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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-present, by Anthony Boulestreau and Contributors.
031 *
032 * Original Author:  Anthony Boulestreau;
033 * Contributor(s):   David Gilbert;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import org.jfree.chart.util.PublicCloneable;
041
042import org.jfree.data.time.RegularTimePeriod;
043import org.jfree.data.time.TimeSeriesCollection;
044import org.jfree.data.xy.XYDataset;
045import org.jfree.data.xy.XisSymbolic;
046import org.jfree.data.xy.YisSymbolic;
047
048/**
049 * A standard item label generator for plots that use data from an
050 * {@link XYDataset}.
051 */
052public class SymbolicXYItemLabelGenerator implements XYItemLabelGenerator,
053        XYToolTipGenerator, Cloneable, PublicCloneable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = 3963400354475494395L;
057
058    /**
059     * Generates a tool tip text item for a particular item within a series.
060     *
061     * @param data  the dataset.
062     * @param series  the series number (zero-based index).
063     * @param item  the item number (zero-based index).
064     *
065     * @return The tool tip text (possibly {@code null}).
066     */
067    @Override
068    public String generateToolTip(XYDataset data, int series, int item) {
069
070        String xStr, yStr;
071        if (data instanceof YisSymbolic) {
072            yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
073        }
074        else {
075            double y = data.getYValue(series, item);
076            yStr = Double.toString(round(y, 2));
077        }
078        if (data instanceof XisSymbolic) {
079            xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
080        }
081        else if (data instanceof TimeSeriesCollection) {
082            RegularTimePeriod p
083                = ((TimeSeriesCollection) data).getSeries(series)
084                    .getTimePeriod(item);
085            xStr = p.toString();
086        }
087        else {
088            double x = data.getXValue(series, item);
089            xStr = Double.toString(round(x, 2));
090        }
091        return "X: " + xStr + ", Y: " + yStr;
092    }
093
094    /**
095     * Generates a label for the specified item. The label is typically a
096     * formatted version of the data value, but any text can be used.
097     *
098     * @param dataset  the dataset ({@code null} not permitted).
099     * @param series  the series index (zero-based).
100     * @param category  the category index (zero-based).
101     *
102     * @return The label (possibly {@code null}).
103     */
104    @Override
105    public String generateLabel(XYDataset dataset, int series, int category) {
106        return null;  //TODO: implement this method properly
107    }
108
109    /**
110    * Round a double value.
111    *
112    * @param value  the value.
113    * @param nb  the exponent.
114    *
115    * @return The rounded value.
116    */
117    private static double round(double value, int nb) {
118        if (nb <= 0) {
119            return Math.floor(value + 0.5d);
120        }
121        double p = Math.pow(10, nb);
122        double tempval = Math.floor(value * p + 0.5d);
123        return tempval / p;
124    }
125
126    /**
127     * Returns an independent copy of the generator.
128     *
129     * @return A clone.
130     *
131     * @throws CloneNotSupportedException if cloning is not supported.
132     */
133    @Override
134    public Object clone() throws CloneNotSupportedException {
135        return super.clone();
136    }
137
138    /**
139     * Tests if this object is equal to another.
140     *
141     * @param obj  the other object.
142     *
143     * @return A boolean.
144     */
145    @Override
146    public boolean equals(Object obj) {
147        if (obj == this) {
148            return true;
149        }
150        if (obj instanceof SymbolicXYItemLabelGenerator) {
151            return true;
152        }
153        return false;
154    }
155
156    /**
157     * Returns a hash code for this instance.
158     *
159     * @return A hash code.
160     */
161    @Override
162    public int hashCode() {
163        int result = 127;
164        return result;
165    }
166
167}