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