001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * StandardPieToolTipGenerator.java 029 * -------------------------------- 030 * (C) Copyright 2001-2008, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Richard Atkinson; 034 * Andreas Schroeder; 035 * 036 * Changes 037 * ------- 038 * 13-Dec-2001 : Version 1 (DG); 039 * 16-Jan-2002 : Completed Javadocs (DG); 040 * 29-Aug-2002 : Changed to format numbers using default locale (RA); 041 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 042 * 30-Oct-2002 : Changed PieToolTipGenerator interface (DG); 043 * 21-Mar-2003 : Implemented Serializable (DG); 044 * 13-Aug-2003 : Implemented Cloneable (DG); 045 * 19-Aug-2003 : Renamed StandardPieToolTipGenerator --> 046 * StandardPieItemLabelGenerator (DG); 047 * 10-Mar-2004 : Modified to use MessageFormat class (DG); 048 * 31-Mar-2004 : Added javadocs for the MessageFormat usage (AS); 049 * 15-Apr-2004 : Split PieItemLabelGenerator interface into 050 * PieSectionLabelGenerator and PieToolTipGenerator (DG); 051 * 25-Nov-2004 : Moved some code into abstract super class (DG); 052 * 29-Jul-2005 : Removed implementation of PieSectionLabelGenerator 053 * interface (DG); 054 * 10-Jul-2007 : Added constructors with locale argument (DG); 055 * 056 */ 057 058package org.jfree.chart.labels; 059 060import java.io.Serializable; 061import java.text.NumberFormat; 062import java.util.Locale; 063import org.jfree.chart.util.PublicCloneable; 064 065import org.jfree.data.general.PieDataset; 066 067/** 068 * A standard item label generator for plots that use data from a 069 * {@link PieDataset}. 070 * <p> 071 * For the label format, use {0} where the pie section key should be inserted, 072 * {1} for the absolute section value and {2} for the percent amount of the pie 073 * section, e.g. {@code "{0} = {1} ({2})"} will display as 074 * {@code apple = 120 (5%)}. 075 */ 076public class StandardPieToolTipGenerator extends AbstractPieItemLabelGenerator 077 implements PieToolTipGenerator, Cloneable, PublicCloneable, 078 Serializable { 079 080 /** For serialization. */ 081 private static final long serialVersionUID = 2995304200445733779L; 082 083 /** The default tooltip format. */ 084 public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})"; 085 086 /** 087 * Creates an item label generator using default number formatters. 088 */ 089 public StandardPieToolTipGenerator() { 090 this(DEFAULT_TOOLTIP_FORMAT); 091 } 092 093 /** 094 * Creates a pie tool tip generator for the specified locale, using the 095 * default format string. 096 * 097 * @param locale the locale ({@code null} not permitted). 098 * 099 * @since 1.0.7 100 */ 101 public StandardPieToolTipGenerator(Locale locale) { 102 this(DEFAULT_TOOLTIP_FORMAT, locale); 103 } 104 105 /** 106 * Creates a pie tool tip generator for the default locale. 107 * 108 * @param labelFormat the label format ({@code null} not permitted). 109 */ 110 public StandardPieToolTipGenerator(String labelFormat) { 111 this(labelFormat, Locale.getDefault()); 112 } 113 114 /** 115 * Creates a pie tool tip generator for the specified locale. 116 * 117 * @param labelFormat the label format ({@code null} not permitted). 118 * @param locale the locale ({@code null} not permitted). 119 * 120 * @since 1.0.7 121 */ 122 public StandardPieToolTipGenerator(String labelFormat, Locale locale) { 123 this(labelFormat, NumberFormat.getNumberInstance(locale), 124 NumberFormat.getPercentInstance(locale)); 125 } 126 127 /** 128 * Creates an item label generator using the specified number formatters. 129 * 130 * @param labelFormat the label format string ({@code null} not 131 * permitted). 132 * @param numberFormat the format object for the values ({@code null} 133 * not permitted). 134 * @param percentFormat the format object for the percentages 135 * ({@code null} not permitted). 136 */ 137 public StandardPieToolTipGenerator(String labelFormat, 138 NumberFormat numberFormat, NumberFormat percentFormat) { 139 super(labelFormat, numberFormat, percentFormat); 140 } 141 142 /** 143 * Generates a tool tip text item for one section in a pie chart. 144 * 145 * @param dataset the dataset ({@code null} not permitted). 146 * @param key the section key ({@code null} not permitted). 147 * 148 * @return The tool tip text (possibly {@code null}). 149 */ 150 @Override 151 public String generateToolTip(PieDataset dataset, Comparable key) { 152 return generateSectionLabel(dataset, key); 153 } 154 155 /** 156 * Returns an independent copy of the generator. 157 * 158 * @return A clone. 159 * 160 * @throws CloneNotSupportedException should not happen. 161 */ 162 @Override 163 public Object clone() throws CloneNotSupportedException { 164 return super.clone(); 165 } 166 167}