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 * StandardPieToolTipGenerator.java
029 * --------------------------------
030 * (C) Copyright 2001-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Richard Atkinson;
034 *                   Andreas Schroeder;
035 *
036 */
037
038package org.jfree.chart.labels;
039
040import java.io.Serializable;
041import java.text.NumberFormat;
042import java.util.Locale;
043import org.jfree.chart.util.PublicCloneable;
044
045import org.jfree.data.general.PieDataset;
046
047/**
048 * A standard item label generator for plots that use data from a
049 * {@link PieDataset}.
050 * <p>
051 * For the label format, use {0} where the pie section key should be inserted,
052 * {1} for the absolute section value and {2} for the percent amount of the pie
053 * section, e.g. {@code "{0} = {1} ({2})"} will display as
054 * {@code apple = 120 (5%)}.
055 */
056public class StandardPieToolTipGenerator extends AbstractPieItemLabelGenerator
057        implements PieToolTipGenerator, Cloneable, PublicCloneable,
058            Serializable {
059
060    /** For serialization. */
061    private static final long serialVersionUID = 2995304200445733779L;
062
063    /** The default tooltip format. */
064    public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})";
065
066    /**
067     * Creates an item label generator using default number formatters.
068     */
069    public StandardPieToolTipGenerator() {
070        this(DEFAULT_TOOLTIP_FORMAT);
071    }
072
073    /**
074     * Creates a pie tool tip generator for the specified locale, using the
075     * default format string.
076     *
077     * @param locale  the locale ({@code null} not permitted).
078     */
079    public StandardPieToolTipGenerator(Locale locale) {
080        this(DEFAULT_TOOLTIP_FORMAT, locale);
081    }
082
083    /**
084     * Creates a pie tool tip generator for the default locale.
085     *
086     * @param labelFormat  the label format ({@code null} not permitted).
087     */
088    public StandardPieToolTipGenerator(String labelFormat) {
089        this(labelFormat, Locale.getDefault());
090    }
091
092    /**
093     * Creates a pie tool tip generator for the specified locale.
094     *
095     * @param labelFormat  the label format ({@code null} not permitted).
096     * @param locale  the locale ({@code null} not permitted).
097     */
098    public StandardPieToolTipGenerator(String labelFormat, Locale locale) {
099        this(labelFormat, NumberFormat.getNumberInstance(locale),
100                NumberFormat.getPercentInstance(locale));
101    }
102
103    /**
104     * Creates an item label generator using the specified number formatters.
105     *
106     * @param labelFormat  the label format string ({@code null} not
107     *                     permitted).
108     * @param numberFormat  the format object for the values ({@code null}
109     *                      not permitted).
110     * @param percentFormat  the format object for the percentages
111     *                       ({@code null} not permitted).
112     */
113    public StandardPieToolTipGenerator(String labelFormat,
114            NumberFormat numberFormat, NumberFormat percentFormat) {
115        super(labelFormat, numberFormat, percentFormat);
116    }
117
118    /**
119     * Generates a tool tip text item for one section in a pie chart.
120     *
121     * @param dataset  the dataset ({@code null} not permitted).
122     * @param key  the section key ({@code null} not permitted).
123     *
124     * @return The tool tip text (possibly {@code null}).
125     */
126    @Override
127    public String generateToolTip(PieDataset dataset, Comparable key) {
128        return generateSectionLabel(dataset, key);
129    }
130
131    /**
132     * Returns an independent copy of the generator.
133     *
134     * @return A clone.
135     *
136     * @throws CloneNotSupportedException  should not happen.
137     */
138    @Override
139    public Object clone() throws CloneNotSupportedException {
140        return super.clone();
141    }
142
143}