001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------------------- 028 * BubbleXYItemLabelGenerator.java 029 * ------------------------------- 030 * (C) Copyright 2005-2013, 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 * 23-Nov-2007 : Implemented hashCode() (DG); 041 * 23-Apr-2008 : Implemented PublicCloneable (DG); 042 * 03-Jul-2013 : Use ParamChecks (DG); 043 * 044 */ 045 046package org.jfree.chart.labels; 047 048import java.io.Serializable; 049import java.text.DateFormat; 050import java.text.MessageFormat; 051import java.text.NumberFormat; 052 053import org.jfree.chart.HashUtilities; 054import org.jfree.chart.renderer.xy.XYBubbleRenderer; 055import org.jfree.chart.util.ParamChecks; 056import org.jfree.data.xy.XYDataset; 057import org.jfree.data.xy.XYZDataset; 058import org.jfree.util.ObjectUtilities; 059import org.jfree.util.PublicCloneable; 060 061/** 062 * An item label generator defined for use with the {@link XYBubbleRenderer} 063 * class, or any other class that uses an {@link XYZDataset}. 064 * 065 * @since 1.0.1 066 */ 067public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator 068 implements XYItemLabelGenerator, PublicCloneable, Serializable { 069 070 /** For serialization. */ 071 static final long serialVersionUID = -8458568928021240922L; 072 073 /** The default item label format. */ 074 public static final String DEFAULT_FORMAT_STRING = "{3}"; 075 076 /** 077 * A number formatter for the z value - if this is <code>null</code>, then 078 * zDateFormat must be non-null. 079 */ 080 private NumberFormat zFormat; 081 082 /** 083 * A date formatter for the z-value - if this is null, then zFormat must be 084 * non-null. 085 */ 086 private DateFormat zDateFormat; 087 088 /** 089 * Creates a new tool tip generator using default number formatters for the 090 * x, y and z-values. 091 */ 092 public BubbleXYItemLabelGenerator() { 093 this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(), 094 NumberFormat.getNumberInstance(), 095 NumberFormat.getNumberInstance()); 096 } 097 098 /** 099 * Constructs a new tool tip generator using the specified number 100 * formatters. 101 * 102 * @param formatString the format string. 103 * @param xFormat the format object for the x values (<code>null</code> 104 * not permitted). 105 * @param yFormat the format object for the y values (<code>null</code> 106 * not permitted). 107 * @param zFormat the format object for the z values (<code>null</code> 108 * not permitted). 109 */ 110 public BubbleXYItemLabelGenerator(String formatString, 111 NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) { 112 super(formatString, xFormat, yFormat); 113 ParamChecks.nullNotPermitted(zFormat, "zFormat"); 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 ParamChecks.nullNotPermitted(zFormat, "zFormat"); 133 this.zDateFormat = zFormat; 134 } 135 136 /** 137 * Returns the number formatter for the z-values. 138 * 139 * @return The number formatter (possibly <code>null</code>). 140 */ 141 public NumberFormat getZFormat() { 142 return this.zFormat; 143 } 144 145 /** 146 * Returns the date formatter for the z-values. 147 * 148 * @return The date formatter (possibly <code>null</code>). 149 */ 150 public DateFormat getZDateFormat() { 151 return this.zDateFormat; 152 } 153 154 /** 155 * Generates an item label for a particular item within a series. 156 * 157 * @param dataset the dataset (<code>null</code> not permitted). 158 * @param series the series index (zero-based). 159 * @param item the item index (zero-based). 160 * 161 * @return The item label (possibly <code>null</code>). 162 */ 163 @Override 164 public String generateLabel(XYDataset dataset, int series, int item) { 165 return generateLabelString(dataset, series, item); 166 } 167 168 /** 169 * Generates a label string for an item in the dataset. 170 * 171 * @param dataset the dataset (<code>null</code> not permitted). 172 * @param series the series (zero-based index). 173 * @param item the item (zero-based index). 174 * 175 * @return The label (possibly <code>null</code>). 176 */ 177 @Override 178 public String generateLabelString(XYDataset dataset, int series, int item) { 179 String result; 180 Object[] items; 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 @Override 245 public boolean equals(Object obj) { 246 if (obj == this) { 247 return true; 248 } 249 if (!(obj instanceof BubbleXYItemLabelGenerator)) { 250 return false; 251 } 252 if (!super.equals(obj)) { 253 return false; 254 } 255 BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj; 256 if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) { 257 return false; 258 } 259 if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) { 260 return false; 261 } 262 return true; 263 } 264 265 /** 266 * Returns a hash code for this instance. 267 * 268 * @return A hash code. 269 */ 270 @Override 271 public int hashCode() { 272 int h = super.hashCode(); 273 h = HashUtilities.hashCode(h, this.zFormat); 274 h = HashUtilities.hashCode(h, this.zDateFormat); 275 return h; 276 } 277 278}