KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > StandardPieSectionLabelGenerator


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------------------------
28  * StandardPieSectionLabelGenerator.java
29  * -------------------------------------
30  * (C) Copyright 2004-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StandardPieSectionLabelGenerator.java,v 1.4.2.5 2006/05/03 10:46:36 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 09-Nov-2004 : Version 1, derived from StandardPieItemLabelGenerator (DG);
40  * 29-Jul-2005 : Removed unused generateToolTip() method (DG);
41  * ------------- JFREECHART 1.0.0 ---------------------------------------------
42  * 03-May-2006 : Modified DEFAULT_SECTION_LABEL_FORMAT (DG);
43  *
44  */

45
46 package org.jfree.chart.labels;
47
48 import java.awt.Font JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.font.TextAttribute JavaDoc;
51 import java.io.Serializable JavaDoc;
52 import java.text.AttributedString JavaDoc;
53 import java.text.NumberFormat JavaDoc;
54
55 import org.jfree.data.general.PieDataset;
56 import org.jfree.util.ObjectList;
57
58 /**
59  * A standard item label generator for plots that use data from a
60  * {@link PieDataset}.
61  * <p>
62  * For the label format, use {0} where the pie section key should be inserted,
63  * {1} for the absolute section value and {2} for the percent amount of the pie
64  * section, e.g. <code>"{0} = {1} ({2})"</code> will display as
65  * <code>apple = 120 (5%)</code>.
66  */

67 public class StandardPieSectionLabelGenerator
68     extends AbstractPieItemLabelGenerator
69     implements PieSectionLabelGenerator, Cloneable JavaDoc, Serializable JavaDoc {
70
71     /** For serialization. */
72     private static final long serialVersionUID = 3064190563760203668L;
73     
74     /** The default section label format. */
75     public static final String JavaDoc DEFAULT_SECTION_LABEL_FORMAT = "{0}";
76
77     /**
78      * An optional list of attributed labels (instances of AttributedString).
79      */

80     private ObjectList attributedLabels;
81
82     /**
83      * Creates a new section label generator using
84      * {@link #DEFAULT_SECTION_LABEL_FORMAT} as the label format string, and
85      * platform default number and percentage formatters.
86      */

87     public StandardPieSectionLabelGenerator() {
88         this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(),
89                 NumberFormat.getPercentInstance());
90     }
91
92     /**
93      * Creates a new section label generator using the specified label format
94      * string, and platform default number and percentage formatters.
95      *
96      * @param labelFormat the label format (<code>null</code> not permitted).
97      */

98     public StandardPieSectionLabelGenerator(String JavaDoc labelFormat) {
99         this(labelFormat, NumberFormat.getNumberInstance(),
100                 NumberFormat.getPercentInstance());
101     }
102     
103     /**
104      * Creates an item label generator using the specified number formatters.
105      *
106      * @param labelFormat the label format string (<code>null</code> not
107      * permitted).
108      * @param numberFormat the format object for the values (<code>null</code>
109      * not permitted).
110      * @param percentFormat the format object for the percentages
111      * (<code>null</code> not permitted).
112      */

113     public StandardPieSectionLabelGenerator(String JavaDoc labelFormat,
114                                          NumberFormat JavaDoc numberFormat,
115                                          NumberFormat JavaDoc percentFormat) {
116
117         super(labelFormat, numberFormat, percentFormat);
118         this.attributedLabels = new ObjectList();
119
120     }
121
122     /**
123      * Returns the attributed label for a section, or <code>null</code> if none
124      * is defined.
125      *
126      * @param section the section index.
127      *
128      * @return The attributed label.
129      */

130     public AttributedString JavaDoc getAttributedLabel(int section) {
131         return (AttributedString JavaDoc) this.attributedLabels.get(section);
132     }
133     
134     /**
135      * Sets the attributed label for a section.
136      *
137      * @param section the section index.
138      * @param label the label (<code>null</code> permitted).
139      */

140     public void setAttributedLabel(int section, AttributedString JavaDoc label) {
141         this.attributedLabels.set(section, label);
142     }
143     
144     /**
145      * Generates a label for a pie section.
146      *
147      * @param dataset the dataset (<code>null</code> not permitted).
148      * @param key the section key (<code>null</code> not permitted).
149      *
150      * @return The label (possibly <code>null</code>).
151      */

152     public String JavaDoc generateSectionLabel(PieDataset dataset, Comparable JavaDoc key) {
153         return super.generateSectionLabel(dataset, key);
154     }
155
156     /**
157      * Generates an attributed label for the specified series, or
158      * <code>null</code> if no attributed label is available (in which case,
159      * the string returned by
160      * {@link #generateSectionLabel(PieDataset, Comparable)} will
161      * provide the fallback). Only certain attributes are recognised by the
162      * code that ultimately displays the labels:
163      * <ul>
164      * <li>{@link TextAttribute#FONT}: will set the font;</li>
165      * <li>{@link TextAttribute#POSTURE}: a value of
166      * {@link TextAttribute#POSTURE_OBLIQUE} will add {@link Font#ITALIC} to
167      * the current font;</li>
168      * <li>{@link TextAttribute#WEIGHT}: a value of
169      * {@link TextAttribute#WEIGHT_BOLD} will add {@link Font#BOLD} to the
170      * current font;</li>
171      * <li>{@link TextAttribute#FOREGROUND}: this will set the {@link Paint}
172      * for the current</li>
173      * <li>{@link TextAttribute#SUPERSCRIPT}: the values
174      * {@link TextAttribute#SUPERSCRIPT_SUB} and
175      * {@link TextAttribute#SUPERSCRIPT_SUPER} are recognised.</li>
176      * </ul>
177      *
178      * @param dataset the dataset (<code>null</code> not permitted).
179      * @param key the key.
180      *
181      * @return An attributed label (possibly <code>null</code>).
182      */

183     public AttributedString JavaDoc generateAttributedSectionLabel(PieDataset dataset,
184                                                            Comparable JavaDoc key) {
185         return getAttributedLabel(dataset.getIndex(key));
186     }
187
188     /**
189      * Tests the generator for equality with an arbitrary object.
190      *
191      * @param obj the object to test against (<code>null</code> permitted).
192      *
193      * @return A boolean.
194      */

195     public boolean equals(Object JavaDoc obj) {
196         if (obj == this) {
197             return true;
198         }
199         if (!(obj instanceof StandardPieSectionLabelGenerator)) {
200             return false;
201         }
202         if (!super.equals(obj)) {
203             return false;
204         }
205         return true;
206     }
207
208     /**
209      * Returns an independent copy of the generator.
210      *
211      * @return A clone.
212      *
213      * @throws CloneNotSupportedException should not happen.
214      */

215     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
216         return super.clone();
217     }
218
219 }
220
Popular Tags