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-2020, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Richard Atkinson; 034 * Andreas Schroeder; 035 * 036 */ 037 038package org.jfree.chart.labels; 039 040import java.io.Serializable; 041import java.text.NumberFormat; 042import java.util.Locale; 043import org.jfree.chart.util.PublicCloneable; 044 045import org.jfree.data.general.PieDataset; 046 047/** 048 * A standard item label generator for plots that use data from a 049 * {@link PieDataset}. 050 * <p> 051 * For the label format, use {0} where the pie section key should be inserted, 052 * {1} for the absolute section value and {2} for the percent amount of the pie 053 * section, e.g. {@code "{0} = {1} ({2})"} will display as 054 * {@code apple = 120 (5%)}. 055 */ 056public class StandardPieToolTipGenerator extends AbstractPieItemLabelGenerator 057 implements PieToolTipGenerator, Cloneable, PublicCloneable, 058 Serializable { 059 060 /** For serialization. */ 061 private static final long serialVersionUID = 2995304200445733779L; 062 063 /** The default tooltip format. */ 064 public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})"; 065 066 /** 067 * Creates an item label generator using default number formatters. 068 */ 069 public StandardPieToolTipGenerator() { 070 this(DEFAULT_TOOLTIP_FORMAT); 071 } 072 073 /** 074 * Creates a pie tool tip generator for the specified locale, using the 075 * default format string. 076 * 077 * @param locale the locale ({@code null} not permitted). 078 * 079 * @since 1.0.7 080 */ 081 public StandardPieToolTipGenerator(Locale locale) { 082 this(DEFAULT_TOOLTIP_FORMAT, locale); 083 } 084 085 /** 086 * Creates a pie tool tip generator for the default locale. 087 * 088 * @param labelFormat the label format ({@code null} not permitted). 089 */ 090 public StandardPieToolTipGenerator(String labelFormat) { 091 this(labelFormat, Locale.getDefault()); 092 } 093 094 /** 095 * Creates a pie tool tip generator for the specified locale. 096 * 097 * @param labelFormat the label format ({@code null} not permitted). 098 * @param locale the locale ({@code null} not permitted). 099 * 100 * @since 1.0.7 101 */ 102 public StandardPieToolTipGenerator(String labelFormat, Locale locale) { 103 this(labelFormat, NumberFormat.getNumberInstance(locale), 104 NumberFormat.getPercentInstance(locale)); 105 } 106 107 /** 108 * Creates an item label generator using the specified number formatters. 109 * 110 * @param labelFormat the label format string ({@code null} not 111 * permitted). 112 * @param numberFormat the format object for the values ({@code null} 113 * not permitted). 114 * @param percentFormat the format object for the percentages 115 * ({@code null} not permitted). 116 */ 117 public StandardPieToolTipGenerator(String labelFormat, 118 NumberFormat numberFormat, NumberFormat percentFormat) { 119 super(labelFormat, numberFormat, percentFormat); 120 } 121 122 /** 123 * Generates a tool tip text item for one section in a pie chart. 124 * 125 * @param dataset the dataset ({@code null} not permitted). 126 * @param key the section key ({@code null} not permitted). 127 * 128 * @return The tool tip text (possibly {@code null}). 129 */ 130 @Override 131 public String generateToolTip(PieDataset dataset, Comparable key) { 132 return generateSectionLabel(dataset, key); 133 } 134 135 /** 136 * Returns an independent copy of the generator. 137 * 138 * @return A clone. 139 * 140 * @throws CloneNotSupportedException should not happen. 141 */ 142 @Override 143 public Object clone() throws CloneNotSupportedException { 144 return super.clone(); 145 } 146 147}