001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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-2020, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.text.DateFormat; 041import java.text.MessageFormat; 042import java.text.NumberFormat; 043 044import org.jfree.chart.HashUtils; 045import org.jfree.chart.renderer.xy.XYBubbleRenderer; 046import org.jfree.chart.util.ObjectUtils; 047import org.jfree.chart.util.Args; 048import org.jfree.chart.util.PublicCloneable; 049import org.jfree.data.xy.XYDataset; 050import org.jfree.data.xy.XYZDataset; 051 052/** 053 * An item label generator defined for use with the {@link XYBubbleRenderer} 054 * class, or any other class that uses an {@link XYZDataset}. 055 * 056 * @since 1.0.1 057 */ 058public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator 059 implements XYItemLabelGenerator, PublicCloneable, Serializable { 060 061 /** For serialization. */ 062 static final long serialVersionUID = -8458568928021240922L; 063 064 /** The default item label format. */ 065 public static final String DEFAULT_FORMAT_STRING = "{3}"; 066 067 /** 068 * A number formatter for the z value - if this is {@code null}, then 069 * zDateFormat must be non-null. 070 */ 071 private NumberFormat zFormat; 072 073 /** 074 * A date formatter for the z-value - if this is null, then zFormat must be 075 * non-null. 076 */ 077 private DateFormat zDateFormat; 078 079 /** 080 * Creates a new tool tip generator using default number formatters for the 081 * x, y and z-values. 082 */ 083 public BubbleXYItemLabelGenerator() { 084 this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(), 085 NumberFormat.getNumberInstance(), 086 NumberFormat.getNumberInstance()); 087 } 088 089 /** 090 * Constructs a new tool tip generator using the specified number 091 * formatters. 092 * 093 * @param formatString the format string. 094 * @param xFormat the format object for the x values ({@code null} 095 * not permitted). 096 * @param yFormat the format object for the y values ({@code null} 097 * not permitted). 098 * @param zFormat the format object for the z values ({@code null} 099 * not permitted). 100 */ 101 public BubbleXYItemLabelGenerator(String formatString, 102 NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) { 103 super(formatString, xFormat, yFormat); 104 Args.nullNotPermitted(zFormat, "zFormat"); 105 this.zFormat = zFormat; 106 } 107 108 /** 109 * Constructs a new item label generator using the specified date 110 * formatters. 111 * 112 * @param formatString the format string. 113 * @param xFormat the format object for the x values ({@code null} 114 * not permitted). 115 * @param yFormat the format object for the y values ({@code null} 116 * not permitted). 117 * @param zFormat the format object for the z values ({@code null} 118 * not permitted). 119 */ 120 public BubbleXYItemLabelGenerator(String formatString, 121 DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) { 122 super(formatString, xFormat, yFormat); 123 Args.nullNotPermitted(zFormat, "zFormat"); 124 this.zDateFormat = zFormat; 125 } 126 127 /** 128 * Returns the number formatter for the z-values. 129 * 130 * @return The number formatter (possibly {@code null}). 131 */ 132 public NumberFormat getZFormat() { 133 return this.zFormat; 134 } 135 136 /** 137 * Returns the date formatter for the z-values. 138 * 139 * @return The date formatter (possibly {@code null}). 140 */ 141 public DateFormat getZDateFormat() { 142 return this.zDateFormat; 143 } 144 145 /** 146 * Generates an item label for a particular item within a series. 147 * 148 * @param dataset the dataset ({@code null} not permitted). 149 * @param series the series index (zero-based). 150 * @param item the item index (zero-based). 151 * 152 * @return The item label (possibly {@code null}). 153 */ 154 @Override 155 public String generateLabel(XYDataset dataset, int series, int item) { 156 return generateLabelString(dataset, series, item); 157 } 158 159 /** 160 * Generates a label string for an item in the dataset. 161 * 162 * @param dataset the dataset ({@code null} not permitted). 163 * @param series the series (zero-based index). 164 * @param item the item (zero-based index). 165 * 166 * @return The label (possibly {@code null}). 167 */ 168 @Override 169 public String generateLabelString(XYDataset dataset, int series, int item) { 170 String result; 171 Object[] items; 172 if (dataset instanceof XYZDataset) { 173 items = createItemArray((XYZDataset) dataset, series, item); 174 } 175 else { 176 items = createItemArray(dataset, series, item); 177 } 178 result = MessageFormat.format(getFormatString(), items); 179 return result; 180 } 181 182 /** 183 * Creates the array of items that can be passed to the 184 * {@link MessageFormat} class for creating labels. 185 * 186 * @param dataset the dataset ({@code null} not permitted). 187 * @param series the series (zero-based index). 188 * @param item the item (zero-based index). 189 * 190 * @return The items (never {@code null}). 191 */ 192 protected Object[] createItemArray(XYZDataset dataset, 193 int series, int item) { 194 195 Object[] result = new Object[4]; 196 result[0] = dataset.getSeriesKey(series).toString(); 197 198 Number x = dataset.getX(series, item); 199 DateFormat xf = getXDateFormat(); 200 if (xf != null) { 201 result[1] = xf.format(x); 202 } 203 else { 204 result[1] = getXFormat().format(x); 205 } 206 207 Number y = dataset.getY(series, item); 208 DateFormat yf = getYDateFormat(); 209 if (yf != null) { 210 result[2] = yf.format(y); 211 } 212 else { 213 result[2] = getYFormat().format(y); 214 } 215 216 Number z = dataset.getZ(series, item); 217 if (this.zDateFormat != null) { 218 result[3] = this.zDateFormat.format(z); 219 } 220 else { 221 result[3] = this.zFormat.format(z); 222 } 223 224 return result; 225 226 } 227 228 /** 229 * Tests this object for equality with an arbitrary object. 230 * 231 * @param obj the other object ({@code null} permitted). 232 * 233 * @return A boolean. 234 */ 235 @Override 236 public boolean equals(Object obj) { 237 if (obj == this) { 238 return true; 239 } 240 if (!(obj instanceof BubbleXYItemLabelGenerator)) { 241 return false; 242 } 243 if (!super.equals(obj)) { 244 return false; 245 } 246 BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj; 247 if (!ObjectUtils.equal(this.zFormat, that.zFormat)) { 248 return false; 249 } 250 if (!ObjectUtils.equal(this.zDateFormat, that.zDateFormat)) { 251 return false; 252 } 253 return true; 254 } 255 256 /** 257 * Returns a hash code for this instance. 258 * 259 * @return A hash code. 260 */ 261 @Override 262 public int hashCode() { 263 int h = super.hashCode(); 264 h = HashUtils.hashCode(h, this.zFormat); 265 h = HashUtils.hashCode(h, this.zDateFormat); 266 return h; 267 } 268 269}