1 50 51 package org.jfree.chart.labels; 52 53 import java.io.Serializable ; 54 import java.text.DateFormat ; 55 import java.text.MessageFormat ; 56 import java.text.NumberFormat ; 57 import java.util.Date ; 58 59 import org.jfree.data.xy.XYDataset; 60 import org.jfree.util.ObjectUtilities; 61 62 65 public class AbstractXYItemLabelGenerator implements Cloneable , Serializable { 66 67 68 private static final long serialVersionUID = 5869744396278660636L; 69 70 71 private String formatString; 72 73 74 private NumberFormat xFormat; 75 76 77 private DateFormat xDateFormat; 78 79 80 private NumberFormat yFormat; 81 82 83 private DateFormat yDateFormat; 84 85 86 private String nullXString = "null"; 87 88 89 private String nullYString = "null"; 90 91 94 protected AbstractXYItemLabelGenerator() { 95 this( 96 "{2}", NumberFormat.getNumberInstance(), 97 NumberFormat.getNumberInstance() 98 ); 99 } 100 101 111 protected AbstractXYItemLabelGenerator(String formatString, 112 NumberFormat xFormat, 113 NumberFormat yFormat) { 114 115 if (formatString == null) { 116 throw new IllegalArgumentException ("Null 'formatString' argument."); 117 } 118 if (xFormat == null) { 119 throw new IllegalArgumentException ("Null 'xFormat' argument."); 120 } 121 if (yFormat == null) { 122 throw new IllegalArgumentException ("Null 'yFormat' argument."); 123 } 124 this.formatString = formatString; 125 this.xFormat = xFormat; 126 this.yFormat = yFormat; 127 128 } 129 130 140 protected AbstractXYItemLabelGenerator(String formatString, 141 DateFormat xFormat, 142 NumberFormat yFormat) { 143 144 this(formatString, NumberFormat.getInstance(), yFormat); 145 this.xDateFormat = xFormat; 146 147 } 148 149 159 protected AbstractXYItemLabelGenerator(String formatString, 160 DateFormat xFormat, 161 DateFormat yFormat) { 162 163 this( 164 formatString, NumberFormat.getInstance(), 165 NumberFormat.getInstance() 166 ); 167 this.xDateFormat = xFormat; 168 this.yDateFormat = yFormat; 169 170 } 171 172 178 public String getFormatString() { 179 return this.formatString; 180 } 181 182 187 public NumberFormat getXFormat() { 188 return this.xFormat; 189 } 190 191 196 public DateFormat getXDateFormat() { 197 return this.xDateFormat; 198 } 199 200 205 public NumberFormat getYFormat() { 206 return this.yFormat; 207 } 208 209 214 public DateFormat getYDateFormat() { 215 return this.yDateFormat; 216 } 217 218 227 public String generateLabelString(XYDataset dataset, int series, int item) { 228 String result = null; 229 Object [] items = createItemArray(dataset, series, item); 230 result = MessageFormat.format(this.formatString, items); 231 return result; 232 } 233 234 245 protected Object [] createItemArray(XYDataset dataset, int series, 246 int item) { 247 Object [] result = new Object [3]; 248 result[0] = dataset.getSeriesKey(series).toString(); 249 250 double x = dataset.getXValue(series, item); 251 if (Double.isNaN(x) && dataset.getX(series, item) == null) { 252 result[1] = this.nullXString; 253 } 254 else { 255 if (this.xDateFormat != null) { 256 result[1] = this.xDateFormat.format(new Date ((long) x)); 257 } 258 else { 259 result[1] = this.xFormat.format(x); 260 } 261 } 262 263 double y = dataset.getYValue(series, item); 264 if (Double.isNaN(y) && dataset.getY(series, item) == null) { 265 result[2] = this.nullYString; 266 } 267 else { 268 if (this.yDateFormat != null) { 269 result[2] = this.yDateFormat.format(new Date ((long) y)); 270 } 271 else { 272 result[2] = this.yFormat.format(y); 273 } 274 } 275 return result; 276 } 277 278 285 public boolean equals(Object obj) { 286 if (obj == this) { 287 return true; 288 } 289 if (!(obj instanceof AbstractXYItemLabelGenerator)) { 290 return false; 291 } 292 AbstractXYItemLabelGenerator that = (AbstractXYItemLabelGenerator) obj; 293 if (!this.formatString.equals(that.formatString)) { 294 return false; 295 } 296 if (!ObjectUtilities.equal(this.xFormat, that.xFormat)) { 297 return false; 298 } 299 if (!ObjectUtilities.equal(this.xDateFormat, that.xDateFormat)) { 300 return false; 301 } 302 if (!ObjectUtilities.equal(this.yFormat, that.yFormat)) { 303 return false; 304 } 305 if (!ObjectUtilities.equal(this.yDateFormat, that.yDateFormat)) { 306 return false; 307 } 308 return true; 309 310 } 311 312 319 public Object clone() throws CloneNotSupportedException { 320 321 AbstractXYItemLabelGenerator clone 322 = (AbstractXYItemLabelGenerator) super.clone(); 323 324 if (this.xFormat != null) { 325 clone.xFormat = (NumberFormat ) this.xFormat.clone(); 326 } 327 328 if (this.yFormat != null) { 329 clone.yFormat = (NumberFormat ) this.yFormat.clone(); 330 } 331 332 return clone; 333 334 } 335 336 } 337 | Popular Tags |