1 42 43 package org.jfree.data.statistics; 44 45 import java.io.Serializable ; 46 import java.util.ArrayList ; 47 import java.util.Collections ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 import org.jfree.data.DomainOrder; 52 import org.jfree.data.general.DatasetChangeEvent; 53 import org.jfree.data.xy.AbstractIntervalXYDataset; 54 import org.jfree.data.xy.IntervalXYDataset; 55 import org.jfree.util.ObjectUtilities; 56 import org.jfree.util.PublicCloneable; 57 58 63 public class SimpleHistogramDataset extends AbstractIntervalXYDataset 64 implements IntervalXYDataset, 65 Cloneable , PublicCloneable, 66 Serializable { 67 68 69 private static final long serialVersionUID = 7997996479768018443L; 70 71 72 private Comparable key; 73 74 75 private List bins; 76 77 81 private boolean adjustForBinSize; 82 83 88 public SimpleHistogramDataset(Comparable key) { 89 this.key = key; 90 this.bins = new ArrayList (); 91 this.adjustForBinSize = true; 92 } 93 94 100 public boolean getAdjustForBinSize() { 101 return this.adjustForBinSize; 102 } 103 104 110 public void setAdjustForBinSize(boolean adjust) { 111 this.adjustForBinSize = adjust; 112 notifyListeners(new DatasetChangeEvent(this, this)); 113 } 114 115 120 public int getSeriesCount() { 121 return 1; 122 } 123 124 131 public Comparable getSeriesKey(int series) { 132 return this.key; 133 } 134 135 140 public DomainOrder getDomainOrder() { 141 return DomainOrder.ASCENDING; 142 } 143 144 151 public int getItemCount(int series) { 152 return this.bins.size(); 153 } 154 155 161 public void addBin(SimpleHistogramBin bin) { 162 Iterator iterator = this.bins.iterator(); 164 while (iterator.hasNext()) { 165 SimpleHistogramBin existingBin 166 = (SimpleHistogramBin) iterator.next(); 167 if (bin.overlapsWith(existingBin)) { 168 throw new RuntimeException ("Overlapping bin"); 169 } 170 } 171 this.bins.add(bin); 172 Collections.sort(this.bins); 173 } 174 175 182 public void addObservation(double value) { 183 addObservation(value, true); 184 } 185 186 194 public void addObservation(double value, boolean notify) { 195 boolean placed = false; 196 Iterator iterator = this.bins.iterator(); 197 while (iterator.hasNext() && !placed) { 198 SimpleHistogramBin bin = (SimpleHistogramBin) iterator.next(); 199 if (bin.accepts(value)) { 200 bin.setItemCount(bin.getItemCount() + 1); 201 placed = true; 202 } 203 } 204 if (!placed) { 205 throw new RuntimeException ("No bin."); 206 } 207 if (notify) { 208 notifyListeners(new DatasetChangeEvent(this, this)); 209 } 210 } 211 212 217 public void addObservations(double[] values) { 218 for (int i = 0; i < values.length; i++) { 219 addObservation(values[i], false); 220 } 221 notifyListeners(new DatasetChangeEvent(this, this)); 222 } 223 224 234 public Number getX(int series, int item) { 235 return new Double (getXValue(series, item)); 236 } 237 238 246 public double getXValue(int series, int item) { 247 SimpleHistogramBin bin = (SimpleHistogramBin) this.bins.get(item); 248 return (bin.getLowerBound() + bin.getUpperBound()) / 2.0; 249 } 250 251 259 public Number getY(int series, int item) { 260 return new Double (getYValue(series, item)); 261 } 262 263 271 public double getYValue(int series, int item) { 272 SimpleHistogramBin bin = (SimpleHistogramBin) this.bins.get(item); 273 if (this.adjustForBinSize) { 274 return bin.getItemCount() 275 / (bin.getUpperBound() - bin.getLowerBound()); 276 } 277 else { 278 return bin.getItemCount(); 279 } 280 } 281 282 290 public Number getStartX(int series, int item) { 291 return new Double (getStartXValue(series, item)); 292 } 293 294 303 public double getStartXValue(int series, int item) { 304 SimpleHistogramBin bin = (SimpleHistogramBin) this.bins.get(item); 305 return bin.getLowerBound(); 306 } 307 308 316 public Number getEndX(int series, int item) { 317 return new Double (getEndXValue(series, item)); 318 } 319 320 329 public double getEndXValue(int series, int item) { 330 SimpleHistogramBin bin = (SimpleHistogramBin) this.bins.get(item); 331 return bin.getUpperBound(); 332 } 333 334 342 public Number getStartY(int series, int item) { 343 return getY(series, item); 344 } 345 346 355 public double getStartYValue(int series, int item) { 356 return getYValue(series, item); 357 } 358 359 367 public Number getEndY(int series, int item) { 368 return getY(series, item); 369 } 370 371 380 public double getEndYValue(int series, int item) { 381 return getYValue(series, item); 382 } 383 384 391 public boolean equals(Object obj) { 392 if (obj == this) { 393 return true; 394 } 395 if (!(obj instanceof SimpleHistogramDataset)) { 396 return false; 397 } 398 SimpleHistogramDataset that = (SimpleHistogramDataset) obj; 399 if (!this.key.equals(that.key)) { 400 return false; 401 } 402 if (this.adjustForBinSize != that.adjustForBinSize) { 403 return false; 404 } 405 if (!this.bins.equals(that.bins)) { 406 return false; 407 } 408 return true; 409 } 410 411 419 public Object clone() throws CloneNotSupportedException { 420 SimpleHistogramDataset clone = (SimpleHistogramDataset) super.clone(); 421 clone.bins = (List ) ObjectUtilities.deepClone(this.bins); 422 return clone; 423 } 424 425 } 426 | Popular Tags |