1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ----------------------------------
  25. * StandardPieItemLabelGenerator.java
  26. * ----------------------------------
  27. * (C) Copyright 2001-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Richard Atkinson;
  31. * Andreas Schroeder;
  32. *
  33. * $Id: StandardPieItemLabelGenerator.java,v 1.3 2004/11/29 15:17:29 mungady Exp $
  34. *
  35. * Changes
  36. * -------
  37. * 13-Dec-2001 : Version 1 (DG);
  38. * 16-Jan-2002 : Completed Javadocs (DG);
  39. * 29-Aug-2002 : Changed to format numbers using default locale (RA);
  40. * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  41. * 30-Oct-2002 : Changed PieToolTipGenerator interface (DG);
  42. * 21-Mar-2003 : Implemented Serializable (DG);
  43. * 13-Aug-2003 : Implemented Cloneable (DG);
  44. * 19-Aug-2003 : Renamed StandardPieToolTipGenerator --> StandardPieItemLabelGenerator (DG);
  45. * 10-Mar-2004 : Modified to use MessageFormat class (DG);
  46. * 31-Mar-2004 : Added javadocs for the MessageFormat usage (AS);
  47. * 15-Apr-2004 : Split PieItemLabelGenerator interface into PieSectionLabelGenerator and
  48. * PieToolTipGenerator (DG);
  49. * 25-Nov-2004 : Moved some code into abstract super class (DG);
  50. *
  51. */
  52. package org.jfree.chart.labels;
  53. import java.io.Serializable;
  54. import java.text.MessageFormat;
  55. import java.text.NumberFormat;
  56. import org.jfree.data.general.PieDataset;
  57. import org.jfree.util.PublicCloneable;
  58. /**
  59. * A standard item label generator for plots that use data from a {@link PieDataset}.
  60. * <p>
  61. * For the label format, use {0} where the pie section key should be inserted,
  62. * {1} for the absolute section value and {2} for the percent amount of the pie
  63. * section, e.g. <code>"{0} = {1} ({2})"</code> will display as
  64. * <code>apple = 120 (5%)</code>.
  65. */
  66. public class StandardPieItemLabelGenerator extends AbstractPieItemLabelGenerator
  67. implements PieToolTipGenerator,
  68. PieSectionLabelGenerator,
  69. Cloneable, PublicCloneable, Serializable {
  70. /** The default tooltip format. */
  71. public static final String DEFAULT_TOOLTIP_FORMAT = "{0}: ({1}, {2})";
  72. /** The default section label format. */
  73. public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0} = {1}";
  74. /**
  75. * Creates an item label generator using default number formatters.
  76. */
  77. public StandardPieItemLabelGenerator() {
  78. this(
  79. DEFAULT_SECTION_LABEL_FORMAT,
  80. NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
  81. );
  82. }
  83. /**
  84. * Creates an item label generator.
  85. *
  86. * @param labelFormat the label format.
  87. */
  88. public StandardPieItemLabelGenerator(String labelFormat) {
  89. this(labelFormat, NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
  90. }
  91. /**
  92. * Creates an item label generator using the specified number formatters.
  93. *
  94. * @param labelFormat the label format string (<code>null</code> not permitted).
  95. * @param numberFormat the format object for the values (<code>null</code> not permitted).
  96. * @param percentFormat the format object for the percentages (<code>null</code> not
  97. * permitted).
  98. */
  99. public StandardPieItemLabelGenerator(String labelFormat,
  100. NumberFormat numberFormat,
  101. NumberFormat percentFormat) {
  102. super(labelFormat, numberFormat, percentFormat);
  103. }
  104. /**
  105. * Generates a label for a pie section.
  106. *
  107. * @param dataset the dataset (<code>null</code> not permitted).
  108. * @param key the section key (<code>null</code> not permitted).
  109. *
  110. * @return The label (possibly <code>null</code>).
  111. */
  112. public String generateSectionLabel(PieDataset dataset, Comparable key) {
  113. String result = null;
  114. if (dataset != null) {
  115. Object[] items = createItemArray(dataset, key);
  116. result = MessageFormat.format(getLabelFormat(), items);
  117. }
  118. return result;
  119. }
  120. /**
  121. * Generates a tool tip text item for one section in a pie chart.
  122. *
  123. * @param dataset the dataset (<code>null</code> not permitted).
  124. * @param key the section key (<code>null</code> not permitted).
  125. *
  126. * @return The tool tip text (possibly <code>null</code>).
  127. */
  128. public String generateToolTip(PieDataset dataset, Comparable key) {
  129. return generateSectionLabel(dataset, key);
  130. }
  131. /**
  132. * Returns an independent copy of the generator.
  133. *
  134. * @return A clone.
  135. *
  136. * @throws CloneNotSupportedException should not happen.
  137. */
  138. public Object clone() throws CloneNotSupportedException {
  139. return super.clone();
  140. }
  141. }