1 87 88 package org.jfree.chart.renderer.category; 89 90 import java.awt.Graphics2D ; 91 import java.awt.Paint ; 92 import java.awt.Shape ; 93 import java.awt.Stroke ; 94 import java.awt.geom.Line2D ; 95 import java.awt.geom.Rectangle2D ; 96 import java.io.Serializable ; 97 98 import org.jfree.chart.LegendItem; 99 import org.jfree.chart.axis.CategoryAxis; 100 import org.jfree.chart.axis.ValueAxis; 101 import org.jfree.chart.entity.EntityCollection; 102 import org.jfree.chart.event.RendererChangeEvent; 103 import org.jfree.chart.plot.CategoryPlot; 104 import org.jfree.chart.plot.PlotOrientation; 105 import org.jfree.data.category.CategoryDataset; 106 import org.jfree.util.BooleanList; 107 import org.jfree.util.BooleanUtilities; 108 import org.jfree.util.ObjectUtilities; 109 import org.jfree.util.PublicCloneable; 110 import org.jfree.util.ShapeUtilities; 111 112 116 public class LineAndShapeRenderer extends AbstractCategoryItemRenderer 117 implements Cloneable , PublicCloneable, 118 Serializable { 119 120 121 private static final long serialVersionUID = -197749519869226398L; 122 123 124 private Boolean linesVisible; 125 126 130 private BooleanList seriesLinesVisible; 131 132 136 private boolean baseLinesVisible; 137 138 141 private Boolean shapesVisible; 142 143 147 private BooleanList seriesShapesVisible; 148 149 150 private boolean baseShapesVisible; 151 152 153 private Boolean shapesFilled; 154 155 159 private BooleanList seriesShapesFilled; 160 161 162 private boolean baseShapesFilled; 163 164 168 private boolean useFillPaint; 169 170 171 private boolean drawOutlines; 172 173 177 private boolean useOutlinePaint; 178 179 182 public LineAndShapeRenderer() { 183 this(true, true); 184 } 185 186 192 public LineAndShapeRenderer(boolean lines, boolean shapes) { 193 super(); 194 this.linesVisible = null; 195 this.seriesLinesVisible = new BooleanList(); 196 this.baseLinesVisible = lines; 197 this.shapesVisible = null; 198 this.seriesShapesVisible = new BooleanList(); 199 this.baseShapesVisible = shapes; 200 this.shapesFilled = null; 201 this.seriesShapesFilled = new BooleanList(); 202 this.baseShapesFilled = true; 203 this.useFillPaint = false; 204 this.drawOutlines = true; 205 this.useOutlinePaint = false; 206 } 207 208 210 219 public boolean getItemLineVisible(int series, int item) { 220 Boolean flag = this.linesVisible; 221 if (flag == null) { 222 flag = getSeriesLinesVisible(series); 223 } 224 if (flag != null) { 225 return flag.booleanValue(); 226 } 227 else { 228 return this.baseLinesVisible; 229 } 230 } 231 232 239 public Boolean getLinesVisible() { 240 return this.linesVisible; 241 } 242 243 251 public void setLinesVisible(Boolean visible) { 252 this.linesVisible = visible; 253 notifyListeners(new RendererChangeEvent(this)); 254 } 255 256 263 public void setLinesVisible(boolean visible) { 264 setLinesVisible(BooleanUtilities.valueOf(visible)); 265 } 266 267 275 public Boolean getSeriesLinesVisible(int series) { 276 return this.seriesLinesVisible.getBoolean(series); 277 } 278 279 285 public void setSeriesLinesVisible(int series, Boolean flag) { 286 this.seriesLinesVisible.setBoolean(series, flag); 287 notifyListeners(new RendererChangeEvent(this)); 288 } 289 290 296 public void setSeriesLinesVisible(int series, boolean visible) { 297 setSeriesLinesVisible(series, BooleanUtilities.valueOf(visible)); 298 } 299 300 305 public boolean getBaseLinesVisible() { 306 return this.baseLinesVisible; 307 } 308 309 314 public void setBaseLinesVisible(boolean flag) { 315 this.baseLinesVisible = flag; 316 notifyListeners(new RendererChangeEvent(this)); 317 } 318 319 321 330 public boolean getItemShapeVisible(int series, int item) { 331 Boolean flag = this.shapesVisible; 332 if (flag == null) { 333 flag = getSeriesShapesVisible(series); 334 } 335 if (flag != null) { 336 return flag.booleanValue(); 337 } 338 else { 339 return this.baseShapesVisible; 340 } 341 } 342 343 349 public Boolean getShapesVisible() { 350 return this.shapesVisible; 351 } 352 353 359 public void setShapesVisible(Boolean visible) { 360 this.shapesVisible = visible; 361 notifyListeners(new RendererChangeEvent(this)); 362 } 363 364 370 public void setShapesVisible(boolean visible) { 371 setShapesVisible(BooleanUtilities.valueOf(visible)); 372 } 373 374 382 public Boolean getSeriesShapesVisible(int series) { 383 return this.seriesShapesVisible.getBoolean(series); 384 } 385 386 393 public void setSeriesShapesVisible(int series, boolean visible) { 394 setSeriesShapesVisible(series, BooleanUtilities.valueOf(visible)); 395 } 396 397 404 public void setSeriesShapesVisible(int series, Boolean flag) { 405 this.seriesShapesVisible.setBoolean(series, flag); 406 notifyListeners(new RendererChangeEvent(this)); 407 } 408 409 414 public boolean getBaseShapesVisible() { 415 return this.baseShapesVisible; 416 } 417 418 423 public void setBaseShapesVisible(boolean flag) { 424 this.baseShapesVisible = flag; 425 notifyListeners(new RendererChangeEvent(this)); 426 } 427 428 434 public boolean getDrawOutlines() { 435 return this.drawOutlines; 436 } 437 438 448 public void setDrawOutlines(boolean flag) { 449 this.drawOutlines = flag; 450 notifyListeners(new RendererChangeEvent(this)); 451 } 452 453 459 public boolean getUseOutlinePaint() { 460 return this.useOutlinePaint; 461 } 462 463 469 public void setUseOutlinePaint(boolean use) { 470 this.useOutlinePaint = use; 471 } 472 473 475 486 public boolean getItemShapeFilled(int series, int item) { 487 return getSeriesShapesFilled(series); 488 } 489 490 498 public boolean getSeriesShapesFilled(int series) { 499 500 if (this.shapesFilled != null) { 502 return this.shapesFilled.booleanValue(); 503 } 504 505 Boolean flag = this.seriesShapesFilled.getBoolean(series); 507 if (flag != null) { 508 return flag.booleanValue(); 509 } 510 else { 511 return this.baseShapesFilled; 512 } 513 514 } 515 516 522 public Boolean getShapesFilled() { 523 return this.shapesFilled; 524 } 525 526 531 public void setShapesFilled(boolean filled) { 532 if (filled) { 533 setShapesFilled(Boolean.TRUE); 534 } 535 else { 536 setShapesFilled(Boolean.FALSE); 537 } 538 } 539 540 545 public void setShapesFilled(Boolean filled) { 546 this.shapesFilled = filled; 547 } 548 549 555 public void setSeriesShapesFilled(int series, Boolean filled) { 556 this.seriesShapesFilled.setBoolean(series, filled); 557 } 558 559 565 public void setSeriesShapesFilled(int series, boolean filled) { 566 this.seriesShapesFilled.setBoolean( 567 series, BooleanUtilities.valueOf(filled) 568 ); 569 } 570 571 576 public boolean getBaseShapesFilled() { 577 return this.baseShapesFilled; 578 } 579 580 585 public void setBaseShapesFilled(boolean flag) { 586 this.baseShapesFilled = flag; 587 } 588 589 596 public boolean getUseFillPaint() { 597 return this.useFillPaint; 598 } 599 600 607 public void setUseFillPaint(boolean flag) { 608 this.useFillPaint = flag; 609 notifyListeners(new RendererChangeEvent(this)); 610 } 611 612 620 public LegendItem getLegendItem(int datasetIndex, int series) { 621 622 CategoryPlot cp = getPlot(); 623 if (cp == null) { 624 return null; 625 } 626 627 if (isSeriesVisible(series) && isSeriesVisibleInLegend(series)) { 628 CategoryDataset dataset; 629 dataset = cp.getDataset(datasetIndex); 630 String label = getLegendItemLabelGenerator().generateLabel( 631 dataset, series); 632 String description = label; 633 String toolTipText = null; 634 if (getLegendItemToolTipGenerator() != null) { 635 toolTipText = getLegendItemToolTipGenerator().generateLabel( 636 dataset, series); 637 } 638 String urlText = null; 639 if (getLegendItemURLGenerator() != null) { 640 urlText = getLegendItemURLGenerator().generateLabel( 641 dataset, series); 642 } 643 Shape shape = getSeriesShape(series); 644 Paint paint = getSeriesPaint(series); 645 Paint fillPaint = (this.useFillPaint 646 ? getItemFillPaint(series, 0) : paint); 647 boolean shapeOutlineVisible = this.drawOutlines; 648 Paint outlinePaint = (this.useOutlinePaint 649 ? getItemOutlinePaint(series, 0) : paint); 650 Stroke outlineStroke = getSeriesOutlineStroke(series); 651 boolean lineVisible = getItemLineVisible(series, 0); 652 boolean shapeVisible = getItemShapeVisible(series, 0); 653 return new LegendItem(label, description, toolTipText, 654 urlText, shapeVisible, shape, getItemShapeFilled(series, 0), 655 fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke, 656 lineVisible, new Line2D.Double (-7.0, 0.0, 7.0, 0.0), 657 getItemStroke(series, 0), getItemPaint(series, 0)); 658 } 659 return null; 660 661 } 662 663 668 public int getPassCount() { 669 return 2; 670 } 671 672 686 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 687 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 688 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, 689 int pass) { 690 691 if (!getItemVisible(row, column)) { 693 return; 694 } 695 696 if (!getItemLineVisible(row, column) 698 && !getItemShapeVisible(row, column)) { 699 return; 700 } 701 702 Number v = dataset.getValue(row, column); 704 if (v == null) { 705 return; 706 } 707 708 PlotOrientation orientation = plot.getOrientation(); 709 710 double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), 712 dataArea, plot.getDomainAxisEdge()); 713 double value = v.doubleValue(); 714 double y1 = rangeAxis.valueToJava2D(value, dataArea, 715 plot.getRangeAxisEdge()); 716 717 if (pass == 0 && getItemLineVisible(row, column)) { 718 if (column != 0) { 719 Number previousValue = dataset.getValue(row, column - 1); 720 if (previousValue != null) { 721 double previous = previousValue.doubleValue(); 723 double x0 = domainAxis.getCategoryMiddle(column - 1, 724 getColumnCount(), dataArea, 725 plot.getDomainAxisEdge()); 726 double y0 = rangeAxis.valueToJava2D(previous, dataArea, 727 plot.getRangeAxisEdge()); 728 729 Line2D line = null; 730 if (orientation == PlotOrientation.HORIZONTAL) { 731 line = new Line2D.Double (y0, x0, y1, x1); 732 } 733 else if (orientation == PlotOrientation.VERTICAL) { 734 line = new Line2D.Double (x0, y0, x1, y1); 735 } 736 g2.setPaint(getItemPaint(row, column)); 737 g2.setStroke(getItemStroke(row, column)); 738 g2.draw(line); 739 } 740 } 741 } 742 743 if (pass == 1) { 744 Shape shape = getItemShape(row, column); 745 if (orientation == PlotOrientation.HORIZONTAL) { 746 shape = ShapeUtilities.createTranslatedShape(shape, y1, x1); 747 } 748 else if (orientation == PlotOrientation.VERTICAL) { 749 shape = ShapeUtilities.createTranslatedShape(shape, x1, y1); 750 } 751 752 if (getItemShapeVisible(row, column)) { 753 if (getItemShapeFilled(row, column)) { 754 if (this.useFillPaint) { 755 g2.setPaint(getItemFillPaint(row, column)); 756 } 757 else { 758 g2.setPaint(getItemPaint(row, column)); 759 } 760 g2.fill(shape); 761 } 762 if (this.drawOutlines) { 763 if (this.useOutlinePaint) { 764 g2.setPaint(getItemOutlinePaint(row, column)); 765 } 766 else { 767 g2.setPaint(getItemPaint(row, column)); 768 } 769 g2.setStroke(getItemOutlineStroke(row, column)); 770 g2.draw(shape); 771 } 772 } 773 774 if (isItemLabelVisible(row, column)) { 776 if (orientation == PlotOrientation.HORIZONTAL) { 777 drawItemLabel(g2, orientation, dataset, row, column, y1, 778 x1, (value < 0.0)); 779 } 780 else if (orientation == PlotOrientation.VERTICAL) { 781 drawItemLabel(g2, orientation, dataset, row, column, x1, 782 y1, (value < 0.0)); 783 } 784 } 785 786 EntityCollection entities = state.getEntityCollection(); 788 if (entities != null) { 789 addItemEntity(entities, dataset, row, column, shape); 790 } 791 } 792 793 } 794 795 802 public boolean equals(Object obj) { 803 804 if (obj == this) { 805 return true; 806 } 807 if (!(obj instanceof LineAndShapeRenderer)) { 808 return false; 809 } 810 811 LineAndShapeRenderer that = (LineAndShapeRenderer) obj; 812 if (this.baseLinesVisible != that.baseLinesVisible) { 813 return false; 814 } 815 if (!ObjectUtilities.equal(this.seriesLinesVisible, 816 that.seriesLinesVisible)) { 817 return false; 818 } 819 if (!ObjectUtilities.equal(this.linesVisible, that.linesVisible)) { 820 return false; 821 } 822 if (this.baseShapesVisible != that.baseShapesVisible) { 823 return false; 824 } 825 if (!ObjectUtilities.equal(this.seriesShapesVisible, 826 that.seriesShapesVisible)) { 827 return false; 828 } 829 if (!ObjectUtilities.equal(this.shapesVisible, that.shapesVisible)) { 830 return false; 831 } 832 if (!ObjectUtilities.equal(this.shapesFilled, that.shapesFilled)) { 833 return false; 834 } 835 if (!ObjectUtilities.equal(this.seriesShapesFilled, 836 that.seriesShapesFilled)) { 837 return false; 838 } 839 if (this.baseShapesFilled != that.baseShapesFilled) { 840 return false; 841 } 842 if (this.useOutlinePaint != that.useOutlinePaint) { 843 return false; 844 } 845 if (!super.equals(obj)) { 846 return false; 847 } 848 return true; 849 } 850 851 858 public Object clone() throws CloneNotSupportedException { 859 LineAndShapeRenderer clone = (LineAndShapeRenderer) super.clone(); 860 clone.seriesLinesVisible 861 = (BooleanList) this.seriesLinesVisible.clone(); 862 clone.seriesShapesVisible 863 = (BooleanList) this.seriesLinesVisible.clone(); 864 clone.seriesShapesFilled 865 = (BooleanList) this.seriesShapesFilled.clone(); 866 return clone; 867 } 868 869 } 870 | Popular Tags |