001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------------------- 028 * XYLineAndShapeRenderer.java 029 * --------------------------- 030 * (C) Copyright 2004-2017, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes: 036 * -------- 037 * 27-Jan-2004 : Version 1 (DG); 038 * 10-Feb-2004 : Minor change to drawItem() method to make cut-and-paste 039 * overriding easier (DG); 040 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); 041 * 25-Aug-2004 : Added support for chart entities (required for tooltips) (DG); 042 * 24-Sep-2004 : Added flag to allow whole series to be drawn as a path 043 * (necessary when using a dashed stroke with many data 044 * items) (DG); 045 * 04-Oct-2004 : Renamed BooleanUtils --> BooleanUtilities (DG); 046 * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG); 047 * 27-Jan-2005 : The getLegendItem() method now omits hidden series (DG); 048 * 28-Jan-2005 : Added new constructor (DG); 049 * 09-Mar-2005 : Added fillPaint settings (DG); 050 * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG); 051 * 22-Jul-2005 : Renamed defaultLinesVisible --> baseLinesVisible, 052 * defaultShapesVisible --> baseShapesVisible and 053 * defaultShapesFilled --> baseShapesFilled (DG); 054 * 29-Jul-2005 : Added code to draw item labels (DG); 055 * ------------- JFREECHART 1.0.x --------------------------------------------- 056 * 20-Jul-2006 : Set dataset and series indices in LegendItem (DG); 057 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG); 058 * 21-Feb-2007 : Fixed bugs in clone() and equals() (DG); 059 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG); 060 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 061 * 08-Jun-2007 : Fix for bug 1731912 where entities are created even for data 062 * items that are not displayed (DG); 063 * 26-Oct-2007 : Deprecated override attributes (DG); 064 * 02-Jun-2008 : Fixed tooltips at lower edges of data area (DG); 065 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 066 * 19-Sep-2008 : Fixed bug with drawSeriesLineAsPath - patch by Greg Darke (DG); 067 * 18-May-2009 : Clip lines in drawPrimaryLine() (DG); 068 * 05-Jul-2012 : Removed JDK 1.3.1 code (DG); 069 * 02-Jul-2013 : Use ParamChecks (DG); 070 * 18-Feb-2017 : Updates for crosshairs (bug #36) (DG); 071 * 072 */ 073 074package org.jfree.chart.renderer.xy; 075 076import java.awt.Graphics2D; 077import java.awt.Paint; 078import java.awt.Shape; 079import java.awt.Stroke; 080import java.awt.geom.GeneralPath; 081import java.awt.geom.Line2D; 082import java.awt.geom.Rectangle2D; 083import java.io.IOException; 084import java.io.ObjectInputStream; 085import java.io.ObjectOutputStream; 086import java.io.Serializable; 087 088import org.jfree.chart.LegendItem; 089import org.jfree.chart.axis.ValueAxis; 090import org.jfree.chart.entity.EntityCollection; 091import org.jfree.chart.event.RendererChangeEvent; 092import org.jfree.chart.plot.CrosshairState; 093import org.jfree.chart.plot.PlotOrientation; 094import org.jfree.chart.plot.PlotRenderingInfo; 095import org.jfree.chart.plot.XYPlot; 096import org.jfree.chart.ui.RectangleEdge; 097import org.jfree.chart.util.BooleanList; 098import org.jfree.chart.util.LineUtils; 099import org.jfree.chart.util.ObjectUtils; 100import org.jfree.chart.util.Args; 101import org.jfree.chart.util.PublicCloneable; 102import org.jfree.chart.util.SerialUtils; 103import org.jfree.chart.util.ShapeUtils; 104import org.jfree.data.xy.XYDataset; 105 106/** 107 * A renderer that connects data points with lines and/or draws shapes at each 108 * data point. This renderer is designed for use with the {@link XYPlot} 109 * class. The example shown here is generated by 110 * the {@code XYLineAndShapeRendererDemo2.java} program included in the 111 * JFreeChart demo collection: 112 * <br><br> 113 * <img src="../../../../../images/XYLineAndShapeRendererSample.png" 114 * alt="XYLineAndShapeRendererSample.png"> 115 * 116 */ 117public class XYLineAndShapeRenderer extends AbstractXYItemRenderer 118 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable { 119 120 /** For serialization. */ 121 private static final long serialVersionUID = -7435246895986425885L; 122 123 /** 124 * A table of flags that control (per series) whether or not lines are 125 * visible. 126 */ 127 private BooleanList seriesLinesVisible; 128 129 /** The default value returned by the getLinesVisible() method. */ 130 private boolean defaultLinesVisible; 131 132 /** The shape that is used to represent a line in the legend. */ 133 private transient Shape legendLine; 134 135 /** 136 * A table of flags that control (per series) whether or not shapes are 137 * visible. 138 */ 139 private BooleanList seriesShapesVisible; 140 141 /** The default value returned by the getShapeVisible() method. */ 142 private boolean defaultShapesVisible; 143 144 /** 145 * A table of flags that control (per series) whether or not shapes are 146 * filled. 147 */ 148 private BooleanList seriesShapesFilled; 149 150 /** The default value returned by the getShapeFilled() method. */ 151 private boolean defaultShapesFilled; 152 153 /** A flag that controls whether outlines are drawn for shapes. */ 154 private boolean drawOutlines; 155 156 /** 157 * A flag that controls whether the fill paint is used for filling 158 * shapes. 159 */ 160 private boolean useFillPaint; 161 162 /** 163 * A flag that controls whether the outline paint is used for drawing shape 164 * outlines. 165 */ 166 private boolean useOutlinePaint; 167 168 /** 169 * A flag that controls whether or not each series is drawn as a single 170 * path. 171 */ 172 private boolean drawSeriesLineAsPath; 173 174 /** 175 * Creates a new renderer with both lines and shapes visible. 176 */ 177 public XYLineAndShapeRenderer() { 178 this(true, true); 179 } 180 181 /** 182 * Creates a new renderer. 183 * 184 * @param lines lines visible? 185 * @param shapes shapes visible? 186 */ 187 public XYLineAndShapeRenderer(boolean lines, boolean shapes) { 188 this.seriesLinesVisible = new BooleanList(); 189 this.defaultLinesVisible = lines; 190 this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0); 191 192 this.seriesShapesVisible = new BooleanList(); 193 this.defaultShapesVisible = shapes; 194 195 this.useFillPaint = false; // use item paint for fills by default 196 this.seriesShapesFilled = new BooleanList(); 197 this.defaultShapesFilled = true; 198 199 this.drawOutlines = true; 200 this.useOutlinePaint = false; // use item paint for outlines by 201 // default, not outline paint 202 203 this.drawSeriesLineAsPath = false; 204 } 205 206 /** 207 * Returns a flag that controls whether or not each series is drawn as a 208 * single path. The default value is {@code false}. 209 * 210 * @return A boolean. 211 * 212 * @see #setDrawSeriesLineAsPath(boolean) 213 */ 214 public boolean getDrawSeriesLineAsPath() { 215 return this.drawSeriesLineAsPath; 216 } 217 218 /** 219 * Sets the flag that controls whether or not each series is drawn as a 220 * single path and sends a {@link RendererChangeEvent} to all registered 221 * listeners. 222 * 223 * @param flag the flag. 224 * 225 * @see #getDrawSeriesLineAsPath() 226 */ 227 public void setDrawSeriesLineAsPath(boolean flag) { 228 if (this.drawSeriesLineAsPath != flag) { 229 this.drawSeriesLineAsPath = flag; 230 fireChangeEvent(); 231 } 232 } 233 234 /** 235 * Returns the number of passes through the data that the renderer requires 236 * in order to draw the chart. Most charts will require a single pass, but 237 * some require two passes. 238 * 239 * @return The pass count. 240 */ 241 @Override 242 public int getPassCount() { 243 return 2; 244 } 245 246 // LINES VISIBLE 247 248 /** 249 * Returns the flag used to control whether or not the shape for an item is 250 * visible. 251 * 252 * @param series the series index (zero-based). 253 * @param item the item index (zero-based). 254 * 255 * @return A boolean. 256 */ 257 public boolean getItemLineVisible(int series, int item) { 258 Boolean flag = getSeriesLinesVisible(series); 259 if (flag != null) { 260 return flag; 261 } 262 return this.defaultLinesVisible; 263 } 264 265 /** 266 * Returns the flag used to control whether or not the lines for a series 267 * are visible. 268 * 269 * @param series the series index (zero-based). 270 * 271 * @return The flag (possibly {@code null}). 272 * 273 * @see #setSeriesLinesVisible(int, Boolean) 274 */ 275 public Boolean getSeriesLinesVisible(int series) { 276 return this.seriesLinesVisible.getBoolean(series); 277 } 278 279 /** 280 * Sets the 'lines visible' flag for a series and sends a 281 * {@link RendererChangeEvent} to all registered listeners. 282 * 283 * @param series the series index (zero-based). 284 * @param flag the flag ({@code null} permitted). 285 * 286 * @see #getSeriesLinesVisible(int) 287 */ 288 public void setSeriesLinesVisible(int series, Boolean flag) { 289 this.seriesLinesVisible.setBoolean(series, flag); 290 fireChangeEvent(); 291 } 292 293 /** 294 * Sets the 'lines visible' flag for a series and sends a 295 * {@link RendererChangeEvent} to all registered listeners. 296 * 297 * @param series the series index (zero-based). 298 * @param visible the flag. 299 * 300 * @see #getSeriesLinesVisible(int) 301 */ 302 public void setSeriesLinesVisible(int series, boolean visible) { 303 setSeriesLinesVisible(series, Boolean.valueOf(visible)); 304 } 305 306 /** 307 * Returns the default 'lines visible' attribute. 308 * 309 * @return The default flag. 310 * 311 * @see #setDefaultLinesVisible(boolean) 312 */ 313 public boolean getDefaultLinesVisible() { 314 return this.defaultLinesVisible; 315 } 316 317 /** 318 * Sets the default 'lines visible' flag and sends a 319 * {@link RendererChangeEvent} to all registered listeners. 320 * 321 * @param flag the flag. 322 * 323 * @see #getDefaultLinesVisible() 324 */ 325 public void setDefaultLinesVisible(boolean flag) { 326 this.defaultLinesVisible = flag; 327 fireChangeEvent(); 328 } 329 330 /** 331 * Returns the shape used to represent a line in the legend. 332 * 333 * @return The legend line (never {@code null}). 334 * 335 * @see #setLegendLine(Shape) 336 */ 337 public Shape getLegendLine() { 338 return this.legendLine; 339 } 340 341 /** 342 * Sets the shape used as a line in each legend item and sends a 343 * {@link RendererChangeEvent} to all registered listeners. 344 * 345 * @param line the line ({@code null} not permitted). 346 * 347 * @see #getLegendLine() 348 */ 349 public void setLegendLine(Shape line) { 350 Args.nullNotPermitted(line, "line"); 351 this.legendLine = line; 352 fireChangeEvent(); 353 } 354 355 // SHAPES VISIBLE 356 357 /** 358 * Returns the flag used to control whether or not the shape for an item is 359 * visible. 360 * <p> 361 * The default implementation passes control to the 362 * {@code getSeriesShapesVisible()} method. You can override this method 363 * if you require different behaviour. 364 * 365 * @param series the series index (zero-based). 366 * @param item the item index (zero-based). 367 * 368 * @return A boolean. 369 */ 370 public boolean getItemShapeVisible(int series, int item) { 371 Boolean flag = getSeriesShapesVisible(series); 372 if (flag != null) { 373 return flag; 374 } 375 return this.defaultShapesVisible; 376 } 377 378 /** 379 * Returns the flag used to control whether or not the shapes for a series 380 * are visible. 381 * 382 * @param series the series index (zero-based). 383 * 384 * @return A boolean. 385 * 386 * @see #setSeriesShapesVisible(int, Boolean) 387 */ 388 public Boolean getSeriesShapesVisible(int series) { 389 return this.seriesShapesVisible.getBoolean(series); 390 } 391 392 /** 393 * Sets the 'shapes visible' flag for a series and sends a 394 * {@link RendererChangeEvent} to all registered listeners. 395 * 396 * @param series the series index (zero-based). 397 * @param visible the flag. 398 * 399 * @see #getSeriesShapesVisible(int) 400 */ 401 public void setSeriesShapesVisible(int series, boolean visible) { 402 setSeriesShapesVisible(series, Boolean.valueOf(visible)); 403 } 404 405 /** 406 * Sets the 'shapes visible' flag for a series and sends a 407 * {@link RendererChangeEvent} to all registered listeners. 408 * 409 * @param series the series index (zero-based). 410 * @param flag the flag. 411 * 412 * @see #getSeriesShapesVisible(int) 413 */ 414 public void setSeriesShapesVisible(int series, Boolean flag) { 415 this.seriesShapesVisible.setBoolean(series, flag); 416 fireChangeEvent(); 417 } 418 419 /** 420 * Returns the default 'shape visible' attribute. 421 * 422 * @return The default flag. 423 * 424 * @see #setDefaultShapesVisible(boolean) 425 */ 426 public boolean getDefaultShapesVisible() { 427 return this.defaultShapesVisible; 428 } 429 430 /** 431 * Sets the default 'shapes visible' flag and sends a 432 * {@link RendererChangeEvent} to all registered listeners. 433 * 434 * @param flag the flag. 435 * 436 * @see #getDefaultShapesVisible() 437 */ 438 public void setDefaultShapesVisible(boolean flag) { 439 this.defaultShapesVisible = flag; 440 fireChangeEvent(); 441 } 442 443 // SHAPES FILLED 444 445 /** 446 * Returns the flag used to control whether or not the shape for an item 447 * is filled. 448 * <p> 449 * The default implementation passes control to the 450 * {@code getSeriesShapesFilled} method. You can override this method 451 * if you require different behaviour. 452 * 453 * @param series the series index (zero-based). 454 * @param item the item index (zero-based). 455 * 456 * @return A boolean. 457 */ 458 public boolean getItemShapeFilled(int series, int item) { 459 Boolean flag = getSeriesShapesFilled(series); 460 if (flag != null) { 461 return flag; 462 } 463 return this.defaultShapesFilled; 464 465 } 466 467 /** 468 * Returns the flag used to control whether or not the shapes for a series 469 * are filled. 470 * 471 * @param series the series index (zero-based). 472 * 473 * @return A boolean. 474 * 475 * @see #setSeriesShapesFilled(int, Boolean) 476 */ 477 public Boolean getSeriesShapesFilled(int series) { 478 return this.seriesShapesFilled.getBoolean(series); 479 } 480 481 /** 482 * Sets the 'shapes filled' flag for a series and sends a 483 * {@link RendererChangeEvent} to all registered listeners. 484 * 485 * @param series the series index (zero-based). 486 * @param flag the flag. 487 * 488 * @see #getSeriesShapesFilled(int) 489 */ 490 public void setSeriesShapesFilled(int series, boolean flag) { 491 setSeriesShapesFilled(series, Boolean.valueOf(flag)); 492 } 493 494 /** 495 * Sets the 'shapes filled' flag for a series and sends a 496 * {@link RendererChangeEvent} to all registered listeners. 497 * 498 * @param series the series index (zero-based). 499 * @param flag the flag. 500 * 501 * @see #getSeriesShapesFilled(int) 502 */ 503 public void setSeriesShapesFilled(int series, Boolean flag) { 504 this.seriesShapesFilled.setBoolean(series, flag); 505 fireChangeEvent(); 506 } 507 508 /** 509 * Returns the default 'shape filled' attribute. 510 * 511 * @return The default flag. 512 * 513 * @see #setDefaultShapesFilled(boolean) 514 */ 515 public boolean getDefaultShapesFilled() { 516 return this.defaultShapesFilled; 517 } 518 519 /** 520 * Sets the default 'shapes filled' flag and sends a 521 * {@link RendererChangeEvent} to all registered listeners. 522 * 523 * @param flag the flag. 524 * 525 * @see #getDefaultShapesFilled() 526 */ 527 public void setDefaultShapesFilled(boolean flag) { 528 this.defaultShapesFilled = flag; 529 fireChangeEvent(); 530 } 531 532 /** 533 * Returns {@code true} if outlines should be drawn for shapes, and 534 * {@code false} otherwise. 535 * 536 * @return A boolean. 537 * 538 * @see #setDrawOutlines(boolean) 539 */ 540 public boolean getDrawOutlines() { 541 return this.drawOutlines; 542 } 543 544 /** 545 * Sets the flag that controls whether outlines are drawn for 546 * shapes, and sends a {@link RendererChangeEvent} to all registered 547 * listeners. 548 * <P> 549 * In some cases, shapes look better if they do NOT have an outline, but 550 * this flag allows you to set your own preference. 551 * 552 * @param flag the flag. 553 * 554 * @see #getDrawOutlines() 555 */ 556 public void setDrawOutlines(boolean flag) { 557 this.drawOutlines = flag; 558 fireChangeEvent(); 559 } 560 561 /** 562 * Returns {@code true} if the renderer should use the fill paint 563 * setting to fill shapes, and {@code false} if it should just 564 * use the regular paint. 565 * <p> 566 * Refer to {@code XYLineAndShapeRendererDemo2.java} to see the 567 * effect of this flag. 568 * 569 * @return A boolean. 570 * 571 * @see #setUseFillPaint(boolean) 572 * @see #getUseOutlinePaint() 573 */ 574 public boolean getUseFillPaint() { 575 return this.useFillPaint; 576 } 577 578 /** 579 * Sets the flag that controls whether the fill paint is used to fill 580 * shapes, and sends a {@link RendererChangeEvent} to all 581 * registered listeners. 582 * 583 * @param flag the flag. 584 * 585 * @see #getUseFillPaint() 586 */ 587 public void setUseFillPaint(boolean flag) { 588 this.useFillPaint = flag; 589 fireChangeEvent(); 590 } 591 592 /** 593 * Returns {@code true} if the renderer should use the outline paint 594 * setting to draw shape outlines, and {@code false} if it should just 595 * use the regular paint. 596 * 597 * @return A boolean. 598 * 599 * @see #setUseOutlinePaint(boolean) 600 * @see #getUseFillPaint() 601 */ 602 public boolean getUseOutlinePaint() { 603 return this.useOutlinePaint; 604 } 605 606 /** 607 * Sets the flag that controls whether the outline paint is used to draw 608 * shape outlines, and sends a {@link RendererChangeEvent} to all 609 * registered listeners. 610 * <p> 611 * Refer to {@code XYLineAndShapeRendererDemo2.java} to see the 612 * effect of this flag. 613 * 614 * @param flag the flag. 615 * 616 * @see #getUseOutlinePaint() 617 */ 618 public void setUseOutlinePaint(boolean flag) { 619 this.useOutlinePaint = flag; 620 fireChangeEvent(); 621 } 622 623 /** 624 * Records the state for the renderer. This is used to preserve state 625 * information between calls to the drawItem() method for a single chart 626 * drawing. 627 */ 628 public static class State extends XYItemRendererState { 629 630 /** The path for the current series. */ 631 public GeneralPath seriesPath; 632 633 /** 634 * A flag that indicates if the last (x, y) point was 'good' 635 * (non-null). 636 */ 637 private boolean lastPointGood; 638 639 /** 640 * Creates a new state instance. 641 * 642 * @param info the plot rendering info. 643 */ 644 public State(PlotRenderingInfo info) { 645 super(info); 646 this.seriesPath = new GeneralPath(); 647 } 648 649 /** 650 * Returns a flag that indicates if the last point drawn (in the 651 * current series) was 'good' (non-null). 652 * 653 * @return A boolean. 654 */ 655 public boolean isLastPointGood() { 656 return this.lastPointGood; 657 } 658 659 /** 660 * Sets a flag that indicates if the last point drawn (in the current 661 * series) was 'good' (non-null). 662 * 663 * @param good the flag. 664 */ 665 public void setLastPointGood(boolean good) { 666 this.lastPointGood = good; 667 } 668 669 /** 670 * This method is called by the {@link XYPlot} at the start of each 671 * series pass. We reset the state for the current series. 672 * 673 * @param dataset the dataset. 674 * @param series the series index. 675 * @param firstItem the first item index for this pass. 676 * @param lastItem the last item index for this pass. 677 * @param pass the current pass index. 678 * @param passCount the number of passes. 679 */ 680 @Override 681 public void startSeriesPass(XYDataset dataset, int series, 682 int firstItem, int lastItem, int pass, int passCount) { 683 this.seriesPath.reset(); 684 this.lastPointGood = false; 685 super.startSeriesPass(dataset, series, firstItem, lastItem, pass, 686 passCount); 687 } 688 689 } 690 691 /** 692 * Initialises the renderer. 693 * <P> 694 * This method will be called before the first item is rendered, giving the 695 * renderer an opportunity to initialise any state information it wants to 696 * maintain. The renderer can do nothing if it chooses. 697 * 698 * @param g2 the graphics device. 699 * @param dataArea the area inside the axes. 700 * @param plot the plot. 701 * @param data the data. 702 * @param info an optional info collection object to return data back to 703 * the caller. 704 * 705 * @return The renderer state. 706 */ 707 @Override 708 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, 709 XYPlot plot, XYDataset data, PlotRenderingInfo info) { 710 return new State(info); 711 } 712 713 /** 714 * Draws the visual representation of a single data item. 715 * 716 * @param g2 the graphics device. 717 * @param state the renderer state. 718 * @param dataArea the area within which the data is being drawn. 719 * @param info collects information about the drawing. 720 * @param plot the plot (can be used to obtain standard color 721 * information etc). 722 * @param domainAxis the domain axis. 723 * @param rangeAxis the range axis. 724 * @param dataset the dataset. 725 * @param series the series index (zero-based). 726 * @param item the item index (zero-based). 727 * @param crosshairState crosshair information for the plot 728 * ({@code null} permitted). 729 * @param pass the pass index. 730 */ 731 @Override 732 public void drawItem(Graphics2D g2, XYItemRendererState state, 733 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 734 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 735 int series, int item, CrosshairState crosshairState, int pass) { 736 737 // do nothing if item is not visible 738 if (!getItemVisible(series, item)) { 739 return; 740 } 741 742 // first pass draws the background (lines, for instance) 743 if (isLinePass(pass)) { 744 if (getItemLineVisible(series, item)) { 745 if (this.drawSeriesLineAsPath) { 746 drawPrimaryLineAsPath(state, g2, plot, dataset, pass, 747 series, item, domainAxis, rangeAxis, dataArea); 748 } 749 else { 750 drawPrimaryLine(state, g2, plot, dataset, pass, series, 751 item, domainAxis, rangeAxis, dataArea); 752 } 753 } 754 } 755 // second pass adds shapes where the items are .. 756 else if (isItemPass(pass)) { 757 758 // setup for collecting optional entity info... 759 EntityCollection entities = null; 760 if (info != null && info.getOwner() != null) { 761 entities = info.getOwner().getEntityCollection(); 762 } 763 764 drawSecondaryPass(g2, plot, dataset, pass, series, item, 765 domainAxis, dataArea, rangeAxis, crosshairState, entities); 766 } 767 } 768 769 /** 770 * Returns {@code true} if the specified pass is the one for drawing 771 * lines. 772 * 773 * @param pass the pass. 774 * 775 * @return A boolean. 776 */ 777 protected boolean isLinePass(int pass) { 778 return pass == 0; 779 } 780 781 /** 782 * Returns {@code true} if the specified pass is the one for drawing 783 * items. 784 * 785 * @param pass the pass. 786 * 787 * @return A boolean. 788 */ 789 protected boolean isItemPass(int pass) { 790 return pass == 1; 791 } 792 793 /** 794 * Draws the item (first pass). This method draws the lines 795 * connecting the items. 796 * 797 * @param g2 the graphics device. 798 * @param state the renderer state. 799 * @param dataArea the area within which the data is being drawn. 800 * @param plot the plot (can be used to obtain standard color 801 * information etc). 802 * @param domainAxis the domain axis. 803 * @param rangeAxis the range axis. 804 * @param dataset the dataset. 805 * @param pass the pass. 806 * @param series the series index (zero-based). 807 * @param item the item index (zero-based). 808 */ 809 protected void drawPrimaryLine(XYItemRendererState state, 810 Graphics2D g2, 811 XYPlot plot, 812 XYDataset dataset, 813 int pass, 814 int series, 815 int item, 816 ValueAxis domainAxis, 817 ValueAxis rangeAxis, 818 Rectangle2D dataArea) { 819 if (item == 0) { 820 return; 821 } 822 823 // get the data point... 824 double x1 = dataset.getXValue(series, item); 825 double y1 = dataset.getYValue(series, item); 826 if (Double.isNaN(y1) || Double.isNaN(x1)) { 827 return; 828 } 829 830 double x0 = dataset.getXValue(series, item - 1); 831 double y0 = dataset.getYValue(series, item - 1); 832 if (Double.isNaN(y0) || Double.isNaN(x0)) { 833 return; 834 } 835 836 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 837 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 838 839 double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation); 840 double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation); 841 842 double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); 843 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); 844 845 // only draw if we have good values 846 if (Double.isNaN(transX0) || Double.isNaN(transY0) 847 || Double.isNaN(transX1) || Double.isNaN(transY1)) { 848 return; 849 } 850 851 PlotOrientation orientation = plot.getOrientation(); 852 boolean visible; 853 if (orientation == PlotOrientation.HORIZONTAL) { 854 state.workingLine.setLine(transY0, transX0, transY1, transX1); 855 } 856 else if (orientation == PlotOrientation.VERTICAL) { 857 state.workingLine.setLine(transX0, transY0, transX1, transY1); 858 } 859 visible = LineUtils.clipLine(state.workingLine, dataArea); 860 if (visible) { 861 drawFirstPassShape(g2, pass, series, item, state.workingLine); 862 } 863 } 864 865 /** 866 * Draws the first pass shape. 867 * 868 * @param g2 the graphics device. 869 * @param pass the pass. 870 * @param series the series index. 871 * @param item the item index. 872 * @param shape the shape. 873 */ 874 protected void drawFirstPassShape(Graphics2D g2, int pass, int series, 875 int item, Shape shape) { 876 g2.setStroke(getItemStroke(series, item)); 877 g2.setPaint(getItemPaint(series, item)); 878 g2.draw(shape); 879 } 880 881 882 /** 883 * Draws the item (first pass). This method draws the lines 884 * connecting the items. Instead of drawing separate lines, 885 * a {@code GeneralPath} is constructed and drawn at the end of 886 * the series painting. 887 * 888 * @param g2 the graphics device. 889 * @param state the renderer state. 890 * @param plot the plot (can be used to obtain standard color information 891 * etc). 892 * @param dataset the dataset. 893 * @param pass the pass. 894 * @param series the series index (zero-based). 895 * @param item the item index (zero-based). 896 * @param domainAxis the domain axis. 897 * @param rangeAxis the range axis. 898 * @param dataArea the area within which the data is being drawn. 899 */ 900 protected void drawPrimaryLineAsPath(XYItemRendererState state, 901 Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, 902 int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, 903 Rectangle2D dataArea) { 904 905 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 906 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 907 908 // get the data point... 909 double x1 = dataset.getXValue(series, item); 910 double y1 = dataset.getYValue(series, item); 911 double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); 912 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); 913 914 State s = (State) state; 915 // update path to reflect latest point 916 if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) { 917 float x = (float) transX1; 918 float y = (float) transY1; 919 PlotOrientation orientation = plot.getOrientation(); 920 if (orientation == PlotOrientation.HORIZONTAL) { 921 x = (float) transY1; 922 y = (float) transX1; 923 } 924 if (s.isLastPointGood()) { 925 s.seriesPath.lineTo(x, y); 926 } 927 else { 928 s.seriesPath.moveTo(x, y); 929 } 930 s.setLastPointGood(true); 931 } else { 932 s.setLastPointGood(false); 933 } 934 // if this is the last item, draw the path ... 935 if (item == s.getLastItemIndex()) { 936 // draw path 937 drawFirstPassShape(g2, pass, series, item, s.seriesPath); 938 } 939 } 940 941 /** 942 * Draws the item shapes and adds chart entities (second pass). This method 943 * draws the shapes which mark the item positions. If {@code entities} 944 * is not {@code null} it will be populated with entity information 945 * for points that fall within the data area. 946 * 947 * @param g2 the graphics device. 948 * @param plot the plot (can be used to obtain standard color 949 * information etc). 950 * @param domainAxis the domain axis. 951 * @param dataArea the area within which the data is being drawn. 952 * @param rangeAxis the range axis. 953 * @param dataset the dataset. 954 * @param pass the pass. 955 * @param series the series index (zero-based). 956 * @param item the item index (zero-based). 957 * @param crosshairState the crosshair state. 958 * @param entities the entity collection. 959 */ 960 protected void drawSecondaryPass(Graphics2D g2, XYPlot plot, 961 XYDataset dataset, int pass, int series, int item, 962 ValueAxis domainAxis, Rectangle2D dataArea, ValueAxis rangeAxis, 963 CrosshairState crosshairState, EntityCollection entities) { 964 965 Shape entityArea = null; 966 967 // get the data point... 968 double x1 = dataset.getXValue(series, item); 969 double y1 = dataset.getYValue(series, item); 970 if (Double.isNaN(y1) || Double.isNaN(x1)) { 971 return; 972 } 973 974 PlotOrientation orientation = plot.getOrientation(); 975 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 976 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 977 double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); 978 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); 979 980 if (getItemShapeVisible(series, item)) { 981 Shape shape = getItemShape(series, item); 982 if (orientation == PlotOrientation.HORIZONTAL) { 983 shape = ShapeUtils.createTranslatedShape(shape, transY1, 984 transX1); 985 } 986 else if (orientation == PlotOrientation.VERTICAL) { 987 shape = ShapeUtils.createTranslatedShape(shape, transX1, 988 transY1); 989 } 990 entityArea = shape; 991 if (shape.intersects(dataArea)) { 992 if (getItemShapeFilled(series, item)) { 993 if (this.useFillPaint) { 994 g2.setPaint(getItemFillPaint(series, item)); 995 } 996 else { 997 g2.setPaint(getItemPaint(series, item)); 998 } 999 g2.fill(shape); 1000 } 1001 if (this.drawOutlines) { 1002 if (getUseOutlinePaint()) { 1003 g2.setPaint(getItemOutlinePaint(series, item)); 1004 } 1005 else { 1006 g2.setPaint(getItemPaint(series, item)); 1007 } 1008 g2.setStroke(getItemOutlineStroke(series, item)); 1009 g2.draw(shape); 1010 } 1011 } 1012 } 1013 1014 double xx = transX1; 1015 double yy = transY1; 1016 if (orientation == PlotOrientation.HORIZONTAL) { 1017 xx = transY1; 1018 yy = transX1; 1019 } 1020 1021 // draw the item label if there is one... 1022 if (isItemLabelVisible(series, item)) { 1023 drawItemLabel(g2, orientation, dataset, series, item, xx, yy, 1024 (y1 < 0.0)); 1025 } 1026 1027 int datasetIndex = plot.indexOf(dataset); 1028 updateCrosshairValues(crosshairState, x1, y1, datasetIndex, 1029 transX1, transY1, orientation); 1030 1031 // add an entity for the item, but only if it falls within the data 1032 // area... 1033 if (entities != null && ShapeUtils.isPointInRect(dataArea, xx, yy)) { 1034 addEntity(entities, entityArea, dataset, series, item, xx, yy); 1035 } 1036 } 1037 1038 1039 /** 1040 * Returns a legend item for the specified series. 1041 * 1042 * @param datasetIndex the dataset index (zero-based). 1043 * @param series the series index (zero-based). 1044 * 1045 * @return A legend item for the series (possibly {@code null}). 1046 */ 1047 @Override 1048 public LegendItem getLegendItem(int datasetIndex, int series) { 1049 XYPlot plot = getPlot(); 1050 if (plot == null) { 1051 return null; 1052 } 1053 1054 XYDataset dataset = plot.getDataset(datasetIndex); 1055 if (dataset == null) { 1056 return null; 1057 } 1058 1059 if (!getItemVisible(series, 0)) { 1060 return null; 1061 } 1062 String label = getLegendItemLabelGenerator().generateLabel(dataset, 1063 series); 1064 String description = label; 1065 String toolTipText = null; 1066 if (getLegendItemToolTipGenerator() != null) { 1067 toolTipText = getLegendItemToolTipGenerator().generateLabel( 1068 dataset, series); 1069 } 1070 String urlText = null; 1071 if (getLegendItemURLGenerator() != null) { 1072 urlText = getLegendItemURLGenerator().generateLabel(dataset, 1073 series); 1074 } 1075 boolean shapeIsVisible = getItemShapeVisible(series, 0); 1076 Shape shape = lookupLegendShape(series); 1077 boolean shapeIsFilled = getItemShapeFilled(series, 0); 1078 Paint fillPaint = (this.useFillPaint ? lookupSeriesFillPaint(series) 1079 : lookupSeriesPaint(series)); 1080 boolean shapeOutlineVisible = this.drawOutlines; 1081 Paint outlinePaint = (this.useOutlinePaint ? lookupSeriesOutlinePaint( 1082 series) : lookupSeriesPaint(series)); 1083 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 1084 boolean lineVisible = getItemLineVisible(series, 0); 1085 Stroke lineStroke = lookupSeriesStroke(series); 1086 Paint linePaint = lookupSeriesPaint(series); 1087 LegendItem result = new LegendItem(label, description, toolTipText, 1088 urlText, shapeIsVisible, shape, shapeIsFilled, fillPaint, 1089 shapeOutlineVisible, outlinePaint, outlineStroke, lineVisible, 1090 this.legendLine, lineStroke, linePaint); 1091 result.setLabelFont(lookupLegendTextFont(series)); 1092 Paint labelPaint = lookupLegendTextPaint(series); 1093 if (labelPaint != null) { 1094 result.setLabelPaint(labelPaint); 1095 } 1096 result.setSeriesKey(dataset.getSeriesKey(series)); 1097 result.setSeriesIndex(series); 1098 result.setDataset(dataset); 1099 result.setDatasetIndex(datasetIndex); 1100 1101 return result; 1102 } 1103 1104 /** 1105 * Returns a clone of the renderer. 1106 * 1107 * @return A clone. 1108 * 1109 * @throws CloneNotSupportedException if the clone cannot be created. 1110 */ 1111 @Override 1112 public Object clone() throws CloneNotSupportedException { 1113 XYLineAndShapeRenderer clone = (XYLineAndShapeRenderer) super.clone(); 1114 clone.seriesLinesVisible 1115 = (BooleanList) this.seriesLinesVisible.clone(); 1116 if (this.legendLine != null) { 1117 clone.legendLine = ShapeUtils.clone(this.legendLine); 1118 } 1119 clone.seriesShapesVisible 1120 = (BooleanList) this.seriesShapesVisible.clone(); 1121 clone.seriesShapesFilled 1122 = (BooleanList) this.seriesShapesFilled.clone(); 1123 return clone; 1124 } 1125 1126 /** 1127 * Tests this renderer for equality with an arbitrary object. 1128 * 1129 * @param obj the object ({@code null} permitted). 1130 * 1131 * @return {@code true} or {@code false}. 1132 */ 1133 @Override 1134 public boolean equals(Object obj) { 1135 if (obj == this) { 1136 return true; 1137 } 1138 if (!(obj instanceof XYLineAndShapeRenderer)) { 1139 return false; 1140 } 1141 if (!super.equals(obj)) { 1142 return false; 1143 } 1144 XYLineAndShapeRenderer that = (XYLineAndShapeRenderer) obj; 1145 if (!ObjectUtils.equal( 1146 this.seriesLinesVisible, that.seriesLinesVisible) 1147 ) { 1148 return false; 1149 } 1150 if (this.defaultLinesVisible != that.defaultLinesVisible) { 1151 return false; 1152 } 1153 if (!ShapeUtils.equal(this.legendLine, that.legendLine)) { 1154 return false; 1155 } 1156 if (!ObjectUtils.equal( 1157 this.seriesShapesVisible, that.seriesShapesVisible) 1158 ) { 1159 return false; 1160 } 1161 if (this.defaultShapesVisible != that.defaultShapesVisible) { 1162 return false; 1163 } 1164 if (!ObjectUtils.equal( 1165 this.seriesShapesFilled, that.seriesShapesFilled) 1166 ) { 1167 return false; 1168 } 1169 if (this.defaultShapesFilled != that.defaultShapesFilled) { 1170 return false; 1171 } 1172 if (this.drawOutlines != that.drawOutlines) { 1173 return false; 1174 } 1175 if (this.useOutlinePaint != that.useOutlinePaint) { 1176 return false; 1177 } 1178 if (this.useFillPaint != that.useFillPaint) { 1179 return false; 1180 } 1181 if (this.drawSeriesLineAsPath != that.drawSeriesLineAsPath) { 1182 return false; 1183 } 1184 return true; 1185 } 1186 1187 /** 1188 * Provides serialization support. 1189 * 1190 * @param stream the input stream. 1191 * 1192 * @throws IOException if there is an I/O error. 1193 * @throws ClassNotFoundException if there is a classpath problem. 1194 */ 1195 private void readObject(ObjectInputStream stream) 1196 throws IOException, ClassNotFoundException { 1197 stream.defaultReadObject(); 1198 this.legendLine = SerialUtils.readShape(stream); 1199 } 1200 1201 /** 1202 * Provides serialization support. 1203 * 1204 * @param stream the output stream. 1205 * 1206 * @throws IOException if there is an I/O error. 1207 */ 1208 private void writeObject(ObjectOutputStream stream) throws IOException { 1209 stream.defaultWriteObject(); 1210 SerialUtils.writeShape(this.legendLine, stream); 1211 } 1212 1213}