001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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     * Creates a new default instance.
060     */
061    public SymbolicXYItemLabelGenerator() {
062        super();
063    }
064
065    /**
066     * Generates a tool tip text item for a particular item within a series.
067     *
068     * @param data  the dataset.
069     * @param series  the series number (zero-based index).
070     * @param item  the item number (zero-based index).
071     *
072     * @return The tool tip text (possibly {@code null}).
073     */
074    @Override
075    public String generateToolTip(XYDataset data, int series, int item) {
076
077        String xStr, yStr;
078        if (data instanceof YisSymbolic) {
079            yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
080        }
081        else {
082            double y = data.getYValue(series, item);
083            yStr = Double.toString(round(y, 2));
084        }
085        if (data instanceof XisSymbolic) {
086            xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
087        }
088        else if (data instanceof TimeSeriesCollection) {
089            RegularTimePeriod p
090                = ((TimeSeriesCollection) data).getSeries(series)
091                    .getTimePeriod(item);
092            xStr = p.toString();
093        }
094        else {
095            double x = data.getXValue(series, item);
096            xStr = Double.toString(round(x, 2));
097        }
098        return "X: " + xStr + ", Y: " + yStr;
099    }
100
101    /**
102     * Generates a label for the specified item. The label is typically a
103     * formatted version of the data value, but any text can be used.
104     *
105     * @param dataset  the dataset ({@code null} not permitted).
106     * @param series  the series index (zero-based).
107     * @param category  the category index (zero-based).
108     *
109     * @return The label (possibly {@code null}).
110     */
111    @Override
112    public String generateLabel(XYDataset dataset, int series, int category) {
113        return null;  //TODO: implement this method properly
114    }
115
116    /**
117    * Round a double value.
118    *
119    * @param value  the value.
120    * @param nb  the exponent.
121    *
122    * @return The rounded value.
123    */
124    private static double round(double value, int nb) {
125        if (nb <= 0) {
126            return Math.floor(value + 0.5d);
127        }
128        double p = Math.pow(10, nb);
129        double tempval = Math.floor(value * p + 0.5d);
130        return tempval / p;
131    }
132
133    /**
134     * Returns an independent copy of the generator.
135     *
136     * @return A clone.
137     *
138     * @throws CloneNotSupportedException if cloning is not supported.
139     */
140    @Override
141    public Object clone() throws CloneNotSupportedException {
142        return super.clone();
143    }
144
145    /**
146     * Tests if this object is equal to another.
147     *
148     * @param obj  the other object.
149     *
150     * @return A boolean.
151     */
152    @Override
153    public boolean equals(Object obj) {
154        if (obj == this) {
155            return true;
156        }
157        if (obj instanceof SymbolicXYItemLabelGenerator) {
158            return true;
159        }
160        return false;
161    }
162
163    /**
164     * Returns a hash code for this instance.
165     *
166     * @return A hash code.
167     */
168    @Override
169    public int hashCode() {
170        int result = 127;
171        return result;
172    }
173
174}