1 49 50 package org.jfree.chart.plot; 51 52 import java.awt.BasicStroke ; 53 import java.awt.Color ; 54 import java.awt.Graphics2D ; 55 import java.awt.Paint ; 56 import java.awt.Shape ; 57 import java.awt.Stroke ; 58 import java.awt.geom.Arc2D ; 59 import java.awt.geom.GeneralPath ; 60 import java.awt.geom.Line2D ; 61 import java.awt.geom.Rectangle2D ; 62 import java.io.IOException ; 63 import java.io.ObjectInputStream ; 64 import java.io.ObjectOutputStream ; 65 import java.io.Serializable ; 66 67 import org.jfree.chart.entity.EntityCollection; 68 import org.jfree.chart.entity.PieSectionEntity; 69 import org.jfree.chart.event.PlotChangeEvent; 70 import org.jfree.chart.labels.PieToolTipGenerator; 71 import org.jfree.chart.urls.PieURLGenerator; 72 import org.jfree.data.general.PieDataset; 73 import org.jfree.io.SerialUtilities; 74 import org.jfree.ui.RectangleInsets; 75 import org.jfree.util.ObjectUtilities; 76 import org.jfree.util.PaintUtilities; 77 import org.jfree.util.Rotation; 78 import org.jfree.util.ShapeUtilities; 79 import org.jfree.util.UnitType; 80 81 84 public class RingPlot extends PiePlot implements Cloneable , Serializable { 85 86 87 private static final long serialVersionUID = 1556064784129676620L; 88 89 93 private boolean separatorsVisible; 94 95 96 private transient Stroke separatorStroke; 97 98 99 private transient Paint separatorPaint; 100 101 105 private double innerSeparatorExtension; 106 107 111 private double outerSeparatorExtension; 112 113 116 private double sectionDepth; 117 118 121 public RingPlot() { 122 this(null); 123 } 124 125 130 public RingPlot(PieDataset dataset) { 131 super(dataset); 132 this.separatorsVisible = true; 133 this.separatorStroke = new BasicStroke (0.5f); 134 this.separatorPaint = Color.gray; 135 this.innerSeparatorExtension = 0.20; this.outerSeparatorExtension = 0.20; this.sectionDepth = 0.20; } 139 140 148 public boolean getSeparatorsVisible() { 149 return this.separatorsVisible; 150 } 151 152 161 public void setSeparatorsVisible(boolean visible) { 162 this.separatorsVisible = visible; 163 notifyListeners(new PlotChangeEvent(this)); 164 } 165 166 173 public Stroke getSeparatorStroke() { 174 return this.separatorStroke; 175 } 176 177 184 public void setSeparatorStroke(Stroke stroke) { 185 if (stroke == null) { 186 throw new IllegalArgumentException ("Null 'stroke' argument."); 187 } 188 this.separatorStroke = stroke; 189 notifyListeners(new PlotChangeEvent(this)); 190 } 191 192 199 public Paint getSeparatorPaint() { 200 return this.separatorPaint; 201 } 202 203 210 public void setSeparatorPaint(Paint paint) { 211 if (paint == null) { 212 throw new IllegalArgumentException ("Null 'paint' argument."); 213 } 214 this.separatorPaint = paint; 215 notifyListeners(new PlotChangeEvent(this)); 216 } 217 218 227 public double getInnerSeparatorExtension() { 228 return this.innerSeparatorExtension; 229 } 230 231 242 public void setInnerSeparatorExtension(double percent) { 243 this.innerSeparatorExtension = percent; 244 notifyListeners(new PlotChangeEvent(this)); 245 } 246 247 256 public double getOuterSeparatorExtension() { 257 return this.outerSeparatorExtension; 258 } 259 260 270 public void setOuterSeparatorExtension(double percent) { 271 this.outerSeparatorExtension = percent; 272 notifyListeners(new PlotChangeEvent(this)); 273 } 274 275 284 public double getSectionDepth() { 285 return this.sectionDepth; 286 } 287 288 297 public void setSectionDepth(double sectionDepth) { 298 this.sectionDepth = sectionDepth; 299 } 300 301 316 public PiePlotState initialise(Graphics2D g2, Rectangle2D plotArea, 317 PiePlot plot, Integer index, PlotRenderingInfo info) { 318 319 PiePlotState state = super.initialise(g2, plotArea, plot, index, info); 320 state.setPassesRequired(3); 321 return state; 322 323 } 324 325 334 protected void drawItem(Graphics2D g2, 335 int section, 336 Rectangle2D dataArea, 337 PiePlotState state, 338 int currentPass) { 339 340 PieDataset dataset = getDataset(); 341 Number n = dataset.getValue(section); 342 if (n == null) { 343 return; 344 } 345 double value = n.doubleValue(); 346 double angle1 = 0.0; 347 double angle2 = 0.0; 348 349 Rotation direction = getDirection(); 350 if (direction == Rotation.CLOCKWISE) { 351 angle1 = state.getLatestAngle(); 352 angle2 = angle1 - value / state.getTotal() * 360.0; 353 } 354 else if (direction == Rotation.ANTICLOCKWISE) { 355 angle1 = state.getLatestAngle(); 356 angle2 = angle1 + value / state.getTotal() * 360.0; 357 } 358 else { 359 throw new IllegalStateException ("Rotation type not recognised."); 360 } 361 362 double angle = (angle2 - angle1); 363 if (Math.abs(angle) > getMinimumArcAngleToDraw()) { 364 Comparable key = getSectionKey(section); 365 double ep = 0.0; 366 double mep = getMaximumExplodePercent(); 367 if (mep > 0.0) { 368 ep = getExplodePercent(key) / mep; 369 } 370 Rectangle2D arcBounds = getArcBounds(state.getPieArea(), 371 state.getExplodedPieArea(), angle1, angle, ep); 372 Arc2D.Double arc = new Arc2D.Double (arcBounds, angle1, angle, 373 Arc2D.OPEN); 374 375 double depth = this.sectionDepth / 2.0; 377 RectangleInsets s = new RectangleInsets(UnitType.RELATIVE, 378 depth, depth, depth, depth); 379 Rectangle2D innerArcBounds = new Rectangle2D.Double (); 380 innerArcBounds.setRect(arcBounds); 381 s.trim(innerArcBounds); 382 Arc2D.Double arc2 = new Arc2D.Double (innerArcBounds, angle1 385 + angle, -angle, Arc2D.OPEN); 386 GeneralPath path = new GeneralPath (); 387 path.moveTo((float) arc.getStartPoint().getX(), 388 (float) arc.getStartPoint().getY()); 389 path.append(arc.getPathIterator(null), false); 390 path.append(arc2.getPathIterator(null), true); 391 path.closePath(); 392 393 Line2D separator = new Line2D.Double (arc2.getEndPoint(), 394 arc.getStartPoint()); 395 396 if (currentPass == 0) { 397 Paint shadowPaint = getShadowPaint(); 398 double shadowXOffset = getShadowXOffset(); 399 double shadowYOffset = getShadowYOffset(); 400 if (shadowPaint != null) { 401 Shape shadowArc = ShapeUtilities.createTranslatedShape( 402 path, (float) shadowXOffset, (float) shadowYOffset); 403 g2.setPaint(shadowPaint); 404 g2.fill(shadowArc); 405 } 406 } 407 else if (currentPass == 1) { 408 Paint paint = lookupSectionPaint(key, true); 409 g2.setPaint(paint); 410 g2.fill(path); 411 Paint outlinePaint = lookupSectionOutlinePaint(key); 412 Stroke outlineStroke = lookupSectionOutlineStroke(key); 413 if (outlinePaint != null && outlineStroke != null) { 414 g2.setPaint(outlinePaint); 415 g2.setStroke(outlineStroke); 416 g2.draw(path); 417 } 418 419 if (state.getInfo() != null) { 421 EntityCollection entities = state.getEntityCollection(); 422 if (entities != null) { 423 String tip = null; 424 PieToolTipGenerator toolTipGenerator 425 = getToolTipGenerator(); 426 if (toolTipGenerator != null) { 427 tip = toolTipGenerator.generateToolTip(dataset, 428 key); 429 } 430 String url = null; 431 PieURLGenerator urlGenerator = getURLGenerator(); 432 if (urlGenerator != null) { 433 url = urlGenerator.generateURL(dataset, key, 434 getPieIndex()); 435 } 436 PieSectionEntity entity = new PieSectionEntity(path, 437 dataset, getPieIndex(), section, key, tip, 438 url); 439 entities.add(entity); 440 } 441 } 442 } 443 else if (currentPass == 2) { 444 if (this.separatorsVisible) { 445 Line2D extendedSeparator = extendLine(separator, 446 this.innerSeparatorExtension, 447 this.outerSeparatorExtension); 448 g2.setStroke(this.separatorStroke); 449 g2.setPaint(this.separatorPaint); 450 g2.draw(extendedSeparator); 451 } 452 } 453 } 454 state.setLatestAngle(angle2); 455 } 456 457 464 public boolean equals(Object obj) { 465 if (this == obj) { 466 return true; 467 } 468 if (!(obj instanceof RingPlot)) { 469 return false; 470 } 471 RingPlot that = (RingPlot) obj; 472 if (this.separatorsVisible != that.separatorsVisible) { 473 return false; 474 } 475 if (!ObjectUtilities.equal(this.separatorStroke, 476 that.separatorStroke)) { 477 return false; 478 } 479 if (!PaintUtilities.equal(this.separatorPaint, that.separatorPaint)) { 480 return false; 481 } 482 if (this.innerSeparatorExtension != that.innerSeparatorExtension) { 483 return false; 484 } 485 if (this.outerSeparatorExtension != that.outerSeparatorExtension) { 486 return false; 487 } 488 if (this.sectionDepth != that.sectionDepth) { 489 return false; 490 } 491 return super.equals(obj); 492 } 493 494 504 private Line2D extendLine(Line2D line, double startPercent, 505 double endPercent) { 506 if (line == null) { 507 throw new IllegalArgumentException ("Null 'line' argument."); 508 } 509 double x1 = line.getX1(); 510 double x2 = line.getX2(); 511 double deltaX = x2 - x1; 512 double y1 = line.getY1(); 513 double y2 = line.getY2(); 514 double deltaY = y2 - y1; 515 x1 = x1 - (startPercent * deltaX); 516 y1 = y1 - (startPercent * deltaY); 517 x2 = x2 + (endPercent * deltaX); 518 y2 = y2 + (endPercent * deltaY); 519 return new Line2D.Double (x1, y1, x2, y2); 520 } 521 522 529 private void writeObject(ObjectOutputStream stream) throws IOException { 530 stream.defaultWriteObject(); 531 SerialUtilities.writeStroke(this.separatorStroke, stream); 532 SerialUtilities.writePaint(this.separatorPaint, stream); 533 } 534 535 543 private void readObject(ObjectInputStream stream) 544 throws IOException , ClassNotFoundException { 545 stream.defaultReadObject(); 546 this.separatorStroke = SerialUtilities.readStroke(stream); 547 this.separatorPaint = SerialUtilities.readPaint(stream); 548 } 549 550 } 551 | Popular Tags |