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 * AbstractCategoryItemLabelGenerator.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.util.ObjectUtils; 046import org.jfree.chart.util.Args; 047import org.jfree.chart.util.PublicCloneable; 048import org.jfree.data.DataUtils; 049import org.jfree.data.category.CategoryDataset; 050 051/** 052 * A base class that can be used to create a label or tooltip generator that 053 * can be assigned to a 054 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}. 055 */ 056public abstract class AbstractCategoryItemLabelGenerator 057 implements PublicCloneable, Cloneable, Serializable { 058 059 /** For serialization. */ 060 private static final long serialVersionUID = -7108591260223293197L; 061 062 /** 063 * The label format string used by a {@code MessageFormat} object to 064 * combine the standard items: {0} = series name, {1} = category, 065 * {2} = value, {3} = value as a percentage of the column total. 066 */ 067 private String labelFormat; 068 069 /** The string used to represent a null value. */ 070 private String nullValueString; 071 072 /** 073 * A number formatter used to preformat the value before it is passed to 074 * the MessageFormat object. 075 */ 076 private NumberFormat numberFormat; 077 078 /** 079 * A date formatter used to preformat the value before it is passed to the 080 * MessageFormat object. 081 */ 082 private DateFormat dateFormat; 083 084 /** 085 * A number formatter used to preformat the percentage value before it is 086 * passed to the MessageFormat object. 087 */ 088 private NumberFormat percentFormat; 089 090 /** 091 * Creates a label generator with the specified number formatter. 092 * 093 * @param labelFormat the label format string ({@code null} not 094 * permitted). 095 * @param formatter the number formatter ({@code null} not permitted). 096 */ 097 protected AbstractCategoryItemLabelGenerator(String labelFormat, 098 NumberFormat formatter) { 099 this(labelFormat, formatter, NumberFormat.getPercentInstance()); 100 } 101 102 /** 103 * Creates a label generator with the specified number formatter. 104 * 105 * @param labelFormat the label format string ({@code null} not 106 * permitted). 107 * @param formatter the number formatter ({@code null} not permitted). 108 * @param percentFormatter the percent formatter ({@code null} not 109 * permitted). 110 * 111 * @since 1.0.2 112 */ 113 protected AbstractCategoryItemLabelGenerator(String labelFormat, 114 NumberFormat formatter, NumberFormat percentFormatter) { 115 Args.nullNotPermitted(labelFormat, "labelFormat"); 116 Args.nullNotPermitted(formatter, "formatter"); 117 Args.nullNotPermitted(percentFormatter, "percentFormatter"); 118 this.labelFormat = labelFormat; 119 this.numberFormat = formatter; 120 this.percentFormat = percentFormatter; 121 this.dateFormat = null; 122 this.nullValueString = "-"; 123 } 124 125 /** 126 * Creates a label generator with the specified date formatter. 127 * 128 * @param labelFormat the label format string ({@code null} not 129 * permitted). 130 * @param formatter the date formatter ({@code null} not permitted). 131 */ 132 protected AbstractCategoryItemLabelGenerator(String labelFormat, 133 DateFormat formatter) { 134 Args.nullNotPermitted(labelFormat, "labelFormat"); 135 Args.nullNotPermitted(formatter, "formatter"); 136 this.labelFormat = labelFormat; 137 this.numberFormat = null; 138 this.percentFormat = NumberFormat.getPercentInstance(); 139 this.dateFormat = formatter; 140 this.nullValueString = "-"; 141 } 142 143 /** 144 * Generates a label for the specified row. 145 * 146 * @param dataset the dataset ({@code null} not permitted). 147 * @param row the row index (zero-based). 148 * 149 * @return The label. 150 */ 151 public String generateRowLabel(CategoryDataset dataset, int row) { 152 return dataset.getRowKey(row).toString(); 153 } 154 155 /** 156 * Generates a label for the specified row. 157 * 158 * @param dataset the dataset ({@code null} not permitted). 159 * @param column the column index (zero-based). 160 * 161 * @return The label. 162 */ 163 public String generateColumnLabel(CategoryDataset dataset, int column) { 164 return dataset.getColumnKey(column).toString(); 165 } 166 167 /** 168 * Returns the label format string. 169 * 170 * @return The label format string (never {@code null}). 171 */ 172 public String getLabelFormat() { 173 return this.labelFormat; 174 } 175 176 /** 177 * Returns the number formatter. 178 * 179 * @return The number formatter (possibly {@code null}). 180 */ 181 public NumberFormat getNumberFormat() { 182 return this.numberFormat; 183 } 184 185 /** 186 * Returns the date formatter. 187 * 188 * @return The date formatter (possibly {@code null}). 189 */ 190 public DateFormat getDateFormat() { 191 return this.dateFormat; 192 } 193 194 /** 195 * Generates a for the specified item. 196 * 197 * @param dataset the dataset ({@code null} not permitted). 198 * @param row the row index (zero-based). 199 * @param column the column index (zero-based). 200 * 201 * @return The label (possibly {@code null}). 202 */ 203 protected String generateLabelString(CategoryDataset dataset, 204 int row, int column) { 205 Args.nullNotPermitted(dataset, "dataset"); 206 String result; 207 Object[] items = createItemArray(dataset, row, column); 208 result = MessageFormat.format(this.labelFormat, items); 209 return result; 210 211 } 212 213 /** 214 * Creates the array of items that can be passed to the 215 * {@link MessageFormat} class for creating labels. 216 * 217 * @param dataset the dataset ({@code null} not permitted). 218 * @param row the row index (zero-based). 219 * @param column the column index (zero-based). 220 * 221 * @return The items (never {@code null}). 222 */ 223 protected Object[] createItemArray(CategoryDataset dataset, 224 int row, int column) { 225 Object[] result = new Object[4]; 226 result[0] = dataset.getRowKey(row).toString(); 227 result[1] = dataset.getColumnKey(column).toString(); 228 Number value = dataset.getValue(row, column); 229 if (value != null) { 230 if (this.numberFormat != null) { 231 result[2] = this.numberFormat.format(value); 232 } 233 else if (this.dateFormat != null) { 234 result[2] = this.dateFormat.format(value); 235 } 236 } 237 else { 238 result[2] = this.nullValueString; 239 } 240 if (value != null) { 241 double total = DataUtils.calculateColumnTotal(dataset, column); 242 double percent = value.doubleValue() / total; 243 result[3] = this.percentFormat.format(percent); 244 } 245 246 return result; 247 } 248 249 /** 250 * Tests this object for equality with an arbitrary object. 251 * 252 * @param obj the other object ({@code null} permitted). 253 * 254 * @return A boolean. 255 */ 256 @Override 257 public boolean equals(Object obj) { 258 if (obj == this) { 259 return true; 260 } 261 if (!(obj instanceof AbstractCategoryItemLabelGenerator)) { 262 return false; 263 } 264 265 AbstractCategoryItemLabelGenerator that 266 = (AbstractCategoryItemLabelGenerator) obj; 267 if (!this.labelFormat.equals(that.labelFormat)) { 268 return false; 269 } 270 if (!ObjectUtils.equal(this.dateFormat, that.dateFormat)) { 271 return false; 272 } 273 if (!ObjectUtils.equal(this.numberFormat, that.numberFormat)) { 274 return false; 275 } 276 return true; 277 } 278 279 /** 280 * Returns a hash code for this instance. 281 * 282 * @return A hash code. 283 */ 284 @Override 285 public int hashCode() { 286 int result = 127; 287 result = HashUtils.hashCode(result, this.labelFormat); 288 result = HashUtils.hashCode(result, this.nullValueString); 289 result = HashUtils.hashCode(result, this.dateFormat); 290 result = HashUtils.hashCode(result, this.numberFormat); 291 result = HashUtils.hashCode(result, this.percentFormat); 292 return result; 293 } 294 295 /** 296 * Returns an independent copy of the generator. 297 * 298 * @return A clone. 299 * 300 * @throws CloneNotSupportedException should not happen. 301 */ 302 @Override 303 public Object clone() throws CloneNotSupportedException { 304 AbstractCategoryItemLabelGenerator clone 305 = (AbstractCategoryItemLabelGenerator) super.clone(); 306 if (this.numberFormat != null) { 307 clone.numberFormat = (NumberFormat) this.numberFormat.clone(); 308 } 309 if (this.dateFormat != null) { 310 clone.dateFormat = (DateFormat) this.dateFormat.clone(); 311 } 312 return clone; 313 } 314 315}