KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------------------
27  * StandardPieItemLabelGenerator.java
28  * ----------------------------------
29  * (C) Copyright 2001-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Richard Atkinson;
33  * Andreas Schroeder;
34  *
35  * $Id: StandardPieItemLabelGenerator.java,v 1.5 2005/05/19 15:43:00 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-Dec-2001 : Version 1 (DG);
40  * 16-Jan-2002 : Completed Javadocs (DG);
41  * 29-Aug-2002 : Changed to format numbers using default locale (RA);
42  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
43  * 30-Oct-2002 : Changed PieToolTipGenerator interface (DG);
44  * 21-Mar-2003 : Implemented Serializable (DG);
45  * 13-Aug-2003 : Implemented Cloneable (DG);
46  * 19-Aug-2003 : Renamed StandardPieToolTipGenerator -->
47  * StandardPieItemLabelGenerator (DG);
48  * 10-Mar-2004 : Modified to use MessageFormat class (DG);
49  * 31-Mar-2004 : Added javadocs for the MessageFormat usage (AS);
50  * 15-Apr-2004 : Split PieItemLabelGenerator interface into
51  * PieSectionLabelGenerator and PieToolTipGenerator (DG);
52  * 25-Nov-2004 : Moved some code into abstract super class (DG);
53  *
54  */

55
56 package org.jfree.chart.labels;
57
58 import java.io.Serializable JavaDoc;
59 import java.text.MessageFormat JavaDoc;
60 import java.text.NumberFormat JavaDoc;
61
62 import org.jfree.data.general.PieDataset;
63 import org.jfree.util.PublicCloneable;
64
65 /**
66  * A standard item label generator for plots that use data from a
67  * {@link PieDataset}.
68  * <p>
69  * For the label format, use {0} where the pie section key should be inserted,
70  * {1} for the absolute section value and {2} for the percent amount of the pie
71  * section, e.g. <code>"{0} = {1} ({2})"</code> will display as
72  * <code>apple = 120 (5%)</code>.
73  */

74 public class StandardPieItemLabelGenerator extends AbstractPieItemLabelGenerator
75                                            implements PieToolTipGenerator,
76                                                       PieSectionLabelGenerator,
77                                                       Cloneable JavaDoc,
78                                                       PublicCloneable,
79                                                       Serializable JavaDoc {
80
81     /** For serialization. */
82     private static final long serialVersionUID = 2995304200445733779L;
83     
84     /** The default tooltip format. */
85     public static final String JavaDoc DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})";
86
87     /** The default section label format. */
88     public static final String JavaDoc DEFAULT_SECTION_LABEL_FORMAT = "{0} = {1}";
89
90     /**
91      * Creates an item label generator using default number formatters.
92      */

93     public StandardPieItemLabelGenerator() {
94         this(
95             DEFAULT_SECTION_LABEL_FORMAT,
96             NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
97         );
98     }
99
100     /**
101      * Creates an item label generator.
102      *
103      * @param labelFormat the label format.
104      */

105     public StandardPieItemLabelGenerator(String JavaDoc labelFormat) {
106         this(
107             labelFormat, NumberFormat.getNumberInstance(),
108             NumberFormat.getPercentInstance()
109         );
110     }
111     
112     /**
113      * Creates an item label generator using the specified number formatters.
114      *
115      * @param labelFormat the label format string (<code>null</code> not
116      * permitted).
117      * @param numberFormat the format object for the values (<code>null</code>
118      * not permitted).
119      * @param percentFormat the format object for the percentages
120      * (<code>null</code> not permitted).
121      */

122     public StandardPieItemLabelGenerator(String JavaDoc labelFormat,
123                                          NumberFormat JavaDoc numberFormat,
124                                          NumberFormat JavaDoc percentFormat) {
125
126         super(labelFormat, numberFormat, percentFormat);
127
128     }
129
130     /**
131      * Generates a label for a pie section.
132      *
133      * @param dataset the dataset (<code>null</code> not permitted).
134      * @param key the section key (<code>null</code> not permitted).
135      *
136      * @return The label (possibly <code>null</code>).
137      */

138     public String JavaDoc generateSectionLabel(PieDataset dataset, Comparable JavaDoc key) {
139         String JavaDoc result = null;
140         if (dataset != null) {
141             Object JavaDoc[] items = createItemArray(dataset, key);
142             result = MessageFormat.format(getLabelFormat(), items);
143         }
144         return result;
145     }
146
147     /**
148      * Generates a tool tip text item for one section in a pie chart.
149      *
150      * @param dataset the dataset (<code>null</code> not permitted).
151      * @param key the section key (<code>null</code> not permitted).
152      *
153      * @return The tool tip text (possibly <code>null</code>).
154      */

155     public String JavaDoc generateToolTip(PieDataset dataset, Comparable JavaDoc key) {
156         return generateSectionLabel(dataset, key);
157     }
158
159     /**
160      * Returns an independent copy of the generator.
161      *
162      * @return A clone.
163      *
164      * @throws CloneNotSupportedException should not happen.
165      */

166     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
167         return super.clone();
168     }
169
170 }
171
Popular Tags