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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------------------------
  27. * AbstractXYItemLabelGenerator.java
  28. * ---------------------------------
  29. * (C) Copyright 2004, 2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: AbstractXYItemLabelGenerator.java,v 1.5 2005/02/10 10:09:55 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 27-Feb-2004 : Version 1 (DG);
  39. * 12-May-2004 : Moved default tool tip format to
  40. * StandardXYToolTipGenerator (DG);
  41. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
  42. * getYValue() (DG);
  43. * 08-Oct-2004 : Modified createItemArray() method to handle null values (DG);
  44. * 10-Jan-2005 : Updated createItemArray() to use x, y primitives if
  45. * possible (DG);
  46. *
  47. */
  48. package org.jfree.chart.labels;
  49. import java.io.Serializable;
  50. import java.text.DateFormat;
  51. import java.text.MessageFormat;
  52. import java.text.NumberFormat;
  53. import java.util.Date;
  54. import org.jfree.data.xy.XYDataset;
  55. import org.jfree.util.ObjectUtilities;
  56. /**
  57. * A base class for creating item label generators.
  58. */
  59. public class AbstractXYItemLabelGenerator implements Cloneable, Serializable {
  60. /** The item label format string. */
  61. private String formatString;
  62. /** A number formatter for the x value. */
  63. private NumberFormat xFormat;
  64. /** A date formatter for the x value. */
  65. private DateFormat xDateFormat;
  66. /** A formatter for the y value. */
  67. private NumberFormat yFormat;
  68. /** A date formatter for the y value. */
  69. private DateFormat yDateFormat;
  70. /** The string used to represent 'null' for the x-value. */
  71. private String nullXString = "null";
  72. /** The string used to represent 'null' for the y-value. */
  73. private String nullYString = "null";
  74. /**
  75. * Creates an item label generator using default number formatters.
  76. */
  77. protected AbstractXYItemLabelGenerator() {
  78. this(
  79. "{2}", NumberFormat.getNumberInstance(),
  80. NumberFormat.getNumberInstance()
  81. );
  82. }
  83. /**
  84. * Creates an item label generator using the specified number formatters.
  85. *
  86. * @param formatString the item label format string (<code>null</code>
  87. * not permitted).
  88. * @param xFormat the format object for the x values (<code>null</code>
  89. * not permitted).
  90. * @param yFormat the format object for the y values (<code>null</code>
  91. * not permitted).
  92. */
  93. protected AbstractXYItemLabelGenerator(String formatString,
  94. NumberFormat xFormat,
  95. NumberFormat yFormat) {
  96. if (formatString == null) {
  97. throw new IllegalArgumentException("Null 'formatString' argument.");
  98. }
  99. if (xFormat == null) {
  100. throw new IllegalArgumentException("Null 'xFormat' argument.");
  101. }
  102. if (yFormat == null) {
  103. throw new IllegalArgumentException("Null 'yFormat' argument.");
  104. }
  105. this.formatString = formatString;
  106. this.xFormat = xFormat;
  107. this.yFormat = yFormat;
  108. }
  109. /**
  110. * Creates an item label generator using the specified number formatters.
  111. *
  112. * @param formatString the item label format string (<code>null</code>
  113. * not permitted).
  114. * @param xFormat the format object for the x values (<code>null</code>
  115. * permitted).
  116. * @param yFormat the format object for the y values (<code>null</code>
  117. * not permitted).
  118. */
  119. protected AbstractXYItemLabelGenerator(String formatString,
  120. DateFormat xFormat,
  121. NumberFormat yFormat) {
  122. this(formatString, NumberFormat.getInstance(), yFormat);
  123. this.xDateFormat = xFormat;
  124. }
  125. /**
  126. * Creates an item label generator using the specified number formatters.
  127. *
  128. * @param formatString the item label format string (<code>null</code>
  129. * not permitted).
  130. * @param xFormat the format object for the x values (<code>null</code>
  131. * permitted).
  132. * @param yFormat the format object for the y values (<code>null</code>
  133. * not permitted).
  134. */
  135. protected AbstractXYItemLabelGenerator(String formatString,
  136. DateFormat xFormat,
  137. DateFormat yFormat) {
  138. this(
  139. formatString, NumberFormat.getInstance(),
  140. NumberFormat.getInstance()
  141. );
  142. this.xDateFormat = xFormat;
  143. this.yDateFormat = yFormat;
  144. }
  145. /**
  146. * Returns the format string (this controls the overall structure of the
  147. * label).
  148. *
  149. * @return The format string (never <code>null</code>).
  150. */
  151. public String getFormatString() {
  152. return this.formatString;
  153. }
  154. /**
  155. * Returns the number formatter for the x-values.
  156. *
  157. * @return The number formatter (possibly <code>null</code>).
  158. */
  159. public NumberFormat getXFormat() {
  160. return this.xFormat;
  161. }
  162. /**
  163. * Returns the date formatter for the x-values.
  164. *
  165. * @return The date formatter (possibly <code>null</code>).
  166. */
  167. public DateFormat getXDateFormat() {
  168. return this.xDateFormat;
  169. }
  170. /**
  171. * Returns the number formatter for the y-values.
  172. *
  173. * @return the number formatter (possibly <code>null</code>).
  174. */
  175. public NumberFormat getYFormat() {
  176. return this.yFormat;
  177. }
  178. /**
  179. * Returns the date formatter for the y-values.
  180. *
  181. * @return The date formatter (possibly <code>null</code>).
  182. */
  183. public DateFormat getYDateFormat() {
  184. return this.yDateFormat;
  185. }
  186. /**
  187. * Generates a label string for an item in the dataset.
  188. *
  189. * @param dataset the dataset (<code>null</code> not permitted).
  190. * @param series the series (zero-based index).
  191. * @param item the item (zero-based index).
  192. *
  193. * @return The label (possibly <code>null</code>).
  194. */
  195. public String generateLabelString(XYDataset dataset, int series, int item) {
  196. String result = null;
  197. Object[] items = createItemArray(dataset, series, item);
  198. result = MessageFormat.format(this.formatString, items);
  199. return result;
  200. }
  201. /**
  202. * Creates the array of items that can be passed to the
  203. * {@link MessageFormat} class for creating labels.
  204. *
  205. * @param dataset the dataset (<code>null</code> not permitted).
  206. * @param series the series (zero-based index).
  207. * @param item the item (zero-based index).
  208. *
  209. * @return The items (never <code>null</code>).
  210. */
  211. protected Object[] createItemArray(XYDataset dataset, int series,
  212. int item) {
  213. Object[] result = new Object[3];
  214. result[0] = dataset.getSeriesName(series);
  215. double x = dataset.getXValue(series, item);
  216. if (Double.isNaN(x) && dataset.getX(series, item) == null) {
  217. result[1] = this.nullXString;
  218. }
  219. else {
  220. if (this.xDateFormat != null) {
  221. result[1] = this.xDateFormat.format(new Date((long) x));
  222. }
  223. else {
  224. result[1] = this.xFormat.format(x);
  225. }
  226. }
  227. double y = dataset.getYValue(series, item);
  228. if (Double.isNaN(y) && dataset.getY(series, item) == null) {
  229. result[2] = this.nullYString;
  230. }
  231. else {
  232. if (this.yDateFormat != null) {
  233. result[2] = this.yDateFormat.format(new Date((long) y));
  234. }
  235. else {
  236. result[2] = this.yFormat.format(y);
  237. }
  238. }
  239. return result;
  240. }
  241. /**
  242. * Tests this object for equality with an arbitrary object.
  243. *
  244. * @param obj the other object (<code>null</code> permitted).
  245. *
  246. * @return A boolean.
  247. */
  248. public boolean equals(Object obj) {
  249. if (obj == this) {
  250. return true;
  251. }
  252. if (!(obj instanceof AbstractXYItemLabelGenerator)) {
  253. return false;
  254. }
  255. AbstractXYItemLabelGenerator that = (AbstractXYItemLabelGenerator) obj;
  256. if (!this.formatString.equals(that.formatString)) {
  257. return false;
  258. }
  259. if (!ObjectUtilities.equal(this.xFormat, that.xFormat)) {
  260. return false;
  261. }
  262. if (!ObjectUtilities.equal(this.xDateFormat, that.xDateFormat)) {
  263. return false;
  264. }
  265. if (!ObjectUtilities.equal(this.yFormat, that.yFormat)) {
  266. return false;
  267. }
  268. if (!ObjectUtilities.equal(this.yDateFormat, that.yDateFormat)) {
  269. return false;
  270. }
  271. return true;
  272. }
  273. /**
  274. * Returns an independent copy of the generator.
  275. *
  276. * @return A clone.
  277. *
  278. * @throws CloneNotSupportedException if cloning is not supported.
  279. */
  280. public Object clone() throws CloneNotSupportedException {
  281. AbstractXYItemLabelGenerator clone
  282. = (AbstractXYItemLabelGenerator) super.clone();
  283. if (this.xFormat != null) {
  284. clone.xFormat = (NumberFormat) this.xFormat.clone();
  285. }
  286. if (this.yFormat != null) {
  287. clone.yFormat = (NumberFormat) this.yFormat.clone();
  288. }
  289. return clone;
  290. }
  291. }