001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * StandardXYItemRenderer.java 029 * --------------------------- 030 * (C) Copyright 2001-2017, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Mark Watson (www.markwatson.com); 034 * Jonathan Nash; 035 * Andreas Schneider; 036 * Norbert Kiesel (for TBD Networks); 037 * Christian W. Zuckschwerdt; 038 * Bill Kelemen; 039 * Nicolas Brodu (for Astrium and EADS Corporate Research 040 * Center); 041 * 042 * Changes: 043 * -------- 044 * 19-Oct-2001 : Version 1, based on code by Mark Watson (DG); 045 * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG); 046 * 21-Dec-2001 : Added working line instance to improve performance (DG); 047 * 22-Jan-2002 : Added code to lock crosshairs to data points. Based on code 048 * by Jonathan Nash (DG); 049 * 23-Jan-2002 : Added DrawInfo parameter to drawItem() method (DG); 050 * 28-Mar-2002 : Added a property change listener mechanism so that the 051 * renderer no longer needs to be immutable (DG); 052 * 02-Apr-2002 : Modified to handle null values (DG); 053 * 09-Apr-2002 : Modified draw method to return void. Removed the translated 054 * zero from the drawItem method. Override the initialise() 055 * method to calculate it (DG); 056 * 13-May-2002 : Added code from Andreas Schneider to allow changing 057 * shapes/colors per item (DG); 058 * 24-May-2002 : Incorporated tooltips into chart entities (DG); 059 * 25-Jun-2002 : Removed redundant code (DG); 060 * 05-Aug-2002 : Incorporated URLs for HTML image maps into chart entities (RA); 061 * 08-Aug-2002 : Added discontinuous lines option contributed by 062 * Norbert Kiesel (DG); 063 * 20-Aug-2002 : Added user definable default values to be returned by 064 * protected methods unless overridden by a subclass (DG); 065 * 23-Sep-2002 : Updated for changes in the XYItemRenderer interface (DG); 066 * 02-Oct-2002 : Fixed errors reported by Checkstyle (DG); 067 * 25-Mar-2003 : Implemented Serializable (DG); 068 * 01-May-2003 : Modified drawItem() method signature (DG); 069 * 15-May-2003 : Modified to take into account the plot orientation (DG); 070 * 29-Jul-2003 : Amended code that doesn't compile with JDK 1.2.2 (DG); 071 * 30-Jul-2003 : Modified entity constructor (CZ); 072 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG); 073 * 24-Aug-2003 : Added null/NaN checks in drawItem (BK); 074 * 08-Sep-2003 : Fixed serialization (NB); 075 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 076 * 21-Jan-2004 : Override for getLegendItem() method (DG); 077 * 27-Jan-2004 : Moved working line into state object (DG); 078 * 10-Feb-2004 : Changed drawItem() method to make cut-and-paste overriding 079 * easier (DG); 080 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed 081 * XYToolTipGenerator --> XYItemLabelGenerator (DG); 082 * 08-Jun-2004 : Modified to use getX() and getY() methods (DG); 083 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 084 * getYValue() (DG); 085 * 25-Aug-2004 : Created addEntity() method in superclass (DG); 086 * 08-Oct-2004 : Added 'gapThresholdType' as suggested by Mike Watts (DG); 087 * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG); 088 * 23-Feb-2005 : Fixed getLegendItem() method to show lines. Fixed bug 089 * 1077108 (shape not visible for first item in series) (DG); 090 * 10-Apr-2005 : Fixed item label positioning with horizontal orientation (DG); 091 * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG); 092 * 27-Apr-2005 : Use generator for series label in legend (DG); 093 * ------------- JFREECHART 1.0.x --------------------------------------------- 094 * 15-Jun-2006 : Fixed bug (1380480) for rendering series as path (DG); 095 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG); 096 * 14-Mar-2007 : Fixed problems with the equals() and clone() methods (DG); 097 * 23-Mar-2007 : Clean-up of shapesFilled attributes (DG); 098 * 20-Apr-2007 : Updated getLegendItem() and drawItem() for renderer 099 * change (DG); 100 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() 101 * method (DG); 102 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 103 * 08-Jun-2007 : Fixed bug in entity creation (DG); 104 * 21-Nov-2007 : Deprecated override flag methods (DG); 105 * 02-Jun-2008 : Fixed tooltips for data items at lower edges of data area (DG); 106 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 107 * 03-Jul-2013 : Use ParamChecks (DG); 108 * 18-Feb-2017 : Updates for crosshairs (bug #36) (DG); 109 * 110 */ 111 112package org.jfree.chart.renderer.xy; 113 114import java.awt.Graphics2D; 115import java.awt.Image; 116import java.awt.Paint; 117import java.awt.Point; 118import java.awt.Shape; 119import java.awt.Stroke; 120import java.awt.geom.GeneralPath; 121import java.awt.geom.Line2D; 122import java.awt.geom.Rectangle2D; 123import java.io.IOException; 124import java.io.ObjectInputStream; 125import java.io.ObjectOutputStream; 126import java.io.Serializable; 127 128import org.jfree.chart.LegendItem; 129import org.jfree.chart.axis.ValueAxis; 130import org.jfree.chart.entity.EntityCollection; 131import org.jfree.chart.event.RendererChangeEvent; 132import org.jfree.chart.labels.XYToolTipGenerator; 133import org.jfree.chart.plot.CrosshairState; 134import org.jfree.chart.plot.Plot; 135import org.jfree.chart.plot.PlotOrientation; 136import org.jfree.chart.plot.PlotRenderingInfo; 137import org.jfree.chart.plot.XYPlot; 138import org.jfree.chart.ui.RectangleEdge; 139import org.jfree.chart.urls.XYURLGenerator; 140import org.jfree.chart.util.BooleanList; 141import org.jfree.chart.util.Args; 142import org.jfree.chart.util.PublicCloneable; 143import org.jfree.chart.util.SerialUtils; 144import org.jfree.chart.util.ShapeUtils; 145import org.jfree.chart.util.UnitType; 146import org.jfree.data.xy.XYDataset; 147 148/** 149 * Standard item renderer for an {@link XYPlot}. This class can draw (a) 150 * shapes at each point, or (b) lines between points, or (c) both shapes and 151 * lines. 152 * <P> 153 * This renderer has been retained for historical reasons and, in general, you 154 * should use the {@link XYLineAndShapeRenderer} class instead. 155 */ 156public class StandardXYItemRenderer extends AbstractXYItemRenderer 157 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable { 158 159 /** For serialization. */ 160 private static final long serialVersionUID = -3271351259436865995L; 161 162 /** Constant for the type of rendering (shapes only). */ 163 public static final int SHAPES = 1; 164 165 /** Constant for the type of rendering (lines only). */ 166 public static final int LINES = 2; 167 168 /** Constant for the type of rendering (shapes and lines). */ 169 public static final int SHAPES_AND_LINES = SHAPES | LINES; 170 171 /** Constant for the type of rendering (images only). */ 172 public static final int IMAGES = 4; 173 174 /** Constant for the type of rendering (discontinuous lines). */ 175 public static final int DISCONTINUOUS = 8; 176 177 /** Constant for the type of rendering (discontinuous lines). */ 178 public static final int DISCONTINUOUS_LINES = LINES | DISCONTINUOUS; 179 180 /** A flag indicating whether or not shapes are drawn at each XY point. */ 181 private boolean baseShapesVisible; 182 183 /** A flag indicating whether or not lines are drawn between XY points. */ 184 private boolean plotLines; 185 186 /** A flag indicating whether or not images are drawn between XY points. */ 187 private boolean plotImages; 188 189 /** A flag controlling whether or not discontinuous lines are used. */ 190 private boolean plotDiscontinuous; 191 192 /** Specifies how the gap threshold value is interpreted. */ 193 private UnitType gapThresholdType = UnitType.RELATIVE; 194 195 /** Threshold for deciding when to discontinue a line. */ 196 private double gapThreshold = 1.0; 197 198 /** 199 * A table of flags that control (per series) whether or not shapes are 200 * filled. 201 */ 202 private BooleanList seriesShapesFilled; 203 204 /** The default value returned by the getShapeFilled() method. */ 205 private boolean baseShapesFilled; 206 207 /** 208 * A flag that controls whether or not each series is drawn as a single 209 * path. 210 */ 211 private boolean drawSeriesLineAsPath; 212 213 /** 214 * The shape that is used to represent a line in the legend. 215 * This should never be set to {@code null}. 216 */ 217 private transient Shape legendLine; 218 219 /** 220 * Constructs a new renderer. 221 */ 222 public StandardXYItemRenderer() { 223 this(LINES, null); 224 } 225 226 /** 227 * Constructs a new renderer. To specify the type of renderer, use one of 228 * the constants: {@link #SHAPES}, {@link #LINES} or 229 * {@link #SHAPES_AND_LINES}. 230 * 231 * @param type the type. 232 */ 233 public StandardXYItemRenderer(int type) { 234 this(type, null); 235 } 236 237 /** 238 * Constructs a new renderer. To specify the type of renderer, use one of 239 * the constants: {@link #SHAPES}, {@link #LINES} or 240 * {@link #SHAPES_AND_LINES}. 241 * 242 * @param type the type of renderer. 243 * @param toolTipGenerator the item label generator ({@code null} 244 * permitted). 245 */ 246 public StandardXYItemRenderer(int type, 247 XYToolTipGenerator toolTipGenerator) { 248 this(type, toolTipGenerator, null); 249 } 250 251 /** 252 * Constructs a new renderer. To specify the type of renderer, use one of 253 * the constants: {@link #SHAPES}, {@link #LINES} or 254 * {@link #SHAPES_AND_LINES}. 255 * 256 * @param type the type of renderer. 257 * @param toolTipGenerator the item label generator ({@code null} 258 * permitted). 259 * @param urlGenerator the URL generator. 260 */ 261 public StandardXYItemRenderer(int type, XYToolTipGenerator toolTipGenerator, 262 XYURLGenerator urlGenerator) { 263 264 super(); 265 setDefaultToolTipGenerator(toolTipGenerator); 266 setURLGenerator(urlGenerator); 267 if ((type & SHAPES) != 0) { 268 this.baseShapesVisible = true; 269 } 270 if ((type & LINES) != 0) { 271 this.plotLines = true; 272 } 273 if ((type & IMAGES) != 0) { 274 this.plotImages = true; 275 } 276 if ((type & DISCONTINUOUS) != 0) { 277 this.plotDiscontinuous = true; 278 } 279 280 this.seriesShapesFilled = new BooleanList(); 281 this.baseShapesFilled = true; 282 this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0); 283 this.drawSeriesLineAsPath = false; 284 } 285 286 /** 287 * Returns true if shapes are being plotted by the renderer. 288 * 289 * @return {@code true} if shapes are being plotted by the renderer. 290 * 291 * @see #setBaseShapesVisible 292 */ 293 public boolean getBaseShapesVisible() { 294 return this.baseShapesVisible; 295 } 296 297 /** 298 * Sets the flag that controls whether or not a shape is plotted at each 299 * data point. 300 * 301 * @param flag the flag. 302 * 303 * @see #getBaseShapesVisible 304 */ 305 public void setBaseShapesVisible(boolean flag) { 306 if (this.baseShapesVisible != flag) { 307 this.baseShapesVisible = flag; 308 fireChangeEvent(); 309 } 310 } 311 312 // SHAPES FILLED 313 314 /** 315 * Returns the flag used to control whether or not the shape for an item is 316 * filled. 317 * <p> 318 * The default implementation passes control to the 319 * {@code getSeriesShapesFilled()} method. You can override this method 320 * if you require different behaviour. 321 * 322 * @param series the series index (zero-based). 323 * @param item the item index (zero-based). 324 * 325 * @return A boolean. 326 * 327 * @see #getSeriesShapesFilled(int) 328 */ 329 public boolean getItemShapeFilled(int series, int item) { 330 331 // otherwise look up the paint table 332 Boolean flag = this.seriesShapesFilled.getBoolean(series); 333 if (flag != null) { 334 return flag; 335 } 336 else { 337 return this.baseShapesFilled; 338 } 339 } 340 341 /** 342 * Returns the flag used to control whether or not the shapes for a series 343 * are filled. 344 * 345 * @param series the series index (zero-based). 346 * 347 * @return A boolean. 348 */ 349 public Boolean getSeriesShapesFilled(int series) { 350 return this.seriesShapesFilled.getBoolean(series); 351 } 352 353 /** 354 * Sets the 'shapes filled' flag for a series and sends a 355 * {@link RendererChangeEvent} to all registered listeners. 356 * 357 * @param series the series index (zero-based). 358 * @param flag the flag. 359 * 360 * @see #getSeriesShapesFilled(int) 361 */ 362 public void setSeriesShapesFilled(int series, Boolean flag) { 363 this.seriesShapesFilled.setBoolean(series, flag); 364 fireChangeEvent(); 365 } 366 367 /** 368 * Returns the base 'shape filled' attribute. 369 * 370 * @return The base flag. 371 * 372 * @see #setBaseShapesFilled(boolean) 373 */ 374 public boolean getBaseShapesFilled() { 375 return this.baseShapesFilled; 376 } 377 378 /** 379 * Sets the base 'shapes filled' flag and sends a 380 * {@link RendererChangeEvent} to all registered listeners. 381 * 382 * @param flag the flag. 383 * 384 * @see #getBaseShapesFilled() 385 */ 386 public void setBaseShapesFilled(boolean flag) { 387 this.baseShapesFilled = flag; 388 } 389 390 /** 391 * Returns true if lines are being plotted by the renderer. 392 * 393 * @return {@code true} if lines are being plotted by the renderer. 394 * 395 * @see #setPlotLines(boolean) 396 */ 397 public boolean getPlotLines() { 398 return this.plotLines; 399 } 400 401 /** 402 * Sets the flag that controls whether or not a line is plotted between 403 * each data point and sends a {@link RendererChangeEvent} to all 404 * registered listeners. 405 * 406 * @param flag the flag. 407 * 408 * @see #getPlotLines() 409 */ 410 public void setPlotLines(boolean flag) { 411 if (this.plotLines != flag) { 412 this.plotLines = flag; 413 fireChangeEvent(); 414 } 415 } 416 417 /** 418 * Returns the gap threshold type (relative or absolute). 419 * 420 * @return The type. 421 * 422 * @see #setGapThresholdType(UnitType) 423 */ 424 public UnitType getGapThresholdType() { 425 return this.gapThresholdType; 426 } 427 428 /** 429 * Sets the gap threshold type and sends a {@link RendererChangeEvent} to 430 * all registered listeners. 431 * 432 * @param thresholdType the type ({@code null} not permitted). 433 * 434 * @see #getGapThresholdType() 435 */ 436 public void setGapThresholdType(UnitType thresholdType) { 437 Args.nullNotPermitted(thresholdType, "thresholdType"); 438 this.gapThresholdType = thresholdType; 439 fireChangeEvent(); 440 } 441 442 /** 443 * Returns the gap threshold for discontinuous lines. 444 * 445 * @return The gap threshold. 446 * 447 * @see #setGapThreshold(double) 448 */ 449 public double getGapThreshold() { 450 return this.gapThreshold; 451 } 452 453 /** 454 * Sets the gap threshold for discontinuous lines and sends a 455 * {@link RendererChangeEvent} to all registered listeners. 456 * 457 * @param t the threshold. 458 * 459 * @see #getGapThreshold() 460 */ 461 public void setGapThreshold(double t) { 462 this.gapThreshold = t; 463 fireChangeEvent(); 464 } 465 466 /** 467 * Returns true if images are being plotted by the renderer. 468 * 469 * @return {@code true} if images are being plotted by the renderer. 470 * 471 * @see #setPlotImages(boolean) 472 */ 473 public boolean getPlotImages() { 474 return this.plotImages; 475 } 476 477 /** 478 * Sets the flag that controls whether or not an image is drawn at each 479 * data point and sends a {@link RendererChangeEvent} to all registered 480 * listeners. 481 * 482 * @param flag the flag. 483 * 484 * @see #getPlotImages() 485 */ 486 public void setPlotImages(boolean flag) { 487 if (this.plotImages != flag) { 488 this.plotImages = flag; 489 fireChangeEvent(); 490 } 491 } 492 493 /** 494 * Returns a flag that controls whether or not the renderer shows 495 * discontinuous lines. 496 * 497 * @return {@code true} if lines should be discontinuous. 498 */ 499 public boolean getPlotDiscontinuous() { 500 return this.plotDiscontinuous; 501 } 502 503 /** 504 * Sets the flag that controls whether or not the renderer shows 505 * discontinuous lines, and sends a {@link RendererChangeEvent} to all 506 * registered listeners. 507 * 508 * @param flag the new flag value. 509 * 510 * @since 1.0.5 511 */ 512 public void setPlotDiscontinuous(boolean flag) { 513 if (this.plotDiscontinuous != flag) { 514 this.plotDiscontinuous = flag; 515 fireChangeEvent(); 516 } 517 } 518 519 /** 520 * Returns a flag that controls whether or not each series is drawn as a 521 * single path. 522 * 523 * @return A boolean. 524 * 525 * @see #setDrawSeriesLineAsPath(boolean) 526 */ 527 public boolean getDrawSeriesLineAsPath() { 528 return this.drawSeriesLineAsPath; 529 } 530 531 /** 532 * Sets the flag that controls whether or not each series is drawn as a 533 * single path. 534 * 535 * @param flag the flag. 536 * 537 * @see #getDrawSeriesLineAsPath() 538 */ 539 public void setDrawSeriesLineAsPath(boolean flag) { 540 this.drawSeriesLineAsPath = flag; 541 } 542 543 /** 544 * Returns the shape used to represent a line in the legend. 545 * 546 * @return The legend line (never {@code null}). 547 * 548 * @see #setLegendLine(Shape) 549 */ 550 public Shape getLegendLine() { 551 return this.legendLine; 552 } 553 554 /** 555 * Sets the shape used as a line in each legend item and sends a 556 * {@link RendererChangeEvent} to all registered listeners. 557 * 558 * @param line the line ({@code null} not permitted). 559 * 560 * @see #getLegendLine() 561 */ 562 public void setLegendLine(Shape line) { 563 Args.nullNotPermitted(line, "line"); 564 this.legendLine = line; 565 fireChangeEvent(); 566 } 567 568 /** 569 * Returns a legend item for a series. 570 * 571 * @param datasetIndex the dataset index (zero-based). 572 * @param series the series index (zero-based). 573 * 574 * @return A legend item for the series. 575 */ 576 @Override 577 public LegendItem getLegendItem(int datasetIndex, int series) { 578 XYPlot plot = getPlot(); 579 if (plot == null) { 580 return null; 581 } 582 LegendItem result = null; 583 XYDataset dataset = plot.getDataset(datasetIndex); 584 if (dataset != null) { 585 if (getItemVisible(series, 0)) { 586 String label = getLegendItemLabelGenerator().generateLabel( 587 dataset, series); 588 String description = label; 589 String toolTipText = null; 590 if (getLegendItemToolTipGenerator() != null) { 591 toolTipText = getLegendItemToolTipGenerator().generateLabel( 592 dataset, series); 593 } 594 String urlText = null; 595 if (getLegendItemURLGenerator() != null) { 596 urlText = getLegendItemURLGenerator().generateLabel( 597 dataset, series); 598 } 599 Shape shape = lookupLegendShape(series); 600 boolean shapeFilled = getItemShapeFilled(series, 0); 601 Paint paint = lookupSeriesPaint(series); 602 Paint linePaint = paint; 603 Stroke lineStroke = lookupSeriesStroke(series); 604 result = new LegendItem(label, description, toolTipText, 605 urlText, this.baseShapesVisible, shape, shapeFilled, 606 paint, !shapeFilled, paint, lineStroke, 607 this.plotLines, this.legendLine, lineStroke, linePaint); 608 result.setLabelFont(lookupLegendTextFont(series)); 609 Paint labelPaint = lookupLegendTextPaint(series); 610 if (labelPaint != null) { 611 result.setLabelPaint(labelPaint); 612 } 613 result.setDataset(dataset); 614 result.setDatasetIndex(datasetIndex); 615 result.setSeriesKey(dataset.getSeriesKey(series)); 616 result.setSeriesIndex(series); 617 } 618 } 619 return result; 620 } 621 622 /** 623 * Records the state for the renderer. This is used to preserve state 624 * information between calls to the drawItem() method for a single chart 625 * drawing. 626 */ 627 public static class State extends XYItemRendererState { 628 629 /** The path for the current series. */ 630 public GeneralPath seriesPath; 631 632 /** The series index. */ 633 private int seriesIndex; 634 635 /** 636 * A flag that indicates if the last (x, y) point was 'good' 637 * (non-null). 638 */ 639 private boolean lastPointGood; 640 641 /** 642 * Creates a new state instance. 643 * 644 * @param info the plot rendering info. 645 */ 646 public State(PlotRenderingInfo info) { 647 super(info); 648 } 649 650 /** 651 * Returns a flag that indicates if the last point drawn (in the 652 * current series) was 'good' (non-null). 653 * 654 * @return A boolean. 655 */ 656 public boolean isLastPointGood() { 657 return this.lastPointGood; 658 } 659 660 /** 661 * Sets a flag that indicates if the last point drawn (in the current 662 * series) was 'good' (non-null). 663 * 664 * @param good the flag. 665 */ 666 public void setLastPointGood(boolean good) { 667 this.lastPointGood = good; 668 } 669 670 /** 671 * Returns the series index for the current path. 672 * 673 * @return The series index for the current path. 674 */ 675 public int getSeriesIndex() { 676 return this.seriesIndex; 677 } 678 679 /** 680 * Sets the series index for the current path. 681 * 682 * @param index the index. 683 */ 684 public void setSeriesIndex(int index) { 685 this.seriesIndex = index; 686 } 687 } 688 689 /** 690 * Initialises the renderer. 691 * <P> 692 * This method will be called before the first item is rendered, giving the 693 * renderer an opportunity to initialise any state information it wants to 694 * maintain. The renderer can do nothing if it chooses. 695 * 696 * @param g2 the graphics device. 697 * @param dataArea the area inside the axes. 698 * @param plot the plot. 699 * @param data the data. 700 * @param info an optional info collection object to return data back to 701 * the caller. 702 * 703 * @return The renderer state. 704 */ 705 @Override 706 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, 707 XYPlot plot, XYDataset data, PlotRenderingInfo info) { 708 709 State state = new State(info); 710 state.seriesPath = new GeneralPath(); 711 state.seriesIndex = -1; 712 return state; 713 714 } 715 716 /** 717 * Draws the visual representation of a single data item. 718 * 719 * @param g2 the graphics device. 720 * @param state the renderer state. 721 * @param dataArea the area within which the data is being drawn. 722 * @param info collects information about the drawing. 723 * @param plot the plot (can be used to obtain standard color information 724 * etc). 725 * @param domainAxis the domain axis. 726 * @param rangeAxis the range axis. 727 * @param dataset the dataset. 728 * @param series the series index (zero-based). 729 * @param item the item index (zero-based). 730 * @param crosshairState crosshair information for the plot 731 * ({@code null} permitted). 732 * @param pass the pass index. 733 */ 734 @Override 735 public void drawItem(Graphics2D g2, XYItemRendererState state, 736 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 737 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 738 int series, int item, CrosshairState crosshairState, int pass) { 739 740 boolean itemVisible = getItemVisible(series, item); 741 742 // setup for collecting optional entity info... 743 Shape entityArea = null; 744 EntityCollection entities = null; 745 if (info != null) { 746 entities = info.getOwner().getEntityCollection(); 747 } 748 749 PlotOrientation orientation = plot.getOrientation(); 750 Paint paint = getItemPaint(series, item); 751 Stroke seriesStroke = getItemStroke(series, item); 752 g2.setPaint(paint); 753 g2.setStroke(seriesStroke); 754 755 // get the data point... 756 double x1 = dataset.getXValue(series, item); 757 double y1 = dataset.getYValue(series, item); 758 if (Double.isNaN(x1) || Double.isNaN(y1)) { 759 itemVisible = false; 760 } 761 762 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 763 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 764 double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); 765 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); 766 767 if (getPlotLines()) { 768 if (this.drawSeriesLineAsPath) { 769 State s = (State) state; 770 if (s.getSeriesIndex() != series) { 771 // we are starting a new series path 772 s.seriesPath.reset(); 773 s.lastPointGood = false; 774 s.setSeriesIndex(series); 775 } 776 777 // update path to reflect latest point 778 if (itemVisible && !Double.isNaN(transX1) 779 && !Double.isNaN(transY1)) { 780 float x = (float) transX1; 781 float y = (float) transY1; 782 if (orientation == PlotOrientation.HORIZONTAL) { 783 x = (float) transY1; 784 y = (float) transX1; 785 } 786 if (s.isLastPointGood()) { 787 // TODO: check threshold 788 s.seriesPath.lineTo(x, y); 789 } 790 else { 791 s.seriesPath.moveTo(x, y); 792 } 793 s.setLastPointGood(true); 794 } 795 else { 796 s.setLastPointGood(false); 797 } 798 if (item == dataset.getItemCount(series) - 1) { 799 if (s.seriesIndex == series) { 800 // draw path 801 g2.setStroke(lookupSeriesStroke(series)); 802 g2.setPaint(lookupSeriesPaint(series)); 803 g2.draw(s.seriesPath); 804 } 805 } 806 } 807 808 else if (item != 0 && itemVisible) { 809 // get the previous data point... 810 double x0 = dataset.getXValue(series, item - 1); 811 double y0 = dataset.getYValue(series, item - 1); 812 if (!Double.isNaN(x0) && !Double.isNaN(y0)) { 813 boolean drawLine = true; 814 if (getPlotDiscontinuous()) { 815 // only draw a line if the gap between the current and 816 // previous data point is within the threshold 817 int numX = dataset.getItemCount(series); 818 double minX = dataset.getXValue(series, 0); 819 double maxX = dataset.getXValue(series, numX - 1); 820 if (this.gapThresholdType == UnitType.ABSOLUTE) { 821 drawLine = Math.abs(x1 - x0) <= this.gapThreshold; 822 } 823 else { 824 drawLine = Math.abs(x1 - x0) <= ((maxX - minX) 825 / numX * getGapThreshold()); 826 } 827 } 828 if (drawLine) { 829 double transX0 = domainAxis.valueToJava2D(x0, dataArea, 830 xAxisLocation); 831 double transY0 = rangeAxis.valueToJava2D(y0, dataArea, 832 yAxisLocation); 833 834 // only draw if we have good values 835 if (Double.isNaN(transX0) || Double.isNaN(transY0) 836 || Double.isNaN(transX1) || Double.isNaN(transY1)) { 837 return; 838 } 839 840 if (orientation == PlotOrientation.HORIZONTAL) { 841 state.workingLine.setLine(transY0, transX0, 842 transY1, transX1); 843 } 844 else if (orientation == PlotOrientation.VERTICAL) { 845 state.workingLine.setLine(transX0, transY0, 846 transX1, transY1); 847 } 848 849 if (state.workingLine.intersects(dataArea)) { 850 g2.draw(state.workingLine); 851 } 852 } 853 } 854 } 855 } 856 857 // we needed to get this far even for invisible items, to ensure that 858 // seriesPath updates happened, but now there is nothing more we need 859 // to do for non-visible items... 860 if (!itemVisible) { 861 return; 862 } 863 864 if (getBaseShapesVisible()) { 865 866 Shape shape = getItemShape(series, item); 867 if (orientation == PlotOrientation.HORIZONTAL) { 868 shape = ShapeUtils.createTranslatedShape(shape, transY1, 869 transX1); 870 } 871 else if (orientation == PlotOrientation.VERTICAL) { 872 shape = ShapeUtils.createTranslatedShape(shape, transX1, 873 transY1); 874 } 875 if (shape.intersects(dataArea)) { 876 if (getItemShapeFilled(series, item)) { 877 g2.fill(shape); 878 } 879 else { 880 g2.draw(shape); 881 } 882 } 883 entityArea = shape; 884 885 } 886 887 if (getPlotImages()) { 888 Image image = getImage(plot, series, item, transX1, transY1); 889 if (image != null) { 890 Point hotspot = getImageHotspot(plot, series, item, transX1, 891 transY1, image); 892 g2.drawImage(image, (int) (transX1 - hotspot.getX()), 893 (int) (transY1 - hotspot.getY()), null); 894 entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), 895 transY1 - hotspot.getY(), image.getWidth(null), 896 image.getHeight(null)); 897 } 898 899 } 900 901 double xx = transX1; 902 double yy = transY1; 903 if (orientation == PlotOrientation.HORIZONTAL) { 904 xx = transY1; 905 yy = transX1; 906 } 907 908 // draw the item label if there is one... 909 if (isItemLabelVisible(series, item)) { 910 drawItemLabel(g2, orientation, dataset, series, item, xx, yy, 911 (y1 < 0.0)); 912 } 913 914 int datasetIndex = plot.indexOf(dataset); 915 updateCrosshairValues(crosshairState, x1, y1, datasetIndex, 916 transX1, transY1, orientation); 917 918 // add an entity for the item... 919 if (entities != null && ShapeUtils.isPointInRect(dataArea, xx, yy)) { 920 addEntity(entities, entityArea, dataset, series, item, xx, yy); 921 } 922 923 } 924 925 /** 926 * Tests this renderer for equality with another object. 927 * 928 * @param obj the object ({@code null} permitted). 929 * 930 * @return A boolean. 931 */ 932 @Override 933 public boolean equals(Object obj) { 934 935 if (obj == this) { 936 return true; 937 } 938 if (!(obj instanceof StandardXYItemRenderer)) { 939 return false; 940 } 941 StandardXYItemRenderer that = (StandardXYItemRenderer) obj; 942 if (this.baseShapesVisible != that.baseShapesVisible) { 943 return false; 944 } 945 if (this.plotLines != that.plotLines) { 946 return false; 947 } 948 if (this.plotImages != that.plotImages) { 949 return false; 950 } 951 if (this.plotDiscontinuous != that.plotDiscontinuous) { 952 return false; 953 } 954 if (this.gapThresholdType != that.gapThresholdType) { 955 return false; 956 } 957 if (this.gapThreshold != that.gapThreshold) { 958 return false; 959 } 960 if (!this.seriesShapesFilled.equals(that.seriesShapesFilled)) { 961 return false; 962 } 963 if (this.baseShapesFilled != that.baseShapesFilled) { 964 return false; 965 } 966 if (this.drawSeriesLineAsPath != that.drawSeriesLineAsPath) { 967 return false; 968 } 969 if (!ShapeUtils.equal(this.legendLine, that.legendLine)) { 970 return false; 971 } 972 return super.equals(obj); 973 974 } 975 976 /** 977 * Returns a clone of the renderer. 978 * 979 * @return A clone. 980 * 981 * @throws CloneNotSupportedException if the renderer cannot be cloned. 982 */ 983 @Override 984 public Object clone() throws CloneNotSupportedException { 985 StandardXYItemRenderer clone = (StandardXYItemRenderer) super.clone(); 986 clone.seriesShapesFilled 987 = (BooleanList) this.seriesShapesFilled.clone(); 988 clone.legendLine = ShapeUtils.clone(this.legendLine); 989 return clone; 990 } 991 992 //////////////////////////////////////////////////////////////////////////// 993 // PROTECTED METHODS 994 // These provide the opportunity to subclass the standard renderer and 995 // create custom effects. 996 //////////////////////////////////////////////////////////////////////////// 997 998 /** 999 * Returns the image used to draw a single data item. 1000 * 1001 * @param plot the plot (can be used to obtain standard color information 1002 * etc). 1003 * @param series the series index. 1004 * @param item the item index. 1005 * @param x the x value of the item. 1006 * @param y the y value of the item. 1007 * 1008 * @return The image. 1009 * 1010 * @see #getPlotImages() 1011 */ 1012 protected Image getImage(Plot plot, int series, int item, 1013 double x, double y) { 1014 // this method must be overridden if you want to display images 1015 return null; 1016 } 1017 1018 /** 1019 * Returns the hotspot of the image used to draw a single data item. 1020 * The hotspot is the point relative to the top left of the image 1021 * that should indicate the data item. The default is the center of the 1022 * image. 1023 * 1024 * @param plot the plot (can be used to obtain standard color information 1025 * etc). 1026 * @param image the image (can be used to get size information about the 1027 * image) 1028 * @param series the series index 1029 * @param item the item index 1030 * @param x the x value of the item 1031 * @param y the y value of the item 1032 * 1033 * @return The hotspot used to draw the data item. 1034 */ 1035 protected Point getImageHotspot(Plot plot, int series, int item, 1036 double x, double y, Image image) { 1037 1038 int height = image.getHeight(null); 1039 int width = image.getWidth(null); 1040 return new Point(width / 2, height / 2); 1041 1042 } 1043 1044 /** 1045 * Provides serialization support. 1046 * 1047 * @param stream the input stream. 1048 * 1049 * @throws IOException if there is an I/O error. 1050 * @throws ClassNotFoundException if there is a classpath problem. 1051 */ 1052 private void readObject(ObjectInputStream stream) 1053 throws IOException, ClassNotFoundException { 1054 stream.defaultReadObject(); 1055 this.legendLine = SerialUtils.readShape(stream); 1056 } 1057 1058 /** 1059 * Provides serialization support. 1060 * 1061 * @param stream the output stream. 1062 * 1063 * @throws IOException if there is an I/O error. 1064 */ 1065 private void writeObject(ObjectOutputStream stream) throws IOException { 1066 stream.defaultWriteObject(); 1067 SerialUtils.writeShape(this.legendLine, stream); 1068 } 1069 1070}