    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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 * StandardXYItemLabelGenerator.java
    029 * ---------------------------------
    030 * (C) Copyright 2001-2009, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes
    036 * -------
    037 * 13-Dec-2001 : Version 1 (DG);
    038 * 16-Jan-2002 : Completed Javadocs (DG);
    039 * 02-Apr-2002 : Modified to handle null y-values (DG);
    040 * 09-Apr-2002 : Added formatting objects for the x and y values (DG);
    041 * 30-May-2002 : Added series name to standard tool tip (DG);
    042 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    043 * 23-Mar-2003 : Implemented Serializable (DG);
    044 * 13-Aug-2003 : Implemented Cloneable (DG);
    045 * 17-Nov-2003 : Implemented PublicCloneable (DG);
    046 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator and
    047 *               StandardXYToolTipGenerator -->
    048 *               StandardXYItemLabelGenerator (DG);
    049 * 26-Feb-2004 : Modified to use MessageFormat (DG);
    050 * 27-Feb-2004 : Added abstract superclass (DG);
    051 * 11-May-2004 : Split into StandardXYToolTipGenerator and
    052 *               StandardXYLabelGenerator (DG);
    053 * 20-Apr-2005 : Renamed StandardXYLabelGenerator
    054 *               --> StandardXYItemLabelGenerator (DG);
    055 * ------------- JFREECHART 1.0.x ---------------------------------------------
    056 * 25-Jan-2007 : Added new constructor - see bug 1624067 (DG);
    057 * 24-Jun-2009 : Added new constructor (DG);
    058 *
    059 */
    060
    061package org.jfree.chart.labels;
    062
    063import java.io.Serializable;
    064import java.text.DateFormat;
    065import java.text.NumberFormat;
    066
    067import org.jfree.data.xy.XYDataset;
    068import org.jfree.util.PublicCloneable;
    069
    070/**
    071 * A standard item label generator for plots that use data from an
    072 * {@link org.jfree.data.xy.XYDataset}.
    073 */
    074public class StandardXYItemLabelGenerator extends AbstractXYItemLabelGenerator
    075        implements XYItemLabelGenerator, Cloneable, PublicCloneable,
    076            Serializable {
    077
    078    /** For serialization. */
    079    private static final long serialVersionUID = 7807668053171837925L;
    080
    081    /** The default item label format. */
    082    public static final String DEFAULT_ITEM_LABEL_FORMAT = "{2}";
    083
    084    /**
    085     * Creates an item label generator using default number formatters.
    086     */
    087    public StandardXYItemLabelGenerator() {
    088        this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(),
    089                NumberFormat.getNumberInstance());
    090    }
    091
    092    /**
    093     * Creates an item label generator using the specified number formatters.
    094     *
    095     * @param formatString  the item label format string (<code>null</code> not
    096     *                      permitted).
    097     *
    098     * @since 1.0.14
    099     */
    100    public StandardXYItemLabelGenerator(String formatString) {
    101        this(formatString, NumberFormat.getNumberInstance(),
    102                NumberFormat.getNumberInstance());
    103    }
    104
    105    /**
    106     * Creates an item label generator using the specified number formatters.
    107     *
    108     * @param formatString  the item label format string (<code>null</code> not
    109     *                      permitted).
    110     * @param xFormat  the format object for the x values (<code>null</code>
    111     *                 not permitted).
    112     * @param yFormat  the format object for the y values (<code>null</code>
    113     *                 not permitted).
    114     */
    115    public StandardXYItemLabelGenerator(String formatString,
    116            NumberFormat xFormat, NumberFormat yFormat) {
    117
    118        super(formatString, xFormat, yFormat);
    119    }
    120
    121    /**
    122     * Creates an item label generator using the specified formatters.
    123     *
    124     * @param formatString  the item label format string (<code>null</code>
    125     *                      not permitted).
    126     * @param xFormat  the format object for the x values (<code>null</code>
    127     *                 not permitted).
    128     * @param yFormat  the format object for the y values (<code>null</code>
    129     *                 not permitted).
    130     */
    131    public StandardXYItemLabelGenerator(String formatString,
    132            DateFormat xFormat, NumberFormat yFormat) {
    133
    134        super(formatString, xFormat, yFormat);
    135    }
    136
    137    /**
    138     * Creates an item label generator using the specified formatters (a
    139     * number formatter for the x-values and a date formatter for the
    140     * y-values).
    141     *
    142     * @param formatString  the item label format string (<code>null</code>
    143     *                      not permitted).
    144     * @param xFormat  the format object for the x values (<code>null</code>
    145     *                 permitted).
    146     * @param yFormat  the format object for the y values (<code>null</code>
    147     *                 not permitted).
    148     *
    149     * @since 1.0.4
    150     */
    151    public StandardXYItemLabelGenerator(String formatString,
    152            NumberFormat xFormat, DateFormat yFormat) {
    153
    154        super(formatString, xFormat, yFormat);
    155    }
    156
    157    /**
    158     * Creates a label generator using the specified date formatters.
    159     *
    160     * @param formatString  the label format string (<code>null</code> not
    161     *                      permitted).
    162     * @param xFormat  the format object for the x values (<code>null</code>
    163     *                 not permitted).
    164     * @param yFormat  the format object for the y values (<code>null</code>
    165     *                 not permitted).
    166     */
    167    public StandardXYItemLabelGenerator(String formatString,
    168            DateFormat xFormat, DateFormat yFormat) {
    169
    170        super(formatString, xFormat, yFormat);
    171    }
    172
    173    /**
    174     * Generates the item label text for an item in a dataset.
    175     *
    176     * @param dataset  the dataset (<code>null</code> not permitted).
    177     * @param series  the series index (zero-based).
    178     * @param item  the item index (zero-based).
    179     *
    180     * @return The label text (possibly <code>null</code>).
    181     */
    182    public String generateLabel(XYDataset dataset, int series, int item) {
    183        return generateLabelString(dataset, series, item);
    184    }
    185
    186    /**
    187     * Returns an independent copy of the generator.
    188     *
    189     * @return A clone.
    190     *
    191     * @throws CloneNotSupportedException if cloning is not supported.
    192     */
    193    public Object clone() throws CloneNotSupportedException {
    194        return super.clone();
    195    }
    196
    197    /**
    198     * Tests this object for equality with an arbitrary object.
    199     *
    200     * @param obj  the other object (<code>null</code> permitted).
    201     *
    202     * @return A boolean.
    203     */
    204    public boolean equals(Object obj) {
    205        if (obj == this) {
    206            return true;
    207        }
    208        if (!(obj instanceof StandardXYItemLabelGenerator)) {
    209            return false;
    210        }
    211        return super.equals(obj);
    212    }
    213
    214}