Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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: * StandardPieToolTipGenerator.java 29: * -------------------------------- 30: * (C) Copyright 2001-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): Richard Atkinson; 34: * Andreas Schroeder; 35: * 36: * Changes 37: * ------- 38: * 13-Dec-2001 : Version 1 (DG); 39: * 16-Jan-2002 : Completed Javadocs (DG); 40: * 29-Aug-2002 : Changed to format numbers using default locale (RA); 41: * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 42: * 30-Oct-2002 : Changed PieToolTipGenerator interface (DG); 43: * 21-Mar-2003 : Implemented Serializable (DG); 44: * 13-Aug-2003 : Implemented Cloneable (DG); 45: * 19-Aug-2003 : Renamed StandardPieToolTipGenerator --> 46: * StandardPieItemLabelGenerator (DG); 47: * 10-Mar-2004 : Modified to use MessageFormat class (DG); 48: * 31-Mar-2004 : Added javadocs for the MessageFormat usage (AS); 49: * 15-Apr-2004 : Split PieItemLabelGenerator interface into 50: * PieSectionLabelGenerator and PieToolTipGenerator (DG); 51: * 25-Nov-2004 : Moved some code into abstract super class (DG); 52: * 29-Jul-2005 : Removed implementation of PieSectionLabelGenerator 53: * interface (DG); 54: * 10-Jul-2007 : Added constructors with locale argument (DG); 55: * 56: */ 57: 58: package org.jfree.chart.labels; 59: 60: import java.io.Serializable; 61: import java.text.NumberFormat; 62: import java.util.Locale; 63: 64: import org.jfree.data.general.PieDataset; 65: import org.jfree.util.PublicCloneable; 66: 67: /** 68: * A standard item label generator for plots that use data from a 69: * {@link PieDataset}. 70: * <p> 71: * For the label format, use {0} where the pie section key should be inserted, 72: * {1} for the absolute section value and {2} for the percent amount of the pie 73: * section, e.g. <code>"{0} = {1} ({2})"</code> will display as 74: * <code>apple = 120 (5%)</code>. 75: */ 76: public class StandardPieToolTipGenerator extends AbstractPieItemLabelGenerator 77: implements PieToolTipGenerator, 78: Cloneable, 79: PublicCloneable, 80: Serializable { 81: 82: /** For serialization. */ 83: private static final long serialVersionUID = 2995304200445733779L; 84: 85: /** The default tooltip format. */ 86: public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})"; 87: 88: /** 89: * The default section label format. 90: * 91: * @deprecated As of 1.0.7, use {@link #DEFAULT_TOOLTIP_FORMAT} instead. 92: */ 93: public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0} = {1}"; 94: 95: /** 96: * Creates an item label generator using default number formatters. 97: */ 98: public StandardPieToolTipGenerator() { 99: this(DEFAULT_TOOLTIP_FORMAT); 100: } 101: 102: /** 103: * Creates a pie tool tip generator for the specified locale, using the 104: * default format string. 105: * 106: * @param locale the locale (<code>null</code> not permitted). 107: * 108: * @since 1.0.7 109: */ 110: public StandardPieToolTipGenerator(Locale locale) { 111: this(DEFAULT_TOOLTIP_FORMAT, locale); 112: } 113: 114: /** 115: * Creates a pie tool tip generator for the default locale. 116: * 117: * @param labelFormat the label format (<code>null</code> not permitted). 118: */ 119: public StandardPieToolTipGenerator(String labelFormat) { 120: this(labelFormat, Locale.getDefault()); 121: } 122: 123: /** 124: * Creates a pie tool tip generator for the specified locale. 125: * 126: * @param labelFormat the label format (<code>null</code> not permitted). 127: * @param locale the locale (<code>null</code> not permitted). 128: * 129: * @since 1.0.7 130: */ 131: public StandardPieToolTipGenerator(String labelFormat, Locale locale) { 132: this(labelFormat, NumberFormat.getNumberInstance(locale), 133: NumberFormat.getPercentInstance(locale)); 134: } 135: 136: /** 137: * Creates an item label generator using the specified number formatters. 138: * 139: * @param labelFormat the label format string (<code>null</code> not 140: * permitted). 141: * @param numberFormat the format object for the values (<code>null</code> 142: * not permitted). 143: * @param percentFormat the format object for the percentages 144: * (<code>null</code> not permitted). 145: */ 146: public StandardPieToolTipGenerator(String labelFormat, 147: NumberFormat numberFormat, NumberFormat percentFormat) { 148: super(labelFormat, numberFormat, percentFormat); 149: } 150: 151: /** 152: * Generates a tool tip text item for one section in a pie chart. 153: * 154: * @param dataset the dataset (<code>null</code> not permitted). 155: * @param key the section key (<code>null</code> not permitted). 156: * 157: * @return The tool tip text (possibly <code>null</code>). 158: */ 159: public String generateToolTip(PieDataset dataset, Comparable key) { 160: return generateSectionLabel(dataset, key); 161: } 162: 163: /** 164: * Returns an independent copy of the generator. 165: * 166: * @return A clone. 167: * 168: * @throws CloneNotSupportedException should not happen. 169: */ 170: public Object clone() throws CloneNotSupportedException { 171: return super.clone(); 172: } 173: 174: }