1 44 45 package org.jfree.chart.labels; 46 47 import java.io.Serializable ; 48 import java.text.DateFormat ; 49 import java.text.MessageFormat ; 50 import java.text.NumberFormat ; 51 52 import org.jfree.chart.renderer.xy.XYBubbleRenderer; 53 import org.jfree.data.xy.XYDataset; 54 import org.jfree.data.xy.XYZDataset; 55 import org.jfree.util.ObjectUtilities; 56 57 63 public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator 64 implements XYItemLabelGenerator, Serializable { 65 66 static final long serialVersionUID = -8458568928021240922L; 67 68 69 public static final String DEFAULT_FORMAT_STRING = "{3}"; 70 71 75 private NumberFormat zFormat; 76 77 81 private DateFormat zDateFormat; 82 83 87 public BubbleXYItemLabelGenerator() { 88 this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(), 89 NumberFormat.getNumberInstance(), 90 NumberFormat.getNumberInstance()); 91 } 92 93 105 public BubbleXYItemLabelGenerator(String formatString, 106 NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) { 107 super(formatString, xFormat, yFormat); 108 if (zFormat == null) { 109 throw new IllegalArgumentException ("Null 'zFormat' argument."); 110 } 111 this.zFormat = zFormat; 112 } 113 114 126 public BubbleXYItemLabelGenerator(String formatString, 127 DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) { 128 super(formatString, xFormat, yFormat); 129 if (zFormat == null) { 130 throw new IllegalArgumentException ("Null 'zFormat' argument."); 131 } 132 this.zDateFormat = zFormat; 133 } 134 135 140 public NumberFormat getZFormat() { 141 return this.zFormat; 142 } 143 144 149 public DateFormat getZDateFormat() { 150 return this.zDateFormat; 151 } 152 153 162 public String generateLabel(XYDataset dataset, int series, int item) { 163 return generateLabelString(dataset, series, item); 164 } 165 166 175 public String generateLabelString(XYDataset dataset, int series, int item) { 176 String result = null; 177 Object [] items = null; 178 if (dataset instanceof XYZDataset) { 179 items = createItemArray((XYZDataset) dataset, series, item); 180 } 181 else { 182 items = createItemArray(dataset, series, item); 183 } 184 result = MessageFormat.format(getFormatString(), items); 185 return result; 186 } 187 188 198 protected Object [] createItemArray(XYZDataset dataset, 199 int series, int item) { 200 201 Object [] result = new Object [4]; 202 result[0] = dataset.getSeriesKey(series).toString(); 203 204 Number x = dataset.getX(series, item); 205 DateFormat xf = getXDateFormat(); 206 if (xf != null) { 207 result[1] = xf.format(x); 208 } 209 else { 210 result[1] = getXFormat().format(x); 211 } 212 213 Number y = dataset.getY(series, item); 214 DateFormat yf = getYDateFormat(); 215 if (yf != null) { 216 result[2] = yf.format(y); 217 } 218 else { 219 result[2] = getYFormat().format(y); 220 } 221 222 Number z = dataset.getZ(series, item); 223 if (this.zDateFormat != null) { 224 result[3] = this.zDateFormat.format(z); 225 } 226 else { 227 result[3] = this.zFormat.format(z); 228 } 229 230 return result; 231 232 } 233 234 241 public boolean equals(Object obj) { 242 if (obj == this) { 243 return true; 244 } 245 if (!(obj instanceof BubbleXYItemLabelGenerator)) { 246 return false; 247 } 248 if (!super.equals(obj)) { 249 return false; 250 } 251 BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj; 252 if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) { 253 return false; 254 } 255 if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) { 256 return false; 257 } 258 return true; 259 } 260 261 } 262 | Popular Tags |