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