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. * HighLowItemLabelGenerator.java
  26. * ------------------------------
  27. * (C) Copyright 2001-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): David Basten;
  31. *
  32. * $Id: HighLowItemLabelGenerator.java,v 1.2 2004/09/08 09:20:40 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 13-Dec-2001 : Version 1 (DG);
  37. * 16-Jan-2002 : Completed Javadocs (DG);
  38. * 23-Apr-2002 : Added date to the tooltip string (DG);
  39. * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 21-Mar-2003 : Implemented Serializable (DG);
  41. * 13-Aug-2003 : Implemented Cloneable (DG);
  42. * 17-Nov-2003 : Implemented PublicCloneable (DG);
  43. * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  44. * 25-May-2004 : Added number formatter (see patch 890496) (DG);
  45. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  46. *
  47. */
  48. package org.jfree.chart.labels;
  49. import java.io.Serializable;
  50. import java.text.DateFormat;
  51. import java.text.NumberFormat;
  52. import java.util.Date;
  53. import org.jfree.data.xy.OHLCDataset;
  54. import org.jfree.data.xy.XYDataset;
  55. import org.jfree.util.PublicCloneable;
  56. /**
  57. * A standard item label generator for plots that use data from a {@link OHLCDataset}.
  58. */
  59. public class HighLowItemLabelGenerator implements XYLabelGenerator,
  60. XYToolTipGenerator,
  61. Cloneable,
  62. PublicCloneable,
  63. Serializable {
  64. /** The date formatter. */
  65. private DateFormat dateFormatter;
  66. /** The number formatter. */
  67. private NumberFormat numberFormatter;
  68. /**
  69. * Creates an item label generator using the default date and number formats.
  70. */
  71. public HighLowItemLabelGenerator() {
  72. this(DateFormat.getInstance(), NumberFormat.getInstance());
  73. }
  74. /**
  75. * Creates a tool tip generator using the supplied date formatter.
  76. *
  77. * @param dateFormatter the date formatter (<code>null</code> not permitted).
  78. * @param numberFormatter the number formatter (<code>null</code> not permitted).
  79. */
  80. public HighLowItemLabelGenerator(DateFormat dateFormatter, NumberFormat numberFormatter) {
  81. if (dateFormatter == null) {
  82. throw new IllegalArgumentException("Null 'dateFormatter' argument.");
  83. }
  84. if (numberFormatter == null) {
  85. throw new IllegalArgumentException("Null 'numberFormatter' argument.");
  86. }
  87. this.dateFormatter = dateFormatter;
  88. this.numberFormatter = numberFormatter;
  89. }
  90. /**
  91. * Generates a tooltip text item for a particular item within a series.
  92. *
  93. * @param dataset the dataset.
  94. * @param series the series (zero-based index).
  95. * @param item the item (zero-based index).
  96. *
  97. * @return the tooltip text.
  98. */
  99. public String generateToolTip(XYDataset dataset, int series, int item) {
  100. String result = null;
  101. if (dataset instanceof OHLCDataset) {
  102. OHLCDataset d = (OHLCDataset) dataset;
  103. Number high = d.getHigh(series, item);
  104. Number low = d.getLow(series, item);
  105. Number open = d.getOpen(series, item);
  106. Number close = d.getClose(series, item);
  107. Number x = d.getX(series, item);
  108. result = d.getSeriesName(series);
  109. if (x != null) {
  110. Date date = new Date(x.longValue());
  111. result = result + "--> Date=" + this.dateFormatter.format(date);
  112. if (high != null) {
  113. result = result + " High=" + this.numberFormatter.format(high.doubleValue());
  114. }
  115. if (low != null) {
  116. result = result + " Low=" + this.numberFormatter.format(low.doubleValue());
  117. }
  118. if (open != null) {
  119. result = result + " Open=" + this.numberFormatter.format(open.doubleValue());
  120. }
  121. if (close != null) {
  122. result = result + " Close=" + this.numberFormatter.format(close.doubleValue());
  123. }
  124. }
  125. }
  126. return result;
  127. }
  128. /**
  129. * Generates a label for the specified item. The label is typically a formatted version of
  130. * the data value, but any text can be used.
  131. *
  132. * @param dataset the dataset (<code>null</code> not permitted).
  133. * @param series the series index (zero-based).
  134. * @param category the category index (zero-based).
  135. *
  136. * @return the label (possibly <code>null</code>).
  137. */
  138. public String generateLabel(XYDataset dataset, int series, int category) {
  139. return null; //TODO: implement this method properly
  140. }
  141. /**
  142. * Returns an independent copy of the generator.
  143. *
  144. * @return A clone.
  145. *
  146. * @throws CloneNotSupportedException if cloning is not supported.
  147. */
  148. public Object clone() throws CloneNotSupportedException {
  149. HighLowItemLabelGenerator clone = (HighLowItemLabelGenerator) super.clone();
  150. if (this.dateFormatter != null) {
  151. clone.dateFormatter = (DateFormat) this.dateFormatter.clone();
  152. }
  153. if (this.numberFormatter != null) {
  154. clone.numberFormatter = (NumberFormat) this.numberFormatter.clone();
  155. }
  156. return clone;
  157. }
  158. /**
  159. * Tests if this object is equal to another.
  160. *
  161. * @param o the other object.
  162. *
  163. * @return A boolean.
  164. */
  165. public boolean equals(Object o) {
  166. if (o == this) {
  167. return true;
  168. }
  169. if (o instanceof HighLowItemLabelGenerator) {
  170. HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) o;
  171. if (!this.dateFormatter.equals(generator.dateFormatter)) {
  172. return false;
  173. }
  174. if (!this.numberFormatter.equals(generator.numberFormatter)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. return false;
  180. }
  181. }