1 78 79 package org.jfree.chart.plot; 80 81 import java.awt.AlphaComposite ; 82 import java.awt.Color ; 83 import java.awt.Composite ; 84 import java.awt.Font ; 85 import java.awt.FontMetrics ; 86 import java.awt.Graphics2D ; 87 import java.awt.Paint ; 88 import java.awt.Polygon ; 89 import java.awt.Shape ; 90 import java.awt.Stroke ; 91 import java.awt.geom.Arc2D ; 92 import java.awt.geom.Area ; 93 import java.awt.geom.Ellipse2D ; 94 import java.awt.geom.Point2D ; 95 import java.awt.geom.Rectangle2D ; 96 import java.io.Serializable ; 97 import java.util.ArrayList ; 98 import java.util.Iterator ; 99 import java.util.List ; 100 101 import org.jfree.chart.entity.EntityCollection; 102 import org.jfree.chart.entity.PieSectionEntity; 103 import org.jfree.chart.labels.PieToolTipGenerator; 104 import org.jfree.data.general.DatasetUtilities; 105 import org.jfree.data.general.PieDataset; 106 import org.jfree.ui.RectangleInsets; 107 108 115 public class PiePlot3D extends PiePlot implements Serializable { 116 117 118 private static final long serialVersionUID = 3408984188945161432L; 119 120 121 private double depthFactor = 0.2; 122 123 126 public PiePlot3D() { 127 this(null); 128 } 129 130 136 public PiePlot3D(PieDataset dataset) { 137 super(dataset); 138 setCircular(false, false); 139 } 140 141 146 public void setDepthFactor(double factor) { 147 this.depthFactor = factor; 148 } 149 150 155 public double getDepthFactor () { 156 return this.depthFactor; 157 } 158 159 172 public void draw(Graphics2D g2, Rectangle2D plotArea, Point2D anchor, 173 PlotState parentState, 174 PlotRenderingInfo info) { 175 176 RectangleInsets insets = getInsets(); 178 insets.trim(plotArea); 179 180 Rectangle2D originalPlotArea = (Rectangle2D ) plotArea.clone(); 181 if (info != null) { 182 info.setPlotArea(plotArea); 183 info.setDataArea(plotArea); 184 } 185 186 Shape savedClip = g2.getClip(); 187 g2.clip(plotArea); 188 189 double gapPercent = getInteriorGap(); 191 double labelPercent = 0.0; 192 if (getLabelGenerator() != null) { 193 labelPercent = getLabelGap() + getMaximumLabelWidth() 194 + getLabelLinkMargin(); 195 } 196 double gapHorizontal = plotArea.getWidth() 197 * (gapPercent + labelPercent); 198 double gapVertical = plotArea.getHeight() * gapPercent; 199 200 double linkX = plotArea.getX() + gapHorizontal / 2; 201 double linkY = plotArea.getY() + gapVertical / 2; 202 double linkW = plotArea.getWidth() - gapHorizontal; 203 double linkH = plotArea.getHeight() - gapVertical; 204 205 if (isCircular()) { double min = Math.min(linkW, linkH) / 2; 208 linkX = (linkX + linkX + linkW) / 2 - min; 209 linkY = (linkY + linkY + linkH) / 2 - min; 210 linkW = 2 * min; 211 linkH = 2 * min; 212 } 213 214 PiePlotState state = initialise(g2, plotArea, this, null, info); 215 double hh = linkW * getLabelLinkMargin(); 219 double vv = linkH * getLabelLinkMargin(); 220 Rectangle2D explodeArea = new Rectangle2D.Double (linkX + hh / 2.0, 221 linkY + vv / 2.0, linkW - hh, linkH - vv); 222 223 state.setExplodedPieArea(explodeArea); 224 225 double maximumExplodePercent = getMaximumExplodePercent(); 229 double percent = maximumExplodePercent / (1.0 + maximumExplodePercent); 230 231 double h1 = explodeArea.getWidth() * percent; 232 double v1 = explodeArea.getHeight() * percent; 233 Rectangle2D pieArea = new Rectangle2D.Double (explodeArea.getX() 234 + h1 / 2.0, explodeArea.getY() + v1 / 2.0, 235 explodeArea.getWidth() - h1, explodeArea.getHeight() - v1); 236 237 int depth = (int) (pieArea.getHeight() * this.depthFactor); 238 Rectangle2D linkArea = new Rectangle2D.Double (linkX, linkY, linkW, 241 linkH - depth); 242 state.setLinkArea(linkArea); 243 244 state.setPieArea(pieArea); 245 state.setPieCenterX(pieArea.getCenterX()); 246 state.setPieCenterY(pieArea.getCenterY() - depth / 2.0); 247 state.setPieWRadius(pieArea.getWidth() / 2.0); 248 state.setPieHRadius((pieArea.getHeight() - depth) / 2.0); 249 250 drawBackground(g2, plotArea); 251 PieDataset dataset = getDataset(); 253 if (DatasetUtilities.isEmptyOrNull(getDataset())) { 254 drawNoDataMessage(g2, plotArea); 255 g2.setClip(savedClip); 256 drawOutline(g2, plotArea); 257 return; 258 } 259 260 if (dataset.getKeys().size() > plotArea.getWidth()) { 262 String text = "Too many elements"; 263 Font sfont = new Font ("dialog", Font.BOLD, 10); 264 g2.setFont(sfont); 265 FontMetrics fm = g2.getFontMetrics(sfont); 266 int stringWidth = fm.stringWidth(text); 267 268 g2.drawString(text, (int) (plotArea.getX() + (plotArea.getWidth() 269 - stringWidth) / 2), (int) (plotArea.getY() 270 + (plotArea.getHeight() / 2))); 271 return; 272 } 273 if (isCircular()) { 277 double min = Math.min(plotArea.getWidth(), 278 plotArea.getHeight()) / 2; 279 plotArea = new Rectangle2D.Double (plotArea.getCenterX() - min, 280 plotArea.getCenterY() - min, 2 * min, 2 * min); 281 } 282 List sectionKeys = dataset.getKeys(); 284 285 if (sectionKeys.size() == 0) { 286 return; 287 } 288 289 double arcX = pieArea.getX(); 291 double arcY = pieArea.getY(); 292 293 Composite originalComposite = g2.getComposite(); 295 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 296 getForegroundAlpha())); 297 298 double totalValue = DatasetUtilities.calculatePieDatasetTotal(dataset); 299 double runningTotal = 0; 300 if (depth < 0) { 301 return; } 303 304 ArrayList arcList = new ArrayList (); 305 Arc2D.Double arc; 306 Paint paint; 307 Paint outlinePaint; 308 Stroke outlineStroke; 309 310 Iterator iterator = sectionKeys.iterator(); 311 while (iterator.hasNext()) { 312 313 Comparable currentKey = (Comparable ) iterator.next(); 314 Number dataValue = dataset.getValue(currentKey); 315 if (dataValue == null) { 316 arcList.add(null); 317 continue; 318 } 319 double value = dataValue.doubleValue(); 320 if (value <= 0) { 321 arcList.add(null); 322 continue; 323 } 324 double startAngle = getStartAngle(); 325 double direction = getDirection().getFactor(); 326 double angle1 = startAngle + (direction * (runningTotal * 360)) 327 / totalValue; 328 double angle2 = startAngle + (direction * (runningTotal + value) 329 * 360) / totalValue; 330 if (Math.abs(angle2 - angle1) > getMinimumArcAngleToDraw()) { 331 arcList.add(new Arc2D.Double (arcX, arcY + depth, 332 pieArea.getWidth(), pieArea.getHeight() - depth, 333 angle1, angle2 - angle1, Arc2D.PIE)); 334 } 335 else { 336 arcList.add(null); 337 } 338 runningTotal += value; 339 } 340 341 Shape oldClip = g2.getClip(); 342 343 Ellipse2D top = new Ellipse2D.Double (pieArea.getX(), pieArea.getY(), 344 pieArea.getWidth(), pieArea.getHeight() - depth); 345 346 Ellipse2D bottom = new Ellipse2D.Double (pieArea.getX(), pieArea.getY() 347 + depth, pieArea.getWidth(), pieArea.getHeight() - depth); 348 349 Rectangle2D lower = new Rectangle2D.Double (top.getX(), 350 top.getCenterY(), pieArea.getWidth(), bottom.getMaxY() 351 - top.getCenterY()); 352 353 Rectangle2D upper = new Rectangle2D.Double (pieArea.getX(), top.getY(), 354 pieArea.getWidth(), bottom.getCenterY() - top.getY()); 355 356 Area a = new Area (top); 357 a.add(new Area (lower)); 358 Area b = new Area (bottom); 359 b.add(new Area (upper)); 360 Area pie = new Area (a); 361 pie.intersect(b); 362 363 Area front = new Area (pie); 364 front.subtract(new Area (top)); 365 366 Area back = new Area (pie); 367 back.subtract(new Area (bottom)); 368 369 int[] xs; 371 int[] ys; 372 arc = new Arc2D.Double (arcX, arcY + depth, pieArea.getWidth(), 373 pieArea.getHeight() - depth, 0, 360, Arc2D.PIE); 374 375 int categoryCount = arcList.size(); 376 for (int categoryIndex = 0; categoryIndex < categoryCount; 377 categoryIndex++) { 378 arc = (Arc2D.Double ) arcList.get(categoryIndex); 379 if (arc == null) { 380 continue; 381 } 382 Comparable key = getSectionKey(categoryIndex); 383 paint = lookupSectionPaint(key, true); 384 outlinePaint = lookupSectionOutlinePaint(key); 385 outlineStroke = lookupSectionOutlineStroke(key); 386 g2.setPaint(paint); 387 g2.fill(arc); 388 g2.setPaint(outlinePaint); 389 g2.setStroke(outlineStroke); 390 g2.draw(arc); 391 g2.setPaint(paint); 392 393 Point2D p1 = arc.getStartPoint(); 394 395 xs = new int[] {(int) arc.getCenterX(), (int) arc.getCenterX(), 397 (int) p1.getX(), (int) p1.getX()}; 398 ys = new int[] {(int) arc.getCenterY(), (int) arc.getCenterY() 399 - depth, (int) p1.getY() - depth, (int) p1.getY()}; 400 Polygon polygon = new Polygon (xs, ys, 4); 401 g2.setPaint(java.awt.Color.lightGray); 402 g2.fill(polygon); 403 g2.setPaint(outlinePaint); 404 g2.setStroke(outlineStroke); 405 g2.draw(polygon); 406 g2.setPaint(paint); 407 408 } 409 410 g2.setPaint(Color.gray); 411 g2.fill(back); 412 g2.fill(front); 413 414 int cat = 0; 416 iterator = arcList.iterator(); 417 while (iterator.hasNext()) { 418 Arc2D segment = (Arc2D ) iterator.next(); 419 if (segment != null) { 420 Comparable key = getSectionKey(cat); 421 paint = lookupSectionPaint(key, true); 422 outlinePaint = lookupSectionOutlinePaint(key); 423 outlineStroke = lookupSectionOutlineStroke(key); 424 drawSide(g2, pieArea, segment, front, back, paint, 425 outlinePaint, outlineStroke, false, true); 426 } 427 cat++; 428 } 429 430 cat = 0; 432 iterator = arcList.iterator(); 433 while (iterator.hasNext()) { 434 Arc2D segment = (Arc2D ) iterator.next(); 435 if (segment != null) { 436 Comparable key = getSectionKey(cat); 437 paint = lookupSectionPaint(key); 438 outlinePaint = lookupSectionOutlinePaint(key); 439 outlineStroke = lookupSectionOutlineStroke(key); 440 drawSide(g2, pieArea, segment, front, back, paint, 441 outlinePaint, outlineStroke, true, false); 442 } 443 cat++; 444 } 445 446 g2.setClip(oldClip); 447 448 Arc2D upperArc; 450 for (int sectionIndex = 0; sectionIndex < categoryCount; 451 sectionIndex++) { 452 arc = (Arc2D.Double ) arcList.get(sectionIndex); 453 if (arc == null) { 454 continue; 455 } 456 upperArc = new Arc2D.Double (arcX, arcY, pieArea.getWidth(), 457 pieArea.getHeight() - depth, arc.getAngleStart(), 458 arc.getAngleExtent(), Arc2D.PIE); 459 460 Comparable currentKey = (Comparable ) sectionKeys.get(sectionIndex); 461 paint = lookupSectionPaint(currentKey, true); 462 outlinePaint = lookupSectionOutlinePaint(currentKey); 463 outlineStroke = lookupSectionOutlineStroke(currentKey); 464 g2.setPaint(paint); 465 g2.fill(upperArc); 466 g2.setStroke(outlineStroke); 467 g2.setPaint(outlinePaint); 468 g2.draw(upperArc); 469 470 if (info != null) { 472 EntityCollection entities 473 = info.getOwner().getEntityCollection(); 474 if (entities != null) { 475 String tip = null; 476 PieToolTipGenerator tipster = getToolTipGenerator(); 477 if (tipster != null) { 478 tip = tipster.generateToolTip(dataset, currentKey); 480 } 481 String url = null; 482 if (getURLGenerator() != null) { 483 url = getURLGenerator().generateURL(dataset, currentKey, 484 getPieIndex()); 485 } 486 PieSectionEntity entity = new PieSectionEntity( 487 upperArc, dataset, getPieIndex(), sectionIndex, 488 currentKey, tip, url); 489 entities.add(entity); 490 } 491 } 492 List keys = dataset.getKeys(); 493 Rectangle2D adjustedPlotArea = new Rectangle2D.Double ( 494 originalPlotArea.getX(), originalPlotArea.getY(), 495 originalPlotArea.getWidth(), originalPlotArea.getHeight() 496 - depth); 497 drawLabels(g2, keys, totalValue, adjustedPlotArea, linkArea, state); 498 } 499 500 g2.setClip(savedClip); 501 g2.setComposite(originalComposite); 502 drawOutline(g2, originalPlotArea); 503 504 } 505 506 520 protected void drawSide(Graphics2D g2, 521 Rectangle2D plotArea, 522 Arc2D arc, 523 Area front, 524 Area back, 525 Paint paint, 526 Paint outlinePaint, 527 Stroke outlineStroke, 528 boolean drawFront, 529 boolean drawBack) { 530 531 double start = arc.getAngleStart(); 532 double extent = arc.getAngleExtent(); 533 double end = start + extent; 534 535 g2.setStroke(outlineStroke); 536 537 if (extent < 0.0) { 539 540 if (isAngleAtFront(start)) { 542 if (!isAngleAtBack(end)) { 543 544 if (extent > -180.0) { if (drawFront) { 547 Area side = new Area (new Rectangle2D.Double ( 548 arc.getEndPoint().getX(), plotArea.getY(), 549 arc.getStartPoint().getX() 550 - arc.getEndPoint().getX(), 551 plotArea.getHeight())); 552 side.intersect(front); 553 g2.setPaint(paint); 554 g2.fill(side); 555 g2.setPaint(outlinePaint); 556 g2.draw(side); 557 } 558 } 559 else { Area side1 = new Area (new Rectangle2D.Double ( 563 plotArea.getX(), plotArea.getY(), 564 arc.getStartPoint().getX() - plotArea.getX(), 565 plotArea.getHeight())); 566 side1.intersect(front); 567 568 Area side2 = new Area (new Rectangle2D.Double ( 569 arc.getEndPoint().getX(), plotArea.getY(), 570 plotArea.getMaxX() - arc.getEndPoint().getX(), 571 plotArea.getHeight())); 572 573 side2.intersect(front); 574 g2.setPaint(paint); 575 if (drawFront) { 576 g2.fill(side1); 577 g2.fill(side2); 578 } 579 580 if (drawBack) { 581 g2.fill(back); 582 } 583 584 g2.setPaint(outlinePaint); 585 if (drawFront) { 586 g2.draw(side1); 587 g2.draw(side2); 588 } 589 590 if (drawBack) { 591 g2.draw(back); 592 } 593 594 } 595 } 596 else { 599 if (drawBack) { 600 Area side2 = new Area (new Rectangle2D.Double ( 601 plotArea.getX(), plotArea.getY(), 602 arc.getEndPoint().getX() - plotArea.getX(), 603 plotArea.getHeight())); 604 side2.intersect(back); 605 g2.setPaint(paint); 606 g2.fill(side2); 607 g2.setPaint(outlinePaint); 608 g2.draw(side2); 609 } 610 611 if (drawFront) { 612 Area side1 = new Area (new Rectangle2D.Double ( 613 plotArea.getX(), plotArea.getY(), 614 arc.getStartPoint().getX() - plotArea.getX(), 615 plotArea.getHeight())); 616 side1.intersect(front); 617 g2.setPaint(paint); 618 g2.fill(side1); 619 g2.setPaint(outlinePaint); 620 g2.draw(side1); 621 } 622 } 623 } 624 else { 627 if (!isAngleAtFront(end)) { 628 if (extent > -180.0) { if (drawBack) { 630 Area side = new Area (new Rectangle2D.Double ( 631 arc.getStartPoint().getX(), plotArea.getY(), 632 arc.getEndPoint().getX() 633 - arc.getStartPoint().getX(), 634 plotArea.getHeight())); 635 side.intersect(back); 636 g2.setPaint(paint); 637 g2.fill(side); 638 g2.setPaint(outlinePaint); 639 g2.draw(side); 640 } 641 } 642 else { Area side1 = new Area (new Rectangle2D.Double ( 645 arc.getStartPoint().getX(), plotArea.getY(), 646 plotArea.getMaxX() - arc.getStartPoint().getX(), 647 plotArea.getHeight())); 648 side1.intersect(back); 649 650 Area side2 = new Area (new Rectangle2D.Double ( 651 plotArea.getX(), plotArea.getY(), 652 arc.getEndPoint().getX() - plotArea.getX(), 653 plotArea.getHeight())); 654 655 side2.intersect(back); 656 657 g2.setPaint(paint); 658 if (drawBack) { 659 g2.fill(side1); 660 g2.fill(side2); 661 } 662 663 if (drawFront) { 664 g2.fill(front); 665 } 666 667 g2.setPaint(outlinePaint); 668 if (drawBack) { 669 g2.draw(side1); 670 g2.draw(side2); 671 } 672 673 if (drawFront) { 674 g2.draw(front); 675 } 676 677 } 678 } 679 else { 681 if (drawBack) { 682 Area side1 = new Area (new Rectangle2D.Double ( 683 arc.getStartPoint().getX(), plotArea.getY(), 684 plotArea.getMaxX() - arc.getStartPoint().getX(), 685 plotArea.getHeight())); 686 side1.intersect(back); 687 g2.setPaint(paint); 688 g2.fill(side1); 689 g2.setPaint(outlinePaint); 690 g2.draw(side1); 691 } 692 693 if (drawFront) { 694 Area side2 = new Area (new Rectangle2D.Double ( 695 arc.getEndPoint().getX(), plotArea.getY(), 696 plotArea.getMaxX() - arc.getEndPoint().getX(), 697 plotArea.getHeight())); 698 side2.intersect(front); 699 g2.setPaint(paint); 700 g2.fill(side2); 701 g2.setPaint(outlinePaint); 702 g2.draw(side2); 703 } 704 705 } 706 } 707 } 708 else if (extent > 0.0) { 710 if (isAngleAtFront(start)) { 712 if (!isAngleAtBack(end)) { 714 if (extent < 180.0) { if (drawFront) { 716 Area side = new Area (new Rectangle2D.Double ( 717 arc.getStartPoint().getX(), plotArea.getY(), 718 arc.getEndPoint().getX() 719 - arc.getStartPoint().getX(), 720 plotArea.getHeight())); 721 side.intersect(front); 722 g2.setPaint(paint); 723 g2.fill(side); 724 g2.setPaint(outlinePaint); 725 g2.draw(side); 726 } 727 } 728 else { Area side1 = new Area (new Rectangle2D.Double ( 730 arc.getStartPoint().getX(), plotArea.getY(), 731 plotArea.getMaxX() - arc.getStartPoint().getX(), 732 plotArea.getHeight())); 733 side1.intersect(front); 734 735 Area side2 = new Area (new Rectangle2D.Double ( 736 plotArea.getX(), plotArea.getY(), 737 arc.getEndPoint().getX() - plotArea.getX(), 738 plotArea.getHeight())); 739 side2.intersect(front); 740 741 g2.setPaint(paint); 742 if (drawFront) { 743 g2.fill(side1); 744 g2.fill(side2); 745 } 746 747 if (drawBack) { 748 g2.fill(back); 749 } 750 751 g2.setPaint(outlinePaint); 752 if (drawFront) { 753 g2.draw(side1); 754 g2.draw(side2); 755 } 756 757 if (drawBack) { 758 g2.draw(back); 759 } 760 761 } 762 } 763 else { if (drawBack) { 765 Area side2 = new Area (new Rectangle2D.Double ( 766 arc.getEndPoint().getX(), plotArea.getY(), 767 plotArea.getMaxX() - arc.getEndPoint().getX(), 768 plotArea.getHeight())); 769 side2.intersect(back); 770 g2.setPaint(paint); 771 g2.fill(side2); 772 g2.setPaint(outlinePaint); 773 g2.draw(side2); 774 } 775 776 if (drawFront) { 777 Area side1 = new Area (new Rectangle2D.Double ( 778 arc.getStartPoint().getX(), plotArea.getY(), 779 plotArea.getMaxX() - arc.getStartPoint().getX(), 780 plotArea.getHeight())); 781 side1.intersect(front); 782 g2.setPaint(paint); 783 g2.fill(side1); 784 g2.setPaint(outlinePaint); 785 g2.draw(side1); 786 } 787 } 788 } 789 else { 791 if (!isAngleAtFront(end)) { 792 if (extent < 180.0) { if (drawBack) { 794 Area side = new Area (new Rectangle2D.Double ( 795 arc.getEndPoint().getX(), plotArea.getY(), 796 arc.getStartPoint().getX() 797 - arc.getEndPoint().getX(), 798 plotArea.getHeight())); 799 side.intersect(back); 800 g2.setPaint(paint); 801 g2.fill(side); 802 g2.setPaint(outlinePaint); 803 g2.draw(side); 804 } 805 } 806 else { Area side1 = new Area (new Rectangle2D.Double ( 809 arc.getStartPoint().getX(), plotArea.getY(), 810 plotArea.getX() - arc.getStartPoint().getX(), 811 plotArea.getHeight())); 812 side1.intersect(back); 813 814 Area side2 = new Area (new Rectangle2D.Double ( 815 arc.getEndPoint().getX(), plotArea.getY(), 816 plotArea.getMaxX() - arc.getEndPoint().getX(), 817 plotArea.getHeight())); 818 side2.intersect(back); 819 820 g2.setPaint(paint); 821 if (drawBack) { 822 g2.fill(side1); 823 g2.fill(side2); 824 } 825 826 if (drawFront) { 827 g2.fill(front); 828 } 829 830 g2.setPaint(outlinePaint); 831 if (drawBack) { 832 g2.draw(side1); 833 g2.draw(side2); 834 } 835 836 if (drawFront) { 837 g2.draw(front); 838 } 839 840 } 841 } 842 else { if (drawBack) { 845 Area side1 = new Area (new Rectangle2D.Double ( 846 plotArea.getX(), plotArea.getY(), 847 arc.getStartPoint().getX() - plotArea.getX(), 848 plotArea.getHeight())); 849 side1.intersect(back); 850 g2.setPaint(paint); 851 g2.fill(side1); 852 g2.setPaint(outlinePaint); 853 g2.draw(side1); 854 } 855 856 if (drawFront) { 857 Area side2 = new Area (new Rectangle2D.Double ( 858 plotArea.getX(), plotArea.getY(), 859 arc.getEndPoint().getX() - plotArea.getX(), 860 plotArea.getHeight())); 861 side2.intersect(front); 862 g2.setPaint(paint); 863 g2.fill(side2); 864 g2.setPaint(outlinePaint); 865 g2.draw(side2); 866 } 867 } 868 } 869 870 } 871 872 } 873 874 879 public String getPlotType () { 880 return localizationResources.getString("Pie_3D_Plot"); 881 } 882 883 892 private boolean isAngleAtFront(double angle) { 893 return (Math.sin(Math.toRadians(angle)) < 0.0); 894 } 895 896 905 private boolean isAngleAtBack(double angle) { 906 return (Math.sin(Math.toRadians(angle)) > 0.0); 907 } 908 909 } 910 | Popular Tags |