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 * StandardXYToolTipGenerator.java
029 * -------------------------------
030 * (C) Copyright 2004-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import java.text.DateFormat;
041import java.text.NumberFormat;
042import org.jfree.chart.util.PublicCloneable;
043
044import org.jfree.data.xy.XYDataset;
045
046/**
047 * A standard tool tip generator for use with an
048 * {@link org.jfree.chart.renderer.xy.XYItemRenderer}.
049 */
050public class StandardXYToolTipGenerator extends AbstractXYItemLabelGenerator
051        implements XYToolTipGenerator, Cloneable, PublicCloneable,
052                   Serializable {
053
054    /** For serialization. */
055    private static final long serialVersionUID = -3564164459039540784L;
056
057    /** The default tooltip format. */
058    public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2})";
059
060    /**
061     * Returns a tool tip generator that formats the x-values as dates and the
062     * y-values as numbers.
063     *
064     * @return A tool tip generator (never {@code null}).
065     */
066    public static StandardXYToolTipGenerator getTimeSeriesInstance() {
067        return new StandardXYToolTipGenerator(DEFAULT_TOOL_TIP_FORMAT,
068                DateFormat.getInstance(), NumberFormat.getInstance());
069    }
070
071    /**
072     * Creates a tool tip generator using default number formatters.
073     */
074    public StandardXYToolTipGenerator() {
075        this(DEFAULT_TOOL_TIP_FORMAT, NumberFormat.getNumberInstance(),
076                NumberFormat.getNumberInstance());
077    }
078
079    /**
080     * Creates a tool tip generator using the specified number formatters.
081     *
082     * @param formatString  the item label format string ({@code null} not
083     *                      permitted).
084     * @param xFormat  the format object for the x values ({@code null}
085     *                 not permitted).
086     * @param yFormat  the format object for the y values ({@code null}
087     *                 not permitted).
088     */
089    public StandardXYToolTipGenerator(String formatString,
090            NumberFormat xFormat, NumberFormat yFormat) {
091
092        super(formatString, xFormat, yFormat);
093
094    }
095
096    /**
097     * Creates a tool tip generator using the specified number formatters.
098     *
099     * @param formatString  the label format string ({@code null} not
100     *                      permitted).
101     * @param xFormat  the format object for the x values ({@code null}
102     *                 not permitted).
103     * @param yFormat  the format object for the y values ({@code null}
104     *                 not permitted).
105     */
106    public StandardXYToolTipGenerator(String formatString, DateFormat xFormat,
107            NumberFormat yFormat) {
108
109        super(formatString, xFormat, yFormat);
110
111    }
112
113    /**
114     * Creates a tool tip generator using the specified formatters (a
115     * number formatter for the x-values and a date formatter for the
116     * y-values).
117     *
118     * @param formatString  the item label format string ({@code null}
119     *                      not permitted).
120     * @param xFormat  the format object for the x values ({@code null}
121     *                 permitted).
122     * @param yFormat  the format object for the y values ({@code null}
123     *                 not permitted).
124     */
125    public StandardXYToolTipGenerator(String formatString,
126            NumberFormat xFormat, DateFormat yFormat) {
127
128        super(formatString, xFormat, yFormat);
129    }
130    /**
131     * Creates a tool tip generator using the specified date formatters.
132     *
133     * @param formatString  the label format string ({@code null} not
134     *                      permitted).
135     * @param xFormat  the format object for the x values ({@code null}
136     *                 not permitted).
137     * @param yFormat  the format object for the y values ({@code null}
138     *                 not permitted).
139     */
140    public StandardXYToolTipGenerator(String formatString,
141            DateFormat xFormat, DateFormat yFormat) {
142
143        super(formatString, xFormat, yFormat);
144
145    }
146
147    /**
148     * Generates the tool tip text for an item in a dataset.
149     *
150     * @param dataset  the dataset ({@code null} not permitted).
151     * @param series  the series index (zero-based).
152     * @param item  the item index (zero-based).
153     *
154     * @return The tooltip text (possibly {@code null}).
155     */
156    @Override
157    public String generateToolTip(XYDataset dataset, int series, int item) {
158        return generateLabelString(dataset, series, item);
159    }
160
161    /**
162     * Tests this object for equality with an arbitrary object.
163     *
164     * @param obj  the other object ({@code null} permitted).
165     *
166     * @return A boolean.
167     */
168    @Override
169    public boolean equals(Object obj) {
170        if (obj == this) {
171            return true;
172        }
173        if (!(obj instanceof StandardXYToolTipGenerator)) {
174            return false;
175        }
176        return super.equals(obj);
177    }
178
179    /**
180     * Returns an independent copy of the generator.
181     *
182     * @return A clone.
183     *
184     * @throws CloneNotSupportedException if cloning is not supported.
185     */
186    @Override
187    public Object clone() throws CloneNotSupportedException {
188        return super.clone();
189    }
190
191}