1 45 46 package org.jfree.chart.labels; 47 48 import java.io.Serializable ; 49 import java.text.DateFormat ; 50 import java.text.MessageFormat ; 51 import java.text.NumberFormat ; 52 53 import org.jfree.data.DataUtilities; 54 import org.jfree.data.category.CategoryDataset; 55 import org.jfree.util.ObjectUtilities; 56 import org.jfree.util.PublicCloneable; 57 58 63 public abstract class AbstractCategoryItemLabelGenerator 64 implements PublicCloneable, Cloneable , Serializable { 65 66 67 private static final long serialVersionUID = -7108591260223293197L; 68 69 74 private String labelFormat; 75 76 77 private String nullValueString; 78 79 83 private NumberFormat numberFormat; 84 85 89 private DateFormat dateFormat; 90 91 95 private NumberFormat percentFormat; 96 97 104 protected AbstractCategoryItemLabelGenerator(String labelFormat, 105 NumberFormat formatter) { 106 this(labelFormat, formatter, NumberFormat.getPercentInstance()); 107 } 108 109 120 protected AbstractCategoryItemLabelGenerator(String labelFormat, 121 NumberFormat formatter, NumberFormat percentFormatter) { 122 if (labelFormat == null) { 123 throw new IllegalArgumentException ("Null 'labelFormat' argument."); 124 } 125 if (formatter == null) { 126 throw new IllegalArgumentException ("Null 'formatter' argument."); 127 } 128 if (percentFormatter == null) { 129 throw new IllegalArgumentException ( 130 "Null 'percentFormatter' argument."); 131 } 132 this.labelFormat = labelFormat; 133 this.numberFormat = formatter; 134 this.percentFormat = percentFormatter; 135 this.dateFormat = null; 136 this.nullValueString = "-"; 137 } 138 139 146 protected AbstractCategoryItemLabelGenerator(String labelFormat, 147 DateFormat formatter) { 148 if (labelFormat == null) { 149 throw new IllegalArgumentException ("Null 'labelFormat' argument."); 150 } 151 if (formatter == null) { 152 throw new IllegalArgumentException ("Null 'formatter' argument."); 153 } 154 this.labelFormat = labelFormat; 155 this.numberFormat = null; 156 this.percentFormat = NumberFormat.getPercentInstance(); 157 this.dateFormat = formatter; 158 this.nullValueString = "-"; 159 } 160 161 169 public String generateRowLabel(CategoryDataset dataset, int row) { 170 return dataset.getRowKey(row).toString(); 171 } 172 173 181 public String generateColumnLabel(CategoryDataset dataset, int column) { 182 return dataset.getColumnKey(column).toString(); 183 } 184 185 190 public String getLabelFormat() { 191 return this.labelFormat; 192 } 193 194 199 public NumberFormat getNumberFormat() { 200 return this.numberFormat; 201 } 202 203 208 public DateFormat getDateFormat() { 209 return this.dateFormat; 210 } 211 212 221 protected String generateLabelString(CategoryDataset dataset, 222 int row, int column) { 223 if (dataset == null) { 224 throw new IllegalArgumentException ("Null 'dataset' argument."); 225 } 226 String result = null; 227 Object [] items = createItemArray(dataset, row, column); 228 result = MessageFormat.format(this.labelFormat, items); 229 return result; 230 231 } 232 233 243 protected Object [] createItemArray(CategoryDataset dataset, 244 int row, int column) { 245 Object [] result = new Object [4]; 246 result[0] = dataset.getRowKey(row).toString(); 247 result[1] = dataset.getColumnKey(column).toString(); 248 Number value = dataset.getValue(row, column); 249 if (value != null) { 250 if (this.numberFormat != null) { 251 result[2] = this.numberFormat.format(value); 252 } 253 else if (this.dateFormat != null) { 254 result[2] = this.dateFormat.format(value); 255 } 256 } 257 else { 258 result[2] = this.nullValueString; 259 } 260 if (value != null) { 261 double total = DataUtilities.calculateColumnTotal(dataset, column); 262 double percent = value.doubleValue() / total; 263 result[3] = this.percentFormat.format(percent); 264 } 265 266 return result; 267 } 268 269 276 public boolean equals(Object obj) { 277 if (obj == this) { 278 return true; 279 } 280 if (!(obj instanceof AbstractCategoryItemLabelGenerator)) { 281 return false; 282 } 283 284 AbstractCategoryItemLabelGenerator that 285 = (AbstractCategoryItemLabelGenerator) obj; 286 if (!this.labelFormat.equals(that.labelFormat)) { 287 return false; 288 } 289 if (!ObjectUtilities.equal(this.dateFormat, that.dateFormat)) { 290 return false; 291 } 292 if (!ObjectUtilities.equal(this.numberFormat, that.numberFormat)) { 293 return false; 294 } 295 return true; 296 } 297 298 305 public Object clone() throws CloneNotSupportedException { 306 AbstractCategoryItemLabelGenerator clone 307 = (AbstractCategoryItemLabelGenerator) super.clone(); 308 if (this.numberFormat != null) { 309 clone.numberFormat = (NumberFormat ) this.numberFormat.clone(); 310 } 311 if (this.dateFormat != null) { 312 clone.dateFormat = (DateFormat ) this.dateFormat.clone(); 313 } 314 return clone; 315 } 316 317 } 318 | Popular Tags |