Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2008, 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 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------------------------- 28: * BubbleXYItemLabelGenerator.java 29: * ------------------------------- 30: * (C) Copyright 2005-2008, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 13-Dec-2005 : Version 1, based on StandardXYZToolTipGenerator (DG); 38: * 26-Jan-2006 : Renamed StandardXYZItemLabelGenerator 39: * --> BubbleXYItemLabelGenerator (DG); 40: * 23-Nov-2007 : Implemented hashCode() (DG); 41: * 23-Apr-2008 : Implemented PublicCloneable (DG); 42: * 43: */ 44: 45: package org.jfree.chart.labels; 46: 47: import java.io.Serializable; 48: import java.text.DateFormat; 49: import java.text.MessageFormat; 50: import java.text.NumberFormat; 51: 52: import org.jfree.chart.HashUtilities; 53: import org.jfree.chart.renderer.xy.XYBubbleRenderer; 54: import org.jfree.data.xy.XYDataset; 55: import org.jfree.data.xy.XYZDataset; 56: import org.jfree.util.ObjectUtilities; 57: import org.jfree.util.PublicCloneable; 58: 59: /** 60: * An item label generator defined for use with the {@link XYBubbleRenderer} 61: * class, or any other class that uses an {@link XYZDataset}. 62: * 63: * @since 1.0.1 64: */ 65: public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator 66: implements XYItemLabelGenerator, PublicCloneable, Serializable { 67: 68: /** For serialization. */ 69: static final long serialVersionUID = -8458568928021240922L; 70: 71: /** The default item label format. */ 72: public static final String DEFAULT_FORMAT_STRING = "{3}"; 73: 74: /** 75: * A number formatter for the z value - if this is <code>null</code>, then 76: * zDateFormat must be non-null. 77: */ 78: private NumberFormat zFormat; 79: 80: /** 81: * A date formatter for the z-value - if this is null, then zFormat must be 82: * non-null. 83: */ 84: private DateFormat zDateFormat; 85: 86: /** 87: * Creates a new tool tip generator using default number formatters for the 88: * x, y and z-values. 89: */ 90: public BubbleXYItemLabelGenerator() { 91: this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(), 92: NumberFormat.getNumberInstance(), 93: NumberFormat.getNumberInstance()); 94: } 95: 96: /** 97: * Constructs a new tool tip generator using the specified number 98: * formatters. 99: * 100: * @param formatString the format string. 101: * @param xFormat the format object for the x values (<code>null</code> 102: * not permitted). 103: * @param yFormat the format object for the y values (<code>null</code> 104: * not permitted). 105: * @param zFormat the format object for the z values (<code>null</code> 106: * not permitted). 107: */ 108: public BubbleXYItemLabelGenerator(String formatString, 109: NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) { 110: super(formatString, xFormat, yFormat); 111: if (zFormat == null) { 112: throw new IllegalArgumentException("Null 'zFormat' argument."); 113: } 114: this.zFormat = zFormat; 115: } 116: 117: /** 118: * Constructs a new item label generator using the specified date 119: * formatters. 120: * 121: * @param formatString the format string. 122: * @param xFormat the format object for the x values (<code>null</code> 123: * not permitted). 124: * @param yFormat the format object for the y values (<code>null</code> 125: * not permitted). 126: * @param zFormat the format object for the z values (<code>null</code> 127: * not permitted). 128: */ 129: public BubbleXYItemLabelGenerator(String formatString, 130: DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) { 131: super(formatString, xFormat, yFormat); 132: if (zFormat == null) { 133: throw new IllegalArgumentException("Null 'zFormat' argument."); 134: } 135: this.zDateFormat = zFormat; 136: } 137: 138: /** 139: * Returns the number formatter for the z-values. 140: * 141: * @return The number formatter (possibly <code>null</code>). 142: */ 143: public NumberFormat getZFormat() { 144: return this.zFormat; 145: } 146: 147: /** 148: * Returns the date formatter for the z-values. 149: * 150: * @return The date formatter (possibly <code>null</code>). 151: */ 152: public DateFormat getZDateFormat() { 153: return this.zDateFormat; 154: } 155: 156: /** 157: * Generates an item label for a particular item within a series. 158: * 159: * @param dataset the dataset (<code>null</code> not permitted). 160: * @param series the series index (zero-based). 161: * @param item the item index (zero-based). 162: * 163: * @return The item label (possibly <code>null</code>). 164: */ 165: public String generateLabel(XYDataset dataset, int series, int item) { 166: return generateLabelString(dataset, series, item); 167: } 168: 169: /** 170: * Generates a label string for an item in the dataset. 171: * 172: * @param dataset the dataset (<code>null</code> not permitted). 173: * @param series the series (zero-based index). 174: * @param item the item (zero-based index). 175: * 176: * @return The label (possibly <code>null</code>). 177: */ 178: public String generateLabelString(XYDataset dataset, int series, int item) { 179: String result = null; 180: Object[] items = null; 181: if (dataset instanceof XYZDataset) { 182: items = createItemArray((XYZDataset) dataset, series, item); 183: } 184: else { 185: items = createItemArray(dataset, series, item); 186: } 187: result = MessageFormat.format(getFormatString(), items); 188: return result; 189: } 190: 191: /** 192: * Creates the array of items that can be passed to the 193: * {@link MessageFormat} class for creating labels. 194: * 195: * @param dataset the dataset (<code>null</code> not permitted). 196: * @param series the series (zero-based index). 197: * @param item the item (zero-based index). 198: * 199: * @return The items (never <code>null</code>). 200: */ 201: protected Object[] createItemArray(XYZDataset dataset, 202: int series, int item) { 203: 204: Object[] result = new Object[4]; 205: result[0] = dataset.getSeriesKey(series).toString(); 206: 207: Number x = dataset.getX(series, item); 208: DateFormat xf = getXDateFormat(); 209: if (xf != null) { 210: result[1] = xf.format(x); 211: } 212: else { 213: result[1] = getXFormat().format(x); 214: } 215: 216: Number y = dataset.getY(series, item); 217: DateFormat yf = getYDateFormat(); 218: if (yf != null) { 219: result[2] = yf.format(y); 220: } 221: else { 222: result[2] = getYFormat().format(y); 223: } 224: 225: Number z = dataset.getZ(series, item); 226: if (this.zDateFormat != null) { 227: result[3] = this.zDateFormat.format(z); 228: } 229: else { 230: result[3] = this.zFormat.format(z); 231: } 232: 233: return result; 234: 235: } 236: 237: /** 238: * Tests this object for equality with an arbitrary object. 239: * 240: * @param obj the other object (<code>null</code> permitted). 241: * 242: * @return A boolean. 243: */ 244: public boolean equals(Object obj) { 245: if (obj == this) { 246: return true; 247: } 248: if (!(obj instanceof BubbleXYItemLabelGenerator)) { 249: return false; 250: } 251: if (!super.equals(obj)) { 252: return false; 253: } 254: BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj; 255: if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) { 256: return false; 257: } 258: if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) { 259: return false; 260: } 261: return true; 262: } 263: 264: /** 265: * Returns a hash code for this instance. 266: * 267: * @return A hash code. 268: */ 269: public int hashCode() { 270: int h = super.hashCode(); 271: h = HashUtilities.hashCode(h, this.zFormat); 272: h = HashUtilities.hashCode(h, this.zDateFormat); 273: return h; 274: } 275: 276: }