1 42 43 package org.jfree.experimental.chart.plot.dial; 44 45 import java.awt.Graphics2D ; 46 import java.awt.Shape ; 47 import java.awt.geom.Point2D ; 48 import java.awt.geom.Rectangle2D ; 49 import java.io.IOException ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutputStream ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 55 import org.jfree.chart.JFreeChart; 56 import org.jfree.chart.event.PlotChangeEvent; 57 import org.jfree.chart.plot.Plot; 58 import org.jfree.chart.plot.PlotRenderingInfo; 59 import org.jfree.chart.plot.PlotState; 60 import org.jfree.data.general.DatasetChangeEvent; 61 import org.jfree.data.general.ValueDataset; 62 import org.jfree.util.ObjectList; 63 import org.jfree.util.ObjectUtilities; 64 65 68 public class DialPlot extends Plot implements DialLayerChangeListener { 69 70 73 private DialLayer background; 74 75 78 private DialLayer cap; 79 80 83 private DialFrame dialFrame; 84 85 88 private ObjectList datasets; 89 90 93 private ObjectList scales; 94 95 96 private ObjectList datasetToScaleMap; 97 98 101 private List layers; 102 103 106 private double viewX; 107 108 111 private double viewY; 112 113 116 private double viewW; 117 118 121 private double viewH; 122 123 126 public DialPlot() { 127 this.background = null; 128 this.cap = null; 129 this.dialFrame = new StandardDialFrame(); 130 this.datasets = new ObjectList(); 131 this.scales = new ObjectList(); 132 this.datasetToScaleMap = new ObjectList(); 133 this.layers = new java.util.ArrayList (); 134 this.viewX = 0.0; 135 this.viewY = 0.0; 136 this.viewW = 1.0; 137 this.viewH = 1.0; 138 } 139 140 147 public DialLayer getBackground() { 148 return this.background; 149 } 150 151 158 public void setBackground(DialLayer background) { 159 this.background = background; 160 notifyListeners(new PlotChangeEvent(this)); 161 } 162 163 170 public DialLayer getCap() { 171 return this.cap; 172 } 173 174 181 public void setCap(DialLayer cap) { 182 this.cap = cap; 183 notifyListeners(new PlotChangeEvent(this)); 184 } 185 186 193 public DialFrame getDialFrame() { 194 return this.dialFrame; 195 } 196 197 204 public void setDialFrame(DialFrame frame) { 205 if (frame == null) { 206 throw new IllegalArgumentException ("Null 'frame' argument."); 207 } 208 this.dialFrame = frame; 209 notifyListeners(new PlotChangeEvent(this)); 210 } 211 212 220 public double getViewX() { 221 return this.viewX; 222 } 223 224 232 public double getViewY() { 233 return this.viewY; 234 } 235 236 244 public double getViewWidth() { 245 return this.viewW; 246 } 247 248 256 public double getViewHeight() { 257 return this.viewH; 258 } 259 260 273 public void setView(double x, double y, double w, double h) { 274 this.viewX = x; 275 this.viewY = y; 276 this.viewW = w; 277 this.viewH = h; 278 notifyListeners(new PlotChangeEvent(this)); 279 } 280 281 286 public void addLayer(DialLayer layer) { 287 if (layer == null) { 288 throw new IllegalArgumentException ("Null 'layer' argument."); 289 } 290 this.layers.add(layer); 291 notifyListeners(new PlotChangeEvent(this)); 292 } 293 294 299 public ValueDataset getDataset() { 300 return getDataset(0); 301 } 302 303 310 public ValueDataset getDataset(int index) { 311 ValueDataset result = null; 312 if (this.datasets.size() > index) { 313 result = (ValueDataset) this.datasets.get(index); 314 } 315 return result; 316 } 317 318 325 public void setDataset(ValueDataset dataset) { 326 setDataset(0, dataset); 327 } 328 329 335 public void setDataset(int index, ValueDataset dataset) { 336 337 ValueDataset existing = (ValueDataset) this.datasets.get(index); 338 if (existing != null) { 339 existing.removeChangeListener(this); 340 } 341 this.datasets.set(index, dataset); 342 if (dataset != null) { 343 dataset.addChangeListener(this); 344 } 345 346 DatasetChangeEvent event = new DatasetChangeEvent(this, dataset); 348 datasetChanged(event); 349 350 } 351 352 357 public int getDatasetCount() { 358 return this.datasets.size(); 359 } 360 361 373 public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, 374 PlotState parentState, PlotRenderingInfo info) { 375 376 Rectangle2D frame = viewToFrame(area); 378 379 if (this.background != null && this.background.isVisible()) { 381 if (this.background.isClippedToWindow()) { 382 Shape savedClip = g2.getClip(); 383 g2.setClip(this.dialFrame.getWindow(frame)); 384 this.background.draw(g2, this, frame, area); 385 g2.setClip(savedClip); 386 } 387 else { 388 this.background.draw(g2, this, frame, area); 389 } 390 } 391 392 Iterator iterator = this.layers.iterator(); 393 while (iterator.hasNext()) { 394 DialLayer current = (DialLayer) iterator.next(); 395 if (current.isVisible()) { 396 if (current.isClippedToWindow()) { 397 Shape savedClip = g2.getClip(); 398 g2.setClip(this.dialFrame.getWindow(frame)); 399 current.draw(g2, this, frame, area); 400 g2.setClip(savedClip); 401 } 402 else { 403 current.draw(g2, this, frame, area); 404 } 405 } 406 } 407 408 if (this.cap != null && this.cap.isVisible()) { 410 if (this.cap.isClippedToWindow()) { 411 Shape savedClip = g2.getClip(); 412 g2.setClip(this.dialFrame.getWindow(frame)); 413 this.cap.draw(g2, this, frame, area); 414 g2.setClip(savedClip); 415 } 416 else { 417 this.cap.draw(g2, this, frame, area); 418 } 419 } 420 421 if (this.dialFrame.isVisible()) { 422 this.dialFrame.draw(g2, this, frame, area); 423 } 424 425 } 426 427 432 private Rectangle2D viewToFrame(Rectangle2D view) { 433 double width = view.getWidth() / this.viewW; 434 double height = view.getHeight() / this.viewH; 435 double x = view.getX() - (width * this.viewX); 436 double y = view.getY() - (height * this.viewY); 437 return new Rectangle2D.Double (x, y, width, height); 438 } 439 440 447 public double getValue(int datasetIndex) { 448 double result = Double.NaN; 449 ValueDataset dataset = getDataset(datasetIndex); 450 if (dataset != null) { 451 Number n = dataset.getValue(); 452 if (n != null) { 453 result = n.doubleValue(); 454 } 455 } 456 return result; 457 } 458 459 465 public void addScale(int index, DialScale scale) { 466 this.layers.add(scale); 467 this.scales.set(index, scale); 468 } 469 470 477 public DialScale getScale(int index) { 478 DialScale result = null; 479 if (this.scales.size() > index) { 480 result = (DialScale) this.scales.get(index); 481 } 482 return result; 483 } 484 485 491 public void mapDatasetToScale(int index, int scaleIndex) { 492 this.datasetToScaleMap.set(index, new Integer (scaleIndex)); 493 notifyListeners(new PlotChangeEvent(this)); 494 } 495 496 503 public DialScale getScaleForDataset(int datasetIndex) { 504 DialScale result = (DialScale) this.scales.get(0); 505 Integer scaleIndex = (Integer ) this.datasetToScaleMap.get(datasetIndex); 506 if (scaleIndex != null) { 507 result = getScale(scaleIndex.intValue()); 508 } 509 return result; 510 } 511 512 521 public static Rectangle2D rectangleByRadius(Rectangle2D rect, 522 double radiusW, double radiusH) { 523 double x = rect.getCenterX(); 524 double y = rect.getCenterY(); 525 double w = rect.getWidth() * radiusW; 526 double h = rect.getHeight() * radiusH; 527 return new Rectangle2D.Double (x - w / 2.0, y - h / 2.0, w, h); 528 } 529 530 535 public void dialLayerChanged(DialLayerChangeEvent event) { 536 this.notifyListeners(new PlotChangeEvent(this)); 537 } 538 539 548 public boolean equals(Object obj) { 549 if (obj == this) { 550 return true; 551 } 552 if (!(obj instanceof DialPlot)) { 553 return false; 554 } 555 DialPlot that = (DialPlot) obj; 556 if (!ObjectUtilities.equal(this.background, that.background)) { 557 return false; 558 } 559 if (!ObjectUtilities.equal(this.cap, that.cap)) { 560 return false; 561 } 562 if (!this.dialFrame.equals(that.dialFrame)) { 563 return false; 564 } 565 if (this.viewX != that.viewX) { 566 return false; 567 } 568 if (this.viewY != that.viewY) { 569 return false; 570 } 571 if (this.viewW != that.viewW) { 572 return false; 573 } 574 if (this.viewH != that.viewH) { 575 return false; 576 } 577 if (!this.layers.equals(that.layers)) { 578 return false; 579 } 580 return super.equals(obj); 581 } 582 583 588 public int hashCode() { 589 int result = 193; 590 result = 37 * result + ObjectUtilities.hashCode(this.background); 591 result = 37 * result + ObjectUtilities.hashCode(this.cap); 592 result = 37 * result + this.dialFrame.hashCode(); 593 long temp = Double.doubleToLongBits(this.viewX); 594 result = (int) (temp ^ (temp >>> 32)); 595 temp = Double.doubleToLongBits(this.viewY); 596 result = (int) (temp ^ (temp >>> 32)); 597 temp = Double.doubleToLongBits(this.viewW); 598 result = (int) (temp ^ (temp >>> 32)); 599 temp = Double.doubleToLongBits(this.viewH); 600 result = (int) (temp ^ (temp >>> 32)); 601 return result; 602 } 603 604 609 public String getPlotType() { 610 return "DialPlot"; 611 } 612 613 620 private void writeObject(ObjectOutputStream stream) throws IOException { 621 stream.defaultWriteObject(); 622 } 623 624 632 private void readObject(ObjectInputStream stream) 633 throws IOException , ClassNotFoundException { 634 stream.defaultReadObject(); 635 } 636 637 638 } 639 | Popular Tags |