1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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. * SymbolicXYItemLabelGenerator.java
  26. * ---------------------------------
  27. * (C) Copyright 2001-2005, by Anthony Boulestreau and Contributors.
  28. *
  29. * Original Author: Anthony Boulestreau;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: SymbolicXYItemLabelGenerator.java,v 1.2 2005/01/19 13:51:39 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
  37. * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 23-Mar-2003 : Implemented Serializable (DG);
  39. * 13-Aug-2003 : Implemented Cloneable (DG);
  40. * 17-Nov-2003 : Implemented PublicCloneable (DG);
  41. * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  42. * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
  43. *
  44. */
  45. package org.jfree.chart.labels;
  46. import java.io.Serializable;
  47. import org.jfree.data.time.RegularTimePeriod;
  48. import org.jfree.data.time.TimeSeriesCollection;
  49. import org.jfree.data.xy.XYDataset;
  50. import org.jfree.data.xy.XisSymbolic;
  51. import org.jfree.data.xy.YisSymbolic;
  52. import org.jfree.util.PublicCloneable;
  53. /**
  54. * A standard item label generator for plots that use data from an {@link XYDataset}.
  55. *
  56. * @author Anthony Boulestreau
  57. */
  58. public class SymbolicXYItemLabelGenerator implements XYLabelGenerator,
  59. XYToolTipGenerator,
  60. Cloneable,
  61. PublicCloneable,
  62. Serializable {
  63. /**
  64. * Generates a tool tip text item for a particular item within a series.
  65. *
  66. * @param data the dataset.
  67. * @param series the series number (zero-based index).
  68. * @param item the item number (zero-based index).
  69. *
  70. * @return The tool tip text (possibly <code>null</code>).
  71. */
  72. public String generateToolTip(XYDataset data, int series, int item) {
  73. String xStr, yStr;
  74. if (data instanceof YisSymbolic) {
  75. yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
  76. }
  77. else {
  78. double y = data.getYValue(series, item);
  79. yStr = Double.toString(round(y, 2));
  80. }
  81. if (data instanceof XisSymbolic) {
  82. xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
  83. }
  84. else if (data instanceof TimeSeriesCollection) {
  85. RegularTimePeriod p
  86. = ((TimeSeriesCollection) data).getSeries(series).getTimePeriod(item);
  87. xStr = p.toString();
  88. }
  89. else {
  90. double x = data.getXValue(series, item);
  91. xStr = Double.toString(round(x, 2));
  92. }
  93. return "X: " + xStr + ", Y: " + yStr;
  94. }
  95. /**
  96. * Generates a label for the specified item. The label is typically a formatted version of
  97. * the data value, but any text can be used.
  98. *
  99. * @param dataset the dataset (<code>null</code> not permitted).
  100. * @param series the series index (zero-based).
  101. * @param category the category index (zero-based).
  102. *
  103. * @return the label (possibly <code>null</code>).
  104. */
  105. public String generateLabel(XYDataset dataset, int series, int category) {
  106. return null; //TODO: implement this method properly
  107. }
  108. /**
  109. * Round a double value.
  110. *
  111. * @param value the value.
  112. * @param nb the exponent.
  113. *
  114. * @return the rounded value.
  115. */
  116. private static double round(double value, int nb) {
  117. if (nb <= 0) {
  118. return Math.floor(value + 0.5d);
  119. }
  120. double p = Math.pow(10, nb);
  121. double tempval = Math.floor(value * p + 0.5d);
  122. return tempval / p;
  123. }
  124. /**
  125. * Returns an independent copy of the generator.
  126. *
  127. * @return A clone.
  128. *
  129. * @throws CloneNotSupportedException if cloning is not supported.
  130. */
  131. public Object clone() throws CloneNotSupportedException {
  132. return super.clone();
  133. }
  134. /**
  135. * Tests if this object is equal to another.
  136. *
  137. * @param o the other object.
  138. *
  139. * @return A boolean.
  140. */
  141. public boolean equals(Object o) {
  142. if (o == this) {
  143. return true;
  144. }
  145. if (o instanceof SymbolicXYItemLabelGenerator) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. }