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. * StandardXYSeriesLabelGenerator.java
  26. * -----------------------------------
  27. * (C) Copyright 2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: StandardXYSeriesLabelGenerator.java,v 1.1 2004/11/25 12:24:12 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 16-Nov-2004 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.chart.labels;
  40. import java.io.Serializable;
  41. import java.text.MessageFormat;
  42. import org.jfree.data.xy.XYDataset;
  43. import org.jfree.util.PublicCloneable;
  44. /**
  45. * A standard series label generator for plots that use data from
  46. * an {@link org.jfree.data.xy.XYDataset}.
  47. */
  48. public class StandardXYSeriesLabelGenerator implements XYSeriesLabelGenerator,
  49. Cloneable,
  50. PublicCloneable,
  51. Serializable {
  52. /** The default item label format. */
  53. public static final String DEFAULT_LABEL_FORMAT = "{0}";
  54. private String formatPattern;
  55. /**
  56. * Creates an item label generator using default number formatters.
  57. */
  58. public StandardXYSeriesLabelGenerator() {
  59. this(DEFAULT_LABEL_FORMAT);
  60. }
  61. /**
  62. * Creates a new series label generator.
  63. *
  64. * @param format the format pattern (<code>null</code> not permitted).
  65. */
  66. public StandardXYSeriesLabelGenerator(String format) {
  67. if (format == null) {
  68. throw new IllegalArgumentException("Null 'format' argument.");
  69. }
  70. this.formatPattern = format;
  71. }
  72. /**
  73. * Generates a label for the specified series. This label will be
  74. * used for the chart legend.
  75. *
  76. * @param dataset the dataset (<code>null</code> not permitted).
  77. * @param series the series.
  78. *
  79. * @return A series label.
  80. */
  81. public String generateLabel(XYDataset dataset, int series) {
  82. if (dataset == null) {
  83. throw new IllegalArgumentException("Null 'dataset' argument.");
  84. }
  85. String label = MessageFormat.format(this.formatPattern, createItemArray(dataset, series));
  86. return label;
  87. }
  88. /**
  89. * Creates the array of items that can be passed to the {@link MessageFormat} class
  90. * for creating labels.
  91. *
  92. * @param dataset the dataset (<code>null</code> not permitted).
  93. * @param series the series (zero-based index).
  94. *
  95. * @return The items (never <code>null</code>).
  96. */
  97. protected Object[] createItemArray(XYDataset dataset, int series) {
  98. Object[] result = new Object[1];
  99. result[0] = dataset.getSeriesName(series);
  100. return result;
  101. }
  102. /**
  103. * Returns an independent copy of the generator.
  104. *
  105. * @return A clone.
  106. *
  107. * @throws CloneNotSupportedException if cloning is not supported.
  108. */
  109. public Object clone() throws CloneNotSupportedException {
  110. return super.clone();
  111. }
  112. /**
  113. * Tests this object for equality with an arbitrary object.
  114. *
  115. * @param obj the other object (<code>null</code> permitted).
  116. *
  117. * @return A boolean.
  118. */
  119. public boolean equals(Object obj) {
  120. if (obj == this) {
  121. return true;
  122. }
  123. if (!(obj instanceof StandardXYSeriesLabelGenerator)) {
  124. return false;
  125. }
  126. if (!super.equals(obj)) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. }