001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * StandardPieSectionLabelGenerator.java
029 * -------------------------------------
030 * (C) Copyright 2004-2020, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.awt.Font;
040import java.awt.Paint;
041import java.awt.font.TextAttribute;
042import java.io.Serializable;
043import java.text.AttributedString;
044import java.text.NumberFormat;
045import java.util.HashMap;
046import java.util.Locale;
047import java.util.Map;
048import org.jfree.chart.util.PublicCloneable;
049
050import org.jfree.data.general.PieDataset;
051
052/**
053 * A standard item label generator for plots that use data from a
054 * {@link PieDataset}.
055 * <p>
056 * For the label format, use {0} where the pie section key should be inserted,
057 * {1} for the absolute section value and {2} for the percent amount of the pie
058 * section, e.g. {@code "{0} = {1} ({2})"} will display as
059 * {@code apple = 120 (5%)}.
060 */
061public class StandardPieSectionLabelGenerator
062        extends AbstractPieItemLabelGenerator
063        implements PieSectionLabelGenerator, Cloneable, PublicCloneable,
064                   Serializable {
065
066    /** For serialization. */
067    private static final long serialVersionUID = 3064190563760203668L;
068
069    /** The default section label format. */
070    public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0}";
071
072    /**
073     * An optional map between item indices (Integer) and attributed labels 
074     * (instances of AttributedString).
075     */
076    private Map attributedLabels;
077
078    /**
079     * Creates a new section label generator using
080     * {@link #DEFAULT_SECTION_LABEL_FORMAT} as the label format string, and
081     * platform default number and percentage formatters.
082     */
083    public StandardPieSectionLabelGenerator() {
084        this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(),
085                NumberFormat.getPercentInstance());
086    }
087
088    /**
089     * Creates a new instance for the specified locale.
090     *
091     * @param locale  the local ({@code null} not permitted).
092     *
093     * @since 1.0.7
094     */
095    public StandardPieSectionLabelGenerator(Locale locale) {
096        this(DEFAULT_SECTION_LABEL_FORMAT, locale);
097    }
098
099    /**
100     * Creates a new section label generator using the specified label format
101     * string, and platform default number and percentage formatters.
102     *
103     * @param labelFormat  the label format ({@code null} not permitted).
104     */
105    public StandardPieSectionLabelGenerator(String labelFormat) {
106        this(labelFormat, NumberFormat.getNumberInstance(),
107                NumberFormat.getPercentInstance());
108    }
109
110    /**
111     * Creates a new instance for the specified locale.
112     *
113     * @param labelFormat  the label format ({@code null} not permitted).
114     * @param locale  the local ({@code null} not permitted).
115     *
116     * @since 1.0.7
117     */
118    public StandardPieSectionLabelGenerator(String labelFormat, Locale locale) {
119        this(labelFormat, NumberFormat.getNumberInstance(locale),
120                NumberFormat.getPercentInstance(locale));
121    }
122
123    /**
124     * Creates an item label generator using the specified number formatters.
125     *
126     * @param labelFormat  the label format string ({@code null} not
127     *                     permitted).
128     * @param numberFormat  the format object for the values ({@code null}
129     *                      not permitted).
130     * @param percentFormat  the format object for the percentages
131     *                       ({@code null} not permitted).
132     */
133    public StandardPieSectionLabelGenerator(String labelFormat,
134            NumberFormat numberFormat, NumberFormat percentFormat) {
135        super(labelFormat, numberFormat, percentFormat);
136        this.attributedLabels = new HashMap();
137    }
138
139    /**
140     * Returns the attributed label for a section, or {@code null} if none
141     * is defined.
142     *
143     * @param section  the section index.
144     *
145     * @return The attributed label.
146     */
147    public AttributedString getAttributedLabel(int section) {
148        return (AttributedString) this.attributedLabels.get(section);
149    }
150
151    /**
152     * Sets the attributed label for a section.
153     *
154     * @param section  the section index.
155     * @param label  the label ({@code null} permitted).
156     */
157    public void setAttributedLabel(int section, AttributedString label) {
158        this.attributedLabels.put(section, label);
159    }
160
161    /**
162     * Generates a label for a pie section.
163     *
164     * @param dataset  the dataset ({@code null} not permitted).
165     * @param key  the section key ({@code null} not permitted).
166     *
167     * @return The label (possibly {@code null}).
168     */
169    @Override
170    public String generateSectionLabel(PieDataset dataset, Comparable key) {
171        return super.generateSectionLabel(dataset, key);
172    }
173
174    /**
175     * Generates an attributed label for the specified series, or
176     * {@code null} if no attributed label is available (in which case,
177     * the string returned by
178     * {@link #generateSectionLabel(PieDataset, Comparable)} will
179     * provide the fallback).  Only certain attributes are recognised by the
180     * code that ultimately displays the labels:
181     * <ul>
182     * <li>{@link TextAttribute#FONT}: will set the font;</li>
183     * <li>{@link TextAttribute#POSTURE}: a value of
184     *     {@link TextAttribute#POSTURE_OBLIQUE} will add {@link Font#ITALIC} to
185     *     the current font;</li>
186     * <li>{@link TextAttribute#WEIGHT}: a value of
187     *     {@link TextAttribute#WEIGHT_BOLD} will add {@link Font#BOLD} to the
188     *     current font;</li>
189     * <li>{@link TextAttribute#FOREGROUND}: this will set the {@link Paint}
190     *     for the current</li>
191     * <li>{@link TextAttribute#SUPERSCRIPT}: the values
192     *     {@link TextAttribute#SUPERSCRIPT_SUB} and
193     *     {@link TextAttribute#SUPERSCRIPT_SUPER} are recognised.</li>
194     * </ul>
195     *
196     * @param dataset  the dataset ({@code null} not permitted).
197     * @param key  the key.
198     *
199     * @return An attributed label (possibly {@code null}).
200     */
201    @Override
202    public AttributedString generateAttributedSectionLabel(PieDataset dataset,
203            Comparable key) {
204        return getAttributedLabel(dataset.getIndex(key));
205    }
206
207    /**
208     * Tests the generator for equality with an arbitrary object.
209     *
210     * @param obj  the object to test against ({@code null} permitted).
211     *
212     * @return A boolean.
213     */
214    @Override
215    public boolean equals(Object obj) {
216        if (obj == this) {
217            return true;
218        }
219        if (!(obj instanceof StandardPieSectionLabelGenerator)) {
220            return false;
221        }
222        StandardPieSectionLabelGenerator that
223                = (StandardPieSectionLabelGenerator) obj;
224        if (!this.attributedLabels.equals(that.attributedLabels)) {
225            return false;
226        }
227        return super.equals(obj);
228    }
229
230    /**
231     * Returns an independent copy of the generator.
232     *
233     * @return A clone.
234     *
235     * @throws CloneNotSupportedException  should not happen.
236     */
237    @Override
238    public Object clone() throws CloneNotSupportedException {
239        StandardPieSectionLabelGenerator clone 
240                = (StandardPieSectionLabelGenerator) super.clone();        
241        clone.attributedLabels = new HashMap();
242        clone.attributedLabels.putAll(this.attributedLabels);
243        return clone;
244    }
245
246}