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. * StandardXYZToolTipGenerator.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: StandardXYZToolTipGenerator.java,v 1.2 2004/11/27 17:13:47 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 11-May-2003 : Version 1, split from StandardXYZItemLabelGenerator (DG);
  37. * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
  38. *
  39. */
  40. package org.jfree.chart.labels;
  41. import java.text.DateFormat;
  42. import java.text.MessageFormat;
  43. import java.text.NumberFormat;
  44. import org.jfree.data.xy.XYDataset;
  45. import org.jfree.data.xy.XYZDataset;
  46. import org.jfree.util.ObjectUtilities;
  47. /**
  48. * A standard item label generator for use with {@link XYZDataset} data. Each value
  49. * can be formatted as a number or as a date.
  50. *
  51. * TODO: add constructors for combinations of number and date formatters.
  52. */
  53. public class StandardXYZToolTipGenerator extends StandardXYToolTipGenerator
  54. implements XYZToolTipGenerator {
  55. /** The default tooltip format. */
  56. public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2}, {3})";
  57. /**
  58. * A number formatter for the z value - if this is null, then zDateFormat must
  59. * be non-null.
  60. */
  61. private NumberFormat zFormat;
  62. /**
  63. * A date formatter for the z-value - if this is null, then zFormat must be
  64. * non-null.
  65. */
  66. private DateFormat zDateFormat;
  67. /**
  68. * Creates a new tool tip generator using default number formatters for the
  69. * x, y and z-values.
  70. */
  71. public StandardXYZToolTipGenerator() {
  72. this(
  73. DEFAULT_TOOL_TIP_FORMAT,
  74. NumberFormat.getNumberInstance(),
  75. NumberFormat.getNumberInstance(),
  76. NumberFormat.getNumberInstance()
  77. );
  78. }
  79. /**
  80. * Constructs a new tool tip generator using the specified number formatters.
  81. *
  82. * @param formatString the format string.
  83. * @param xFormat the format object for the x values (<code>null</code> not permitted).
  84. * @param yFormat the format object for the y values (<code>null</code> not permitted).
  85. * @param zFormat the format object for the z values (<code>null</code> not permitted).
  86. */
  87. public StandardXYZToolTipGenerator(String formatString,
  88. NumberFormat xFormat,
  89. NumberFormat yFormat,
  90. NumberFormat zFormat) {
  91. super(formatString, xFormat, yFormat);
  92. if (zFormat == null) {
  93. throw new IllegalArgumentException("Null 'zFormat' argument.");
  94. }
  95. this.zFormat = zFormat;
  96. }
  97. /**
  98. * Constructs a new tool tip generator using the specified date formatters.
  99. *
  100. * @param formatString the format string.
  101. * @param xFormat the format object for the x values (<code>null</code> not permitted).
  102. * @param yFormat the format object for the y values (<code>null</code> not permitted).
  103. * @param zFormat the format object for the z values (<code>null</code> not permitted).
  104. */
  105. public StandardXYZToolTipGenerator(String formatString,
  106. DateFormat xFormat,
  107. DateFormat yFormat,
  108. DateFormat zFormat) {
  109. super(formatString, xFormat, yFormat);
  110. if (zFormat == null) {
  111. throw new IllegalArgumentException("Null 'zFormat' argument.");
  112. }
  113. this.zDateFormat = zFormat;
  114. }
  115. /**
  116. * Returns the number formatter for the z-values.
  117. *
  118. * @return The number formatter (possibly <code>null</code>).
  119. */
  120. public NumberFormat getZFormat() {
  121. return this.zFormat;
  122. }
  123. /**
  124. * Returns the date formatter for the z-values.
  125. *
  126. * @return The date formatter (possibly <code>null</code>).
  127. */
  128. public DateFormat getZDateFormat() {
  129. return this.zDateFormat;
  130. }
  131. /**
  132. * Generates a tool tip text item for a particular item within a series.
  133. *
  134. * @param dataset the dataset (<code>null</code> not permitted).
  135. * @param series the series index (zero-based).
  136. * @param item the item index (zero-based).
  137. *
  138. * @return The tooltip text (possibly <code>null</code>).
  139. */
  140. public String generateToolTip(XYZDataset dataset, int series, int item) {
  141. return generateLabelString(dataset, series, item);
  142. }
  143. /**
  144. * Generates a label string for an item in the dataset.
  145. *
  146. * @param dataset the dataset (<code>null</code> not permitted).
  147. * @param series the series (zero-based index).
  148. * @param item the item (zero-based index).
  149. *
  150. * @return The label (possibly <code>null</code>).
  151. */
  152. public String generateLabelString(XYDataset dataset, int series, int item) {
  153. String result = null;
  154. Object[] items = createItemArray((XYZDataset) dataset, series, item);
  155. result = MessageFormat.format(getFormatString(), items);
  156. return result;
  157. }
  158. /**
  159. * Creates the array of items that can be passed to the {@link MessageFormat} class
  160. * for creating labels.
  161. *
  162. * @param dataset the dataset (<code>null</code> not permitted).
  163. * @param series the series (zero-based index).
  164. * @param item the item (zero-based index).
  165. *
  166. * @return The items (never <code>null</code>).
  167. */
  168. protected Object[] createItemArray(XYZDataset dataset, int series, int item) {
  169. Object[] result = new Object[4];
  170. result[0] = dataset.getSeriesName(series);
  171. Number x = dataset.getX(series, item);
  172. DateFormat xf = getXDateFormat();
  173. if (xf != null) {
  174. result[1] = xf.format(x);
  175. }
  176. else {
  177. result[1] = getXFormat().format(x);
  178. }
  179. Number y = dataset.getY(series, item);
  180. DateFormat yf = getYDateFormat();
  181. if (yf != null) {
  182. result[2] = yf.format(y);
  183. }
  184. else {
  185. result[2] = getYFormat().format(y);
  186. }
  187. Number z = dataset.getZ(series, item);
  188. if (this.zDateFormat != null) {
  189. result[3] = this.zDateFormat.format(z);
  190. }
  191. else {
  192. result[3] = this.zFormat.format(z);
  193. }
  194. return result;
  195. }
  196. /**
  197. * Tests this object for equality with an arbitrary object.
  198. *
  199. * @param obj the other object (<code>null</code> permitted).
  200. *
  201. * @return A boolean.
  202. */
  203. public boolean equals(Object obj) {
  204. if (obj == this) {
  205. return true;
  206. }
  207. if (!(obj instanceof StandardXYZToolTipGenerator)) {
  208. return false;
  209. }
  210. if (!super.equals(obj)) {
  211. return false;
  212. }
  213. StandardXYZToolTipGenerator that = (StandardXYZToolTipGenerator) obj;
  214. if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) {
  215. return false;
  216. }
  217. if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) {
  218. return false;
  219. }
  220. return true;
  221. }
  222. }