001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------------------- 028 * BubbleXYItemLabelGenerator.java 029 * ------------------------------- 030 * (C) Copyright 2005-2007, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 13-Dec-2005 : Version 1, based on StandardXYZToolTipGenerator (DG); 038 * 26-Jan-2006 : Renamed StandardXYZItemLabelGenerator 039 * --> BubbleXYItemLabelGenerator (DG); 040 * 041 */ 042 043package org.jfree.chart.labels; 044 045import java.io.Serializable; 046import java.text.DateFormat; 047import java.text.MessageFormat; 048import java.text.NumberFormat; 049 050import org.jfree.chart.renderer.xy.XYBubbleRenderer; 051import org.jfree.data.xy.XYDataset; 052import org.jfree.data.xy.XYZDataset; 053import org.jfree.util.ObjectUtilities; 054 055/** 056 * An item label generator defined for use with the {@link XYBubbleRenderer} 057 * class, or any other class that uses an {@link XYZDataset}. 058 * 059 * @since 1.0.1 060 */ 061public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator 062 implements XYItemLabelGenerator, Serializable { 063 064 static final long serialVersionUID = -8458568928021240922L; 065 066 /** The default item label format. */ 067 public static final String DEFAULT_FORMAT_STRING = "{3}"; 068 069 /** 070 * A number formatter for the z value - if this is <code>null</code>, then 071 * zDateFormat must be non-null. 072 */ 073 private NumberFormat zFormat; 074 075 /** 076 * A date formatter for the z-value - if this is null, then zFormat must be 077 * non-null. 078 */ 079 private DateFormat zDateFormat; 080 081 /** 082 * Creates a new tool tip generator using default number formatters for the 083 * x, y and z-values. 084 */ 085 public BubbleXYItemLabelGenerator() { 086 this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(), 087 NumberFormat.getNumberInstance(), 088 NumberFormat.getNumberInstance()); 089 } 090 091 /** 092 * Constructs a new tool tip generator using the specified number 093 * formatters. 094 * 095 * @param formatString the format string. 096 * @param xFormat the format object for the x values (<code>null</code> 097 * not permitted). 098 * @param yFormat the format object for the y values (<code>null</code> 099 * not permitted). 100 * @param zFormat the format object for the z values (<code>null</code> 101 * not permitted). 102 */ 103 public BubbleXYItemLabelGenerator(String formatString, 104 NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) { 105 super(formatString, xFormat, yFormat); 106 if (zFormat == null) { 107 throw new IllegalArgumentException("Null 'zFormat' argument."); 108 } 109 this.zFormat = zFormat; 110 } 111 112 /** 113 * Constructs a new item label generator using the specified date 114 * formatters. 115 * 116 * @param formatString the format string. 117 * @param xFormat the format object for the x values (<code>null</code> 118 * not permitted). 119 * @param yFormat the format object for the y values (<code>null</code> 120 * not permitted). 121 * @param zFormat the format object for the z values (<code>null</code> 122 * not permitted). 123 */ 124 public BubbleXYItemLabelGenerator(String formatString, 125 DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) { 126 super(formatString, xFormat, yFormat); 127 if (zFormat == null) { 128 throw new IllegalArgumentException("Null 'zFormat' argument."); 129 } 130 this.zDateFormat = zFormat; 131 } 132 133 /** 134 * Returns the number formatter for the z-values. 135 * 136 * @return The number formatter (possibly <code>null</code>). 137 */ 138 public NumberFormat getZFormat() { 139 return this.zFormat; 140 } 141 142 /** 143 * Returns the date formatter for the z-values. 144 * 145 * @return The date formatter (possibly <code>null</code>). 146 */ 147 public DateFormat getZDateFormat() { 148 return this.zDateFormat; 149 } 150 151 /** 152 * Generates an item label for a particular item within a series. 153 * 154 * @param dataset the dataset (<code>null</code> not permitted). 155 * @param series the series index (zero-based). 156 * @param item the item index (zero-based). 157 * 158 * @return The item label (possibly <code>null</code>). 159 */ 160 public String generateLabel(XYDataset dataset, int series, int item) { 161 return generateLabelString(dataset, series, item); 162 } 163 164 /** 165 * Generates a label string for an item in the dataset. 166 * 167 * @param dataset the dataset (<code>null</code> not permitted). 168 * @param series the series (zero-based index). 169 * @param item the item (zero-based index). 170 * 171 * @return The label (possibly <code>null</code>). 172 */ 173 public String generateLabelString(XYDataset dataset, int series, int item) { 174 String result = null; 175 Object[] items = null; 176 if (dataset instanceof XYZDataset) { 177 items = createItemArray((XYZDataset) dataset, series, item); 178 } 179 else { 180 items = createItemArray(dataset, series, item); 181 } 182 result = MessageFormat.format(getFormatString(), items); 183 return result; 184 } 185 186 /** 187 * Creates the array of items that can be passed to the 188 * {@link MessageFormat} class for creating labels. 189 * 190 * @param dataset the dataset (<code>null</code> not permitted). 191 * @param series the series (zero-based index). 192 * @param item the item (zero-based index). 193 * 194 * @return The items (never <code>null</code>). 195 */ 196 protected Object[] createItemArray(XYZDataset dataset, 197 int series, int item) { 198 199 Object[] result = new Object[4]; 200 result[0] = dataset.getSeriesKey(series).toString(); 201 202 Number x = dataset.getX(series, item); 203 DateFormat xf = getXDateFormat(); 204 if (xf != null) { 205 result[1] = xf.format(x); 206 } 207 else { 208 result[1] = getXFormat().format(x); 209 } 210 211 Number y = dataset.getY(series, item); 212 DateFormat yf = getYDateFormat(); 213 if (yf != null) { 214 result[2] = yf.format(y); 215 } 216 else { 217 result[2] = getYFormat().format(y); 218 } 219 220 Number z = dataset.getZ(series, item); 221 if (this.zDateFormat != null) { 222 result[3] = this.zDateFormat.format(z); 223 } 224 else { 225 result[3] = this.zFormat.format(z); 226 } 227 228 return result; 229 230 } 231 232 /** 233 * Tests this object for equality with an arbitrary object. 234 * 235 * @param obj the other object (<code>null</code> permitted). 236 * 237 * @return A boolean. 238 */ 239 public boolean equals(Object obj) { 240 if (obj == this) { 241 return true; 242 } 243 if (!(obj instanceof BubbleXYItemLabelGenerator)) { 244 return false; 245 } 246 if (!super.equals(obj)) { 247 return false; 248 } 249 BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj; 250 if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) { 251 return false; 252 } 253 if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) { 254 return false; 255 } 256 return true; 257 } 258 259}