    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ----------------------------------
    028     * StandardPieItemLabelGenerator.java
    029     * ----------------------------------
    030     * (C) Copyright 2001-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   Richard Atkinson;
    034     *                   Andreas Schroeder;
    035     *
    036     * $Id: StandardPieToolTipGenerator.java,v 1.1.2.2 2005/10/25 20:49:02 mungady Exp $
    037     *
    038     * Changes
    039     * -------
    040     * 13-Dec-2001 : Version 1 (DG);
    041     * 16-Jan-2002 : Completed Javadocs (DG);
    042     * 29-Aug-2002 : Changed to format numbers using default locale (RA);
    043     * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    044     * 30-Oct-2002 : Changed PieToolTipGenerator interface (DG);
    045     * 21-Mar-2003 : Implemented Serializable (DG);
    046     * 13-Aug-2003 : Implemented Cloneable (DG);
    047     * 19-Aug-2003 : Renamed StandardPieToolTipGenerator -->
    048     *               StandardPieItemLabelGenerator (DG);
    049     * 10-Mar-2004 : Modified to use MessageFormat class (DG);
    050     * 31-Mar-2004 : Added javadocs for the MessageFormat usage (AS);
    051     * 15-Apr-2004 : Split PieItemLabelGenerator interface into
    052     *               PieSectionLabelGenerator and PieToolTipGenerator (DG);
    053     * 25-Nov-2004 : Moved some code into abstract super class (DG);
    054     * 29-Jul-2005 : Removed implementation of PieSectionLabelGenerator
    055     *               interface (DG);
    056     *
    057     */
    058
    059    package org.jfree.chart.labels;
    060
    061    import java.io.Serializable;
    062    import java.text.NumberFormat;
    063
    064    import org.jfree.data.general.PieDataset;
    065    import org.jfree.util.PublicCloneable;
    066
    067    /**
    068     * A standard item label generator for plots that use data from a
    069     * {@link PieDataset}.
    070     * <p>
    071     * For the label format, use {0} where the pie section key should be inserted,
    072     * {1} for the absolute section value and {2} for the percent amount of the pie
    073     * section, e.g. <code>"{0} = {1} ({2})"</code> will display as
    074     * <code>apple = 120 (5%)</code>.
    075     */
    076    public class StandardPieToolTipGenerator extends AbstractPieItemLabelGenerator
    077                                               implements PieToolTipGenerator,
    078                                                          Cloneable,
    079                                                          PublicCloneable,
    080                                                          Serializable {
    081
    082        /** For serialization. */
    083        private static final long serialVersionUID = 2995304200445733779L;
    084
    085        /** The default tooltip format. */
    086        public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})";
    087
    088        /** The default section label format. */
    089        public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0} = {1}";
    090
    091        /**
    092         * Creates an item label generator using default number formatters.
    093         */
    094        public StandardPieToolTipGenerator() {
    095            this(
    096                DEFAULT_SECTION_LABEL_FORMAT,
    097                NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
    098            );
    099        }
    100
    101        /**
    102         * Creates an item label generator.
    103         *
    104         * @param labelFormat  the label format.
    105         */
    106        public StandardPieToolTipGenerator(String labelFormat) {
    107            this(
    108                labelFormat, NumberFormat.getNumberInstance(),
    109                NumberFormat.getPercentInstance()
    110            );
    111        }
    112
    113        /**
    114         * Creates an item label generator using the specified number formatters.
    115         *
    116         * @param labelFormat  the label format string (<code>null</code> not
    117         *                     permitted).
    118         * @param numberFormat  the format object for the values (<code>null</code>
    119         *                      not permitted).
    120         * @param percentFormat  the format object for the percentages
    121         *                       (<code>null</code> not permitted).
    122         */
    123        public StandardPieToolTipGenerator(String labelFormat,
    124                                             NumberFormat numberFormat,
    125                                             NumberFormat percentFormat) {
    126
    127            super(labelFormat, numberFormat, percentFormat);
    128
    129        }
    130
    131        /**
    132         * Generates a tool tip text item for one section in a pie chart.
    133         *
    134         * @param dataset  the dataset (<code>null</code> not permitted).
    135         * @param key  the section key (<code>null</code> not permitted).
    136         *
    137         * @return The tool tip text (possibly <code>null</code>).
    138         */
    139        public String generateToolTip(PieDataset dataset, Comparable key) {
    140            return generateSectionLabel(dataset, key);
    141        }
    142
    143        /**
    144         * Returns an independent copy of the generator.
    145         *
    146         * @return A clone.
    147         *
    148         * @throws CloneNotSupportedException  should not happen.
    149         */
    150        public Object clone() throws CloneNotSupportedException {
    151            return super.clone();
    152        }
    153
    154    }