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 * AbstractXYItemRenderer.java 029 * --------------------------- 030 * (C) Copyright 2002-2017, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Richard Atkinson; 034 * Focus Computer Services Limited; 035 * Tim Bardzil; 036 * Sergei Ivanov; 037 * Peter Kolb (patch 2809117); 038 * Martin Krauskopf; 039 * 040 * Changes: 041 * -------- 042 * 15-Mar-2002 : Version 1 (DG); 043 * 09-Apr-2002 : Added a getToolTipGenerator() method reflecting the change in 044 * the XYItemRenderer interface (DG); 045 * 05-Aug-2002 : Added a urlGenerator member variable to support HTML image 046 * maps (RA); 047 * 20-Aug-2002 : Added property change events for the tooltip and URL 048 * generators (DG); 049 * 22-Aug-2002 : Moved property change support into AbstractRenderer class (DG); 050 * 23-Sep-2002 : Fixed errors reported by Checkstyle tool (DG); 051 * 18-Nov-2002 : Added methods for drawing grid lines (DG); 052 * 17-Jan-2003 : Moved plot classes into a separate package (DG); 053 * 25-Mar-2003 : Implemented Serializable (DG); 054 * 01-May-2003 : Modified initialise() return type and drawItem() method 055 * signature (DG); 056 * 15-May-2003 : Modified to take into account the plot orientation (DG); 057 * 21-May-2003 : Added labels to markers (DG); 058 * 05-Jun-2003 : Added domain and range grid bands (sponsored by Focus Computer 059 * Services Ltd) (DG); 060 * 27-Jul-2003 : Added getRangeType() to support stacked XY area charts (RA); 061 * 31-Jul-2003 : Deprecated all but the default constructor (DG); 062 * 13-Aug-2003 : Implemented Cloneable (DG); 063 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 064 * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG); 065 * 05-Nov-2003 : Fixed marker rendering bug (833623) (DG); 066 * 11-Feb-2004 : Updated labelling for markers (DG); 067 * 25-Feb-2004 : Added updateCrosshairValues() method. Moved deprecated code 068 * to bottom of source file (DG); 069 * 16-Apr-2004 : Added support for IntervalMarker in drawRangeMarker() method 070 * - thanks to Tim Bardzil (DG); 071 * 05-May-2004 : Fixed bug (948310) where interval markers extend beyond axis 072 * range (DG); 073 * 03-Jun-2004 : Fixed more bugs in drawing interval markers (DG); 074 * 26-Aug-2004 : Added the addEntity() method (DG); 075 * 29-Sep-2004 : Added annotation support (with layers) (DG); 076 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities --> 077 * TextUtilities (DG); 078 * 06-Oct-2004 : Added findDomainBounds() method and renamed 079 * getRangeExtent() --> findRangeBounds() (DG); 080 * 07-Jan-2005 : Removed deprecated code (DG); 081 * 27-Jan-2005 : Modified getLegendItem() to omit hidden series (DG); 082 * 24-Feb-2005 : Added getLegendItems() method (DG); 083 * 08-Mar-2005 : Fixed positioning of marker labels (DG); 084 * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator and 085 * added generators for legend labels, tooltips and URLs (DG); 086 * 01-Jun-2005 : Handle one dimension of the marker label adjustment 087 * automatically (DG); 088 * ------------- JFREECHART 1.0.x --------------------------------------------- 089 * 20-Jul-2006 : Set dataset and series indices in LegendItem (DG); 090 * 24-Oct-2006 : Respect alpha setting in markers (see patch 1567843 by Sergei 091 * Ivanov) (DG); 092 * 24-Oct-2006 : Added code to draw outlines for interval markers (DG); 093 * 24-Nov-2006 : Fixed cloning for legend item generators (DG); 094 * 06-Feb-2007 : Added new updateCrosshairValues() method that takes into 095 * account multiple axis plots (see bug 1086307) (DG); 096 * 20-Feb-2007 : Fixed equals() method implementation (DG); 097 * 01-Mar-2007 : Fixed interval marker drawing (patch 1670686 thanks to 098 * Sergei Ivanov) (DG); 099 * 22-Mar-2007 : Modified the tool tip generator look up (DG); 100 * 23-Mar-2007 : Added drawDomainLine() method (DG); 101 * 20-Apr-2007 : Updated getLegendItem() for renderer change, and deprecated 102 * itemLabelGenerator and toolTipGenerator override fields (DG); 103 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 104 * 12-Nov-2007 : Fixed domain and range band drawing methods (DG); 105 * 07-Apr-2008 : Minor API doc update (DG); 106 * 14-May-2008 : Updated addEntity() method to take plot orientation into 107 * account when the incoming area is null (DG); 108 * 02-Jun-2008 : Added isPointInRect() method (DG); 109 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 110 * 09-Mar-2009 : Added getAnnotations() method (DG); 111 * 27-Mar-2009 : Added new findDomainBounds() and findRangeBounds() methods to 112 * take account of hidden series (DG); 113 * 01-Apr-2009 : Moved defaultEntityRadius up to superclass (DG); 114 * 28-Apr-2009 : Updated getLegendItem() method to observe new 115 * 'treatLegendShapeAsLine' flag (DG); 116 * 24-Jun-2009 : Added support for annotation events - see patch 2809117 117 * by PK (DG); 118 * 01-Sep-2009 : Bug 2840132 - set renderer index when drawing 119 * annotations (DG); 120 * 06-Oct-2011 : Add utility methods to work with 1.4 API in GeneralPath (MK) 121 * 03-Jul-2013 : Use ParamChecks (DG); 122 * 11-Jan-2014 : Fix error in fillDomainGridBand method (DG); 123 * 07-Apr-2014 : Don't use ObjectList anymore (DG); 124 * 29-Jul-2014 : Add rendering hint to normalise domain and range lines (DG); 125 * 24-Aug-2014 : Add beginElementGroup() method, part of JFreeSVG support (DG); 126 * 18-Feb-2017 : Fix for crosshairs with multiple datasets / axes - see 127 * bug #36 (DG); 128 */ 129 130package org.jfree.chart.renderer.xy; 131 132import java.awt.AlphaComposite; 133import java.awt.Composite; 134import java.awt.Font; 135import java.awt.GradientPaint; 136import java.awt.Graphics2D; 137import java.awt.Paint; 138import java.awt.RenderingHints; 139import java.awt.Shape; 140import java.awt.Stroke; 141import java.awt.geom.Ellipse2D; 142import java.awt.geom.GeneralPath; 143import java.awt.geom.Line2D; 144import java.awt.geom.Point2D; 145import java.awt.geom.Rectangle2D; 146import java.io.Serializable; 147import java.util.ArrayList; 148import java.util.Collection; 149import java.util.HashMap; 150import java.util.Iterator; 151import java.util.List; 152import java.util.Map; 153 154import org.jfree.chart.LegendItem; 155import org.jfree.chart.LegendItemCollection; 156import org.jfree.chart.annotations.Annotation; 157import org.jfree.chart.annotations.XYAnnotation; 158import org.jfree.chart.axis.ValueAxis; 159import org.jfree.chart.entity.EntityCollection; 160import org.jfree.chart.entity.XYItemEntity; 161import org.jfree.chart.event.AnnotationChangeEvent; 162import org.jfree.chart.event.AnnotationChangeListener; 163import org.jfree.chart.event.RendererChangeEvent; 164import org.jfree.chart.labels.ItemLabelPosition; 165import org.jfree.chart.labels.StandardXYSeriesLabelGenerator; 166import org.jfree.chart.labels.XYItemLabelGenerator; 167import org.jfree.chart.labels.XYSeriesLabelGenerator; 168import org.jfree.chart.labels.XYToolTipGenerator; 169import org.jfree.chart.plot.CrosshairState; 170import org.jfree.chart.plot.DrawingSupplier; 171import org.jfree.chart.plot.IntervalMarker; 172import org.jfree.chart.plot.Marker; 173import org.jfree.chart.plot.PlotOrientation; 174import org.jfree.chart.plot.PlotRenderingInfo; 175import org.jfree.chart.plot.ValueMarker; 176import org.jfree.chart.plot.XYPlot; 177import org.jfree.chart.renderer.AbstractRenderer; 178import org.jfree.chart.text.TextUtils; 179import org.jfree.chart.ui.GradientPaintTransformer; 180import org.jfree.chart.ui.Layer; 181import org.jfree.chart.ui.LengthAdjustmentType; 182import org.jfree.chart.ui.RectangleAnchor; 183import org.jfree.chart.ui.RectangleInsets; 184import org.jfree.chart.urls.XYURLGenerator; 185import org.jfree.chart.util.CloneUtils; 186import org.jfree.chart.util.ObjectUtils; 187import org.jfree.chart.util.Args; 188import org.jfree.chart.util.PublicCloneable; 189import org.jfree.data.Range; 190import org.jfree.data.general.DatasetUtils; 191import org.jfree.data.xy.XYDataset; 192import org.jfree.data.xy.XYItemKey; 193 194/** 195 * A base class that can be used to create new {@link XYItemRenderer} 196 * implementations. 197 */ 198public abstract class AbstractXYItemRenderer extends AbstractRenderer 199 implements XYItemRenderer, AnnotationChangeListener, 200 Cloneable, Serializable { 201 202 /** For serialization. */ 203 private static final long serialVersionUID = 8019124836026607990L; 204 205 /** The plot. */ 206 private XYPlot plot; 207 208 /** A list of item label generators (one per series). */ 209 private Map<Integer, XYItemLabelGenerator> itemLabelGeneratorMap; 210 211 /** The default item label generator. */ 212 private XYItemLabelGenerator defaultItemLabelGenerator; 213 214 /** A list of tool tip generators (one per series). */ 215 private Map<Integer, XYToolTipGenerator> toolTipGeneratorMap; 216 217 /** The default tool tip generator. */ 218 private XYToolTipGenerator defaultToolTipGenerator; 219 220 /** The URL text generator. */ 221 private XYURLGenerator urlGenerator; 222 223 /** 224 * Annotations to be drawn in the background layer ('underneath' the data 225 * items). 226 */ 227 private List backgroundAnnotations; 228 229 /** 230 * Annotations to be drawn in the foreground layer ('on top' of the data 231 * items). 232 */ 233 private List foregroundAnnotations; 234 235 /** The legend item label generator. */ 236 private XYSeriesLabelGenerator legendItemLabelGenerator; 237 238 /** The legend item tool tip generator. */ 239 private XYSeriesLabelGenerator legendItemToolTipGenerator; 240 241 /** The legend item URL generator. */ 242 private XYSeriesLabelGenerator legendItemURLGenerator; 243 244 /** 245 * Creates a renderer where the tooltip generator and the URL generator are 246 * both {@code null}. 247 */ 248 protected AbstractXYItemRenderer() { 249 super(); 250 this.itemLabelGeneratorMap 251 = new HashMap<Integer, XYItemLabelGenerator>(); 252 this.toolTipGeneratorMap = new HashMap<Integer, XYToolTipGenerator>(); 253 this.urlGenerator = null; 254 this.backgroundAnnotations = new java.util.ArrayList(); 255 this.foregroundAnnotations = new java.util.ArrayList(); 256 this.legendItemLabelGenerator = new StandardXYSeriesLabelGenerator( 257 "{0}"); 258 } 259 260 /** 261 * Returns the number of passes through the data that the renderer requires 262 * in order to draw the chart. Most charts will require a single pass, but 263 * some require two passes. 264 * 265 * @return The pass count. 266 */ 267 @Override 268 public int getPassCount() { 269 return 1; 270 } 271 272 /** 273 * Returns the plot that the renderer is assigned to. 274 * 275 * @return The plot (possibly {@code null}). 276 */ 277 @Override 278 public XYPlot getPlot() { 279 return this.plot; 280 } 281 282 /** 283 * Sets the plot that the renderer is assigned to. 284 * 285 * @param plot the plot ({@code null} permitted). 286 */ 287 @Override 288 public void setPlot(XYPlot plot) { 289 this.plot = plot; 290 } 291 292 /** 293 * Initialises the renderer and returns a state object that should be 294 * passed to all subsequent calls to the drawItem() method. 295 * <P> 296 * This method will be called before the first item is rendered, giving the 297 * renderer an opportunity to initialise any state information it wants to 298 * maintain. The renderer can do nothing if it chooses. 299 * 300 * @param g2 the graphics device. 301 * @param dataArea the area inside the axes. 302 * @param plot the plot. 303 * @param dataset the dataset. 304 * @param info an optional info collection object to return data back to 305 * the caller. 306 * 307 * @return The renderer state (never {@code null}). 308 */ 309 @Override 310 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, 311 XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { 312 return new XYItemRendererState(info); 313 } 314 315 /** 316 * Adds a {@code KEY_BEGIN_ELEMENT} hint to the graphics target. This 317 * hint is recognised by <b>JFreeSVG</b> (in theory it could be used by 318 * other {@code Graphics2D} implementations also). 319 * 320 * @param g2 the graphics target ({@code null} not permitted). 321 * @param seriesKey the series key that identifies the element 322 * ({@code null} not permitted). 323 * @param itemIndex the item index. 324 * 325 * @since 1.0.20 326 */ 327 protected void beginElementGroup(Graphics2D g2, Comparable seriesKey, 328 int itemIndex) { 329 beginElementGroup(g2, new XYItemKey(seriesKey, itemIndex)); 330 } 331 332 // ITEM LABEL GENERATOR 333 334 /** 335 * Returns the label generator for a data item. This implementation simply 336 * passes control to the {@link #getSeriesItemLabelGenerator(int)} method. 337 * If, for some reason, you want a different generator for individual 338 * items, you can override this method. 339 * 340 * @param series the series index (zero based). 341 * @param item the item index (zero based). 342 * 343 * @return The generator (possibly {@code null}). 344 */ 345 @Override 346 public XYItemLabelGenerator getItemLabelGenerator(int series, int item) { 347 348 // otherwise look up the generator table 349 XYItemLabelGenerator generator = this.itemLabelGeneratorMap.get(series); 350 if (generator == null) { 351 generator = this.defaultItemLabelGenerator; 352 } 353 return generator; 354 } 355 356 /** 357 * Returns the item label generator for a series. 358 * 359 * @param series the series index (zero based). 360 * 361 * @return The generator (possibly {@code null}). 362 */ 363 @Override 364 public XYItemLabelGenerator getSeriesItemLabelGenerator(int series) { 365 return this.itemLabelGeneratorMap.get(series); 366 } 367 368 /** 369 * Sets the item label generator for a series and sends a 370 * {@link RendererChangeEvent} to all registered listeners. 371 * 372 * @param series the series index (zero based). 373 * @param generator the generator ({@code null} permitted). 374 */ 375 @Override 376 public void setSeriesItemLabelGenerator(int series, 377 XYItemLabelGenerator generator) { 378 this.itemLabelGeneratorMap.put(series, generator); 379 fireChangeEvent(); 380 } 381 382 /** 383 * Returns the default item label generator. 384 * 385 * @return The generator (possibly {@code null}). 386 */ 387 @Override 388 public XYItemLabelGenerator getDefaultItemLabelGenerator() { 389 return this.defaultItemLabelGenerator; 390 } 391 392 /** 393 * Sets the default item label generator and sends a 394 * {@link RendererChangeEvent} to all registered listeners. 395 * 396 * @param generator the generator ({@code null} permitted). 397 */ 398 @Override 399 public void setDefaultItemLabelGenerator(XYItemLabelGenerator generator) { 400 this.defaultItemLabelGenerator = generator; 401 fireChangeEvent(); 402 } 403 404 // TOOL TIP GENERATOR 405 406 /** 407 * Returns the tool tip generator for a data item. If, for some reason, 408 * you want a different generator for individual items, you can override 409 * this method. 410 * 411 * @param series the series index (zero based). 412 * @param item the item index (zero based). 413 * 414 * @return The generator (possibly {@code null}). 415 */ 416 @Override 417 public XYToolTipGenerator getToolTipGenerator(int series, int item) { 418 419 // otherwise look up the generator table 420 XYToolTipGenerator generator = this.toolTipGeneratorMap.get(series); 421 if (generator == null) { 422 generator = this.defaultToolTipGenerator; 423 } 424 return generator; 425 } 426 427 /** 428 * Returns the tool tip generator for a series. 429 * 430 * @param series the series index (zero based). 431 * 432 * @return The generator (possibly {@code null}). 433 */ 434 @Override 435 public XYToolTipGenerator getSeriesToolTipGenerator(int series) { 436 return this.toolTipGeneratorMap.get(series); 437 } 438 439 /** 440 * Sets the tool tip generator for a series and sends a 441 * {@link RendererChangeEvent} to all registered listeners. 442 * 443 * @param series the series index (zero based). 444 * @param generator the generator ({@code null} permitted). 445 */ 446 @Override 447 public void setSeriesToolTipGenerator(int series, 448 XYToolTipGenerator generator) { 449 this.toolTipGeneratorMap.put(series, generator); 450 fireChangeEvent(); 451 } 452 453 /** 454 * Returns the default tool tip generator. 455 * 456 * @return The generator (possibly {@code null}). 457 * 458 * @see #setDefaultToolTipGenerator(XYToolTipGenerator) 459 */ 460 @Override 461 public XYToolTipGenerator getDefaultToolTipGenerator() { 462 return this.defaultToolTipGenerator; 463 } 464 465 /** 466 * Sets the default tool tip generator and sends a 467 * {@link RendererChangeEvent} to all registered listeners. 468 * 469 * @param generator the generator ({@code null} permitted). 470 * 471 * @see #getDefaultToolTipGenerator() 472 */ 473 @Override 474 public void setDefaultToolTipGenerator(XYToolTipGenerator generator) { 475 this.defaultToolTipGenerator = generator; 476 fireChangeEvent(); 477 } 478 479 // URL GENERATOR 480 481 /** 482 * Returns the URL generator for HTML image maps. 483 * 484 * @return The URL generator (possibly {@code null}). 485 */ 486 @Override 487 public XYURLGenerator getURLGenerator() { 488 return this.urlGenerator; 489 } 490 491 /** 492 * Sets the URL generator for HTML image maps and sends a 493 * {@link RendererChangeEvent} to all registered listeners. 494 * 495 * @param urlGenerator the URL generator ({@code null} permitted). 496 */ 497 @Override 498 public void setURLGenerator(XYURLGenerator urlGenerator) { 499 this.urlGenerator = urlGenerator; 500 fireChangeEvent(); 501 } 502 503 /** 504 * Adds an annotation and sends a {@link RendererChangeEvent} to all 505 * registered listeners. The annotation is added to the foreground 506 * layer. 507 * 508 * @param annotation the annotation ({@code null} not permitted). 509 */ 510 @Override 511 public void addAnnotation(XYAnnotation annotation) { 512 // defer argument checking 513 addAnnotation(annotation, Layer.FOREGROUND); 514 } 515 516 /** 517 * Adds an annotation to the specified layer and sends a 518 * {@link RendererChangeEvent} to all registered listeners. 519 * 520 * @param annotation the annotation ({@code null} not permitted). 521 * @param layer the layer ({@code null} not permitted). 522 */ 523 @Override 524 public void addAnnotation(XYAnnotation annotation, Layer layer) { 525 Args.nullNotPermitted(annotation, "annotation"); 526 if (layer.equals(Layer.FOREGROUND)) { 527 this.foregroundAnnotations.add(annotation); 528 annotation.addChangeListener(this); 529 fireChangeEvent(); 530 } 531 else if (layer.equals(Layer.BACKGROUND)) { 532 this.backgroundAnnotations.add(annotation); 533 annotation.addChangeListener(this); 534 fireChangeEvent(); 535 } 536 else { 537 // should never get here 538 throw new RuntimeException("Unknown layer."); 539 } 540 } 541 /** 542 * Removes the specified annotation and sends a {@link RendererChangeEvent} 543 * to all registered listeners. 544 * 545 * @param annotation the annotation to remove ({@code null} not 546 * permitted). 547 * 548 * @return A boolean to indicate whether or not the annotation was 549 * successfully removed. 550 */ 551 @Override 552 public boolean removeAnnotation(XYAnnotation annotation) { 553 boolean removed = this.foregroundAnnotations.remove(annotation); 554 removed = removed & this.backgroundAnnotations.remove(annotation); 555 annotation.removeChangeListener(this); 556 fireChangeEvent(); 557 return removed; 558 } 559 560 /** 561 * Removes all annotations and sends a {@link RendererChangeEvent} 562 * to all registered listeners. 563 */ 564 @Override 565 public void removeAnnotations() { 566 for(int i = 0; i < this.foregroundAnnotations.size(); i++){ 567 XYAnnotation annotation 568 = (XYAnnotation) this.foregroundAnnotations.get(i); 569 annotation.removeChangeListener(this); 570 } 571 for(int i = 0; i < this.backgroundAnnotations.size(); i++){ 572 XYAnnotation annotation 573 = (XYAnnotation) this.backgroundAnnotations.get(i); 574 annotation.removeChangeListener(this); 575 } 576 this.foregroundAnnotations.clear(); 577 this.backgroundAnnotations.clear(); 578 fireChangeEvent(); 579 } 580 581 582 /** 583 * Receives notification of a change to an {@link Annotation} added to 584 * this renderer. 585 * 586 * @param event information about the event (not used here). 587 * 588 * @since 1.0.14 589 */ 590 @Override 591 public void annotationChanged(AnnotationChangeEvent event) { 592 fireChangeEvent(); 593 } 594 595 /** 596 * Returns a collection of the annotations that are assigned to the 597 * renderer. 598 * 599 * @return A collection of annotations (possibly empty but never 600 * {@code null}). 601 * 602 * @since 1.0.13 603 */ 604 public Collection getAnnotations() { 605 List result = new java.util.ArrayList(this.foregroundAnnotations); 606 result.addAll(this.backgroundAnnotations); 607 return result; 608 } 609 610 /** 611 * Returns the legend item label generator. 612 * 613 * @return The label generator (never {@code null}). 614 * 615 * @see #setLegendItemLabelGenerator(XYSeriesLabelGenerator) 616 */ 617 @Override 618 public XYSeriesLabelGenerator getLegendItemLabelGenerator() { 619 return this.legendItemLabelGenerator; 620 } 621 622 /** 623 * Sets the legend item label generator and sends a 624 * {@link RendererChangeEvent} to all registered listeners. 625 * 626 * @param generator the generator ({@code null} not permitted). 627 * 628 * @see #getLegendItemLabelGenerator() 629 */ 630 @Override 631 public void setLegendItemLabelGenerator(XYSeriesLabelGenerator generator) { 632 Args.nullNotPermitted(generator, "generator"); 633 this.legendItemLabelGenerator = generator; 634 fireChangeEvent(); 635 } 636 637 /** 638 * Returns the legend item tool tip generator. 639 * 640 * @return The tool tip generator (possibly {@code null}). 641 * 642 * @see #setLegendItemToolTipGenerator(XYSeriesLabelGenerator) 643 */ 644 public XYSeriesLabelGenerator getLegendItemToolTipGenerator() { 645 return this.legendItemToolTipGenerator; 646 } 647 648 /** 649 * Sets the legend item tool tip generator and sends a 650 * {@link RendererChangeEvent} to all registered listeners. 651 * 652 * @param generator the generator ({@code null} permitted). 653 * 654 * @see #getLegendItemToolTipGenerator() 655 */ 656 public void setLegendItemToolTipGenerator( 657 XYSeriesLabelGenerator generator) { 658 this.legendItemToolTipGenerator = generator; 659 fireChangeEvent(); 660 } 661 662 /** 663 * Returns the legend item URL generator. 664 * 665 * @return The URL generator (possibly {@code null}). 666 * 667 * @see #setLegendItemURLGenerator(XYSeriesLabelGenerator) 668 */ 669 public XYSeriesLabelGenerator getLegendItemURLGenerator() { 670 return this.legendItemURLGenerator; 671 } 672 673 /** 674 * Sets the legend item URL generator and sends a 675 * {@link RendererChangeEvent} to all registered listeners. 676 * 677 * @param generator the generator ({@code null} permitted). 678 * 679 * @see #getLegendItemURLGenerator() 680 */ 681 public void setLegendItemURLGenerator(XYSeriesLabelGenerator generator) { 682 this.legendItemURLGenerator = generator; 683 fireChangeEvent(); 684 } 685 686 /** 687 * Returns the lower and upper bounds (range) of the x-values in the 688 * specified dataset. 689 * 690 * @param dataset the dataset ({@code null} permitted). 691 * 692 * @return The range ({@code null} if the dataset is {@code null} 693 * or empty). 694 * 695 * @see #findRangeBounds(XYDataset) 696 */ 697 @Override 698 public Range findDomainBounds(XYDataset dataset) { 699 return findDomainBounds(dataset, false); 700 } 701 702 /** 703 * Returns the lower and upper bounds (range) of the x-values in the 704 * specified dataset. 705 * 706 * @param dataset the dataset ({@code null} permitted). 707 * @param includeInterval include the interval (if any) for the dataset? 708 * 709 * @return The range ({@code null} if the dataset is {@code null} 710 * or empty). 711 * 712 * @since 1.0.13 713 */ 714 protected Range findDomainBounds(XYDataset dataset, 715 boolean includeInterval) { 716 if (dataset == null) { 717 return null; 718 } 719 if (getDataBoundsIncludesVisibleSeriesOnly()) { 720 List visibleSeriesKeys = new ArrayList(); 721 int seriesCount = dataset.getSeriesCount(); 722 for (int s = 0; s < seriesCount; s++) { 723 if (isSeriesVisible(s)) { 724 visibleSeriesKeys.add(dataset.getSeriesKey(s)); 725 } 726 } 727 return DatasetUtils.findDomainBounds(dataset, 728 visibleSeriesKeys, includeInterval); 729 } 730 return DatasetUtils.findDomainBounds(dataset, includeInterval); 731 } 732 733 /** 734 * Returns the range of values the renderer requires to display all the 735 * items from the specified dataset. 736 * 737 * @param dataset the dataset ({@code null} permitted). 738 * 739 * @return The range ({@code null} if the dataset is {@code null} 740 * or empty). 741 * 742 * @see #findDomainBounds(XYDataset) 743 */ 744 @Override 745 public Range findRangeBounds(XYDataset dataset) { 746 return findRangeBounds(dataset, false); 747 } 748 749 /** 750 * Returns the range of values the renderer requires to display all the 751 * items from the specified dataset. 752 * 753 * @param dataset the dataset ({@code null} permitted). 754 * @param includeInterval include the interval (if any) for the dataset? 755 * 756 * @return The range ({@code null} if the dataset is {@code null} 757 * or empty). 758 * 759 * @since 1.0.13 760 */ 761 protected Range findRangeBounds(XYDataset dataset, 762 boolean includeInterval) { 763 if (dataset == null) { 764 return null; 765 } 766 if (getDataBoundsIncludesVisibleSeriesOnly()) { 767 List visibleSeriesKeys = new ArrayList(); 768 int seriesCount = dataset.getSeriesCount(); 769 for (int s = 0; s < seriesCount; s++) { 770 if (isSeriesVisible(s)) { 771 visibleSeriesKeys.add(dataset.getSeriesKey(s)); 772 } 773 } 774 // the bounds should be calculated using just the items within 775 // the current range of the x-axis...if there is one 776 Range xRange = null; 777 XYPlot p = getPlot(); 778 if (p != null) { 779 ValueAxis xAxis = null; 780 int index = p.getIndexOf(this); 781 if (index >= 0) { 782 xAxis = this.plot.getDomainAxisForDataset(index); 783 } 784 if (xAxis != null) { 785 xRange = xAxis.getRange(); 786 } 787 } 788 if (xRange == null) { 789 xRange = new Range(Double.NEGATIVE_INFINITY, 790 Double.POSITIVE_INFINITY); 791 } 792 return DatasetUtils.findRangeBounds(dataset, 793 visibleSeriesKeys, xRange, includeInterval); 794 } 795 return DatasetUtils.findRangeBounds(dataset, includeInterval); 796 } 797 798 /** 799 * Returns a (possibly empty) collection of legend items for the series 800 * that this renderer is responsible for drawing. 801 * 802 * @return The legend item collection (never {@code null}). 803 */ 804 @Override 805 public LegendItemCollection getLegendItems() { 806 if (this.plot == null) { 807 return new LegendItemCollection(); 808 } 809 LegendItemCollection result = new LegendItemCollection(); 810 int index = this.plot.getIndexOf(this); 811 XYDataset dataset = this.plot.getDataset(index); 812 if (dataset != null) { 813 int seriesCount = dataset.getSeriesCount(); 814 for (int i = 0; i < seriesCount; i++) { 815 if (isSeriesVisibleInLegend(i)) { 816 LegendItem item = getLegendItem(index, i); 817 if (item != null) { 818 result.add(item); 819 } 820 } 821 } 822 823 } 824 return result; 825 } 826 827 /** 828 * Returns a default legend item for the specified series. Subclasses 829 * should override this method to generate customised items. 830 * 831 * @param datasetIndex the dataset index (zero-based). 832 * @param series the series index (zero-based). 833 * 834 * @return A legend item for the series. 835 */ 836 @Override 837 public LegendItem getLegendItem(int datasetIndex, int series) { 838 XYPlot xyplot = getPlot(); 839 if (xyplot == null) { 840 return null; 841 } 842 XYDataset dataset = xyplot.getDataset(datasetIndex); 843 if (dataset == null) { 844 return null; 845 } 846 String label = this.legendItemLabelGenerator.generateLabel(dataset, 847 series); 848 String description = label; 849 String toolTipText = null; 850 if (getLegendItemToolTipGenerator() != null) { 851 toolTipText = getLegendItemToolTipGenerator().generateLabel( 852 dataset, series); 853 } 854 String urlText = null; 855 if (getLegendItemURLGenerator() != null) { 856 urlText = getLegendItemURLGenerator().generateLabel(dataset, 857 series); 858 } 859 Shape shape = lookupLegendShape(series); 860 Paint paint = lookupSeriesPaint(series); 861 LegendItem item = new LegendItem(label, paint); 862 item.setToolTipText(toolTipText); 863 item.setURLText(urlText); 864 item.setLabelFont(lookupLegendTextFont(series)); 865 Paint labelPaint = lookupLegendTextPaint(series); 866 if (labelPaint != null) { 867 item.setLabelPaint(labelPaint); 868 } 869 item.setSeriesKey(dataset.getSeriesKey(series)); 870 item.setSeriesIndex(series); 871 item.setDataset(dataset); 872 item.setDatasetIndex(datasetIndex); 873 874 if (getTreatLegendShapeAsLine()) { 875 item.setLineVisible(true); 876 item.setLine(shape); 877 item.setLinePaint(paint); 878 item.setShapeVisible(false); 879 } 880 else { 881 Paint outlinePaint = lookupSeriesOutlinePaint(series); 882 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 883 item.setOutlinePaint(outlinePaint); 884 item.setOutlineStroke(outlineStroke); 885 } 886 return item; 887 } 888 889 /** 890 * Fills a band between two values on the axis. This can be used to color 891 * bands between the grid lines. 892 * 893 * @param g2 the graphics device. 894 * @param plot the plot. 895 * @param axis the domain axis. 896 * @param dataArea the data area. 897 * @param start the start value. 898 * @param end the end value. 899 */ 900 @Override 901 public void fillDomainGridBand(Graphics2D g2, XYPlot plot, ValueAxis axis, 902 Rectangle2D dataArea, double start, double end) { 903 904 double x1 = axis.valueToJava2D(start, dataArea, 905 plot.getDomainAxisEdge()); 906 double x2 = axis.valueToJava2D(end, dataArea, 907 plot.getDomainAxisEdge()); 908 Rectangle2D band; 909 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 910 band = new Rectangle2D.Double(Math.min(x1, x2), dataArea.getMinY(), 911 Math.abs(x2 - x1), dataArea.getHeight()); 912 } 913 else { 914 band = new Rectangle2D.Double(dataArea.getMinX(), Math.min(x1, x2), 915 dataArea.getWidth(), Math.abs(x2 - x1)); 916 } 917 Paint paint = plot.getDomainTickBandPaint(); 918 919 if (paint != null) { 920 g2.setPaint(paint); 921 g2.fill(band); 922 } 923 924 } 925 926 /** 927 * Fills a band between two values on the range axis. This can be used to 928 * color bands between the grid lines. 929 * 930 * @param g2 the graphics device. 931 * @param plot the plot. 932 * @param axis the range axis. 933 * @param dataArea the data area. 934 * @param start the start value. 935 * @param end the end value. 936 */ 937 @Override 938 public void fillRangeGridBand(Graphics2D g2, XYPlot plot, ValueAxis axis, 939 Rectangle2D dataArea, double start, double end) { 940 941 double y1 = axis.valueToJava2D(start, dataArea, 942 plot.getRangeAxisEdge()); 943 double y2 = axis.valueToJava2D(end, dataArea, plot.getRangeAxisEdge()); 944 Rectangle2D band; 945 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 946 band = new Rectangle2D.Double(dataArea.getMinX(), Math.min(y1, y2), 947 dataArea.getWidth(), Math.abs(y2 - y1)); 948 } 949 else { 950 band = new Rectangle2D.Double(Math.min(y1, y2), dataArea.getMinY(), 951 Math.abs(y2 - y1), dataArea.getHeight()); 952 } 953 Paint paint = plot.getRangeTickBandPaint(); 954 955 if (paint != null) { 956 g2.setPaint(paint); 957 g2.fill(band); 958 } 959 960 } 961 962 /** 963 * Draws a line perpendicular to the domain axis. 964 * 965 * @param g2 the graphics device. 966 * @param plot the plot. 967 * @param axis the value axis. 968 * @param dataArea the area for plotting data (not yet adjusted for any 3D 969 * effect). 970 * @param value the value at which the grid line should be drawn. 971 * @param paint the paint ({@code null} not permitted). 972 * @param stroke the stroke ({@code null} not permitted). 973 * 974 * @since 1.0.5 975 */ 976 @Override 977 public void drawDomainLine(Graphics2D g2, XYPlot plot, ValueAxis axis, 978 Rectangle2D dataArea, double value, Paint paint, Stroke stroke) { 979 980 Range range = axis.getRange(); 981 if (!range.contains(value)) { 982 return; 983 } 984 985 PlotOrientation orientation = plot.getOrientation(); 986 Line2D line = null; 987 double v = axis.valueToJava2D(value, dataArea, 988 plot.getDomainAxisEdge()); 989 if (orientation.isHorizontal()) { 990 line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), 991 v); 992 } else if (orientation.isVertical()) { 993 line = new Line2D.Double(v, dataArea.getMinY(), v, 994 dataArea.getMaxY()); 995 } 996 997 g2.setPaint(paint); 998 g2.setStroke(stroke); 999 Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL); 1000 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 1001 RenderingHints.VALUE_STROKE_NORMALIZE); 1002 g2.draw(line); 1003 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved); 1004 } 1005 1006 /** 1007 * Draws a line perpendicular to the range axis. 1008 * 1009 * @param g2 the graphics device. 1010 * @param plot the plot. 1011 * @param axis the value axis. 1012 * @param dataArea the area for plotting data (not yet adjusted for any 3D 1013 * effect). 1014 * @param value the value at which the grid line should be drawn. 1015 * @param paint the paint. 1016 * @param stroke the stroke. 1017 */ 1018 @Override 1019 public void drawRangeLine(Graphics2D g2, XYPlot plot, ValueAxis axis, 1020 Rectangle2D dataArea, double value, Paint paint, Stroke stroke) { 1021 1022 Range range = axis.getRange(); 1023 if (!range.contains(value)) { 1024 return; 1025 } 1026 1027 PlotOrientation orientation = plot.getOrientation(); 1028 Line2D line = null; 1029 double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge()); 1030 if (orientation == PlotOrientation.HORIZONTAL) { 1031 line = new Line2D.Double(v, dataArea.getMinY(), v, 1032 dataArea.getMaxY()); 1033 } else if (orientation == PlotOrientation.VERTICAL) { 1034 line = new Line2D.Double(dataArea.getMinX(), v, 1035 dataArea.getMaxX(), v); 1036 } 1037 1038 g2.setPaint(paint); 1039 g2.setStroke(stroke); 1040 Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL); 1041 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 1042 RenderingHints.VALUE_STROKE_NORMALIZE); 1043 g2.draw(line); 1044 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved); 1045 } 1046 1047 /** 1048 * Draws a line on the chart perpendicular to the x-axis to mark 1049 * a value or range of values. 1050 * 1051 * @param g2 the graphics device. 1052 * @param plot the plot. 1053 * @param domainAxis the domain axis. 1054 * @param marker the marker line. 1055 * @param dataArea the axis data area. 1056 */ 1057 @Override 1058 public void drawDomainMarker(Graphics2D g2, XYPlot plot, 1059 ValueAxis domainAxis, Marker marker, Rectangle2D dataArea) { 1060 1061 if (marker instanceof ValueMarker) { 1062 ValueMarker vm = (ValueMarker) marker; 1063 double value = vm.getValue(); 1064 Range range = domainAxis.getRange(); 1065 if (!range.contains(value)) { 1066 return; 1067 } 1068 1069 double v = domainAxis.valueToJava2D(value, dataArea, 1070 plot.getDomainAxisEdge()); 1071 PlotOrientation orientation = plot.getOrientation(); 1072 Line2D line = null; 1073 if (orientation == PlotOrientation.HORIZONTAL) { 1074 line = new Line2D.Double(dataArea.getMinX(), v, 1075 dataArea.getMaxX(), v); 1076 } else if (orientation == PlotOrientation.VERTICAL) { 1077 line = new Line2D.Double(v, dataArea.getMinY(), v, 1078 dataArea.getMaxY()); 1079 } else { 1080 throw new IllegalStateException("Unrecognised orientation."); 1081 } 1082 1083 final Composite originalComposite = g2.getComposite(); 1084 g2.setComposite(AlphaComposite.getInstance( 1085 AlphaComposite.SRC_OVER, marker.getAlpha())); 1086 g2.setPaint(marker.getPaint()); 1087 g2.setStroke(marker.getStroke()); 1088 g2.draw(line); 1089 1090 String label = marker.getLabel(); 1091 RectangleAnchor anchor = marker.getLabelAnchor(); 1092 if (label != null) { 1093 Font labelFont = marker.getLabelFont(); 1094 g2.setFont(labelFont); 1095 Point2D coords = calculateDomainMarkerTextAnchorPoint( 1096 g2, orientation, dataArea, line.getBounds2D(), 1097 marker.getLabelOffset(), 1098 LengthAdjustmentType.EXPAND, anchor); 1099 Rectangle2D r = TextUtils.calcAlignedStringBounds(label, 1100 g2, (float) coords.getX(), (float) coords.getY(), 1101 marker.getLabelTextAnchor()); 1102 g2.setPaint(marker.getLabelBackgroundColor()); 1103 g2.fill(r); 1104 g2.setPaint(marker.getLabelPaint()); 1105 TextUtils.drawAlignedString(label, g2, 1106 (float) coords.getX(), (float) coords.getY(), 1107 marker.getLabelTextAnchor()); 1108 } 1109 g2.setComposite(originalComposite); 1110 } else if (marker instanceof IntervalMarker) { 1111 IntervalMarker im = (IntervalMarker) marker; 1112 double start = im.getStartValue(); 1113 double end = im.getEndValue(); 1114 Range range = domainAxis.getRange(); 1115 if (!(range.intersects(start, end))) { 1116 return; 1117 } 1118 1119 double start2d = domainAxis.valueToJava2D(start, dataArea, 1120 plot.getDomainAxisEdge()); 1121 double end2d = domainAxis.valueToJava2D(end, dataArea, 1122 plot.getDomainAxisEdge()); 1123 double low = Math.min(start2d, end2d); 1124 double high = Math.max(start2d, end2d); 1125 1126 PlotOrientation orientation = plot.getOrientation(); 1127 Rectangle2D rect = null; 1128 if (orientation == PlotOrientation.HORIZONTAL) { 1129 // clip top and bottom bounds to data area 1130 low = Math.max(low, dataArea.getMinY()); 1131 high = Math.min(high, dataArea.getMaxY()); 1132 rect = new Rectangle2D.Double(dataArea.getMinX(), 1133 low, dataArea.getWidth(), 1134 high - low); 1135 } else if (orientation == PlotOrientation.VERTICAL) { 1136 // clip left and right bounds to data area 1137 low = Math.max(low, dataArea.getMinX()); 1138 high = Math.min(high, dataArea.getMaxX()); 1139 rect = new Rectangle2D.Double(low, 1140 dataArea.getMinY(), high - low, 1141 dataArea.getHeight()); 1142 } 1143 1144 final Composite originalComposite = g2.getComposite(); 1145 g2.setComposite(AlphaComposite.getInstance( 1146 AlphaComposite.SRC_OVER, marker.getAlpha())); 1147 Paint p = marker.getPaint(); 1148 if (p instanceof GradientPaint) { 1149 GradientPaint gp = (GradientPaint) p; 1150 GradientPaintTransformer t = im.getGradientPaintTransformer(); 1151 if (t != null) { 1152 gp = t.transform(gp, rect); 1153 } 1154 g2.setPaint(gp); 1155 } else { 1156 g2.setPaint(p); 1157 } 1158 g2.fill(rect); 1159 1160 // now draw the outlines, if visible... 1161 if (im.getOutlinePaint() != null && im.getOutlineStroke() != null) { 1162 if (orientation == PlotOrientation.VERTICAL) { 1163 Line2D line = new Line2D.Double(); 1164 double y0 = dataArea.getMinY(); 1165 double y1 = dataArea.getMaxY(); 1166 g2.setPaint(im.getOutlinePaint()); 1167 g2.setStroke(im.getOutlineStroke()); 1168 if (range.contains(start)) { 1169 line.setLine(start2d, y0, start2d, y1); 1170 g2.draw(line); 1171 } 1172 if (range.contains(end)) { 1173 line.setLine(end2d, y0, end2d, y1); 1174 g2.draw(line); 1175 } 1176 } else { // PlotOrientation.HORIZONTAL 1177 Line2D line = new Line2D.Double(); 1178 double x0 = dataArea.getMinX(); 1179 double x1 = dataArea.getMaxX(); 1180 g2.setPaint(im.getOutlinePaint()); 1181 g2.setStroke(im.getOutlineStroke()); 1182 if (range.contains(start)) { 1183 line.setLine(x0, start2d, x1, start2d); 1184 g2.draw(line); 1185 } 1186 if (range.contains(end)) { 1187 line.setLine(x0, end2d, x1, end2d); 1188 g2.draw(line); 1189 } 1190 } 1191 } 1192 1193 String label = marker.getLabel(); 1194 RectangleAnchor anchor = marker.getLabelAnchor(); 1195 if (label != null) { 1196 Font labelFont = marker.getLabelFont(); 1197 g2.setFont(labelFont); 1198 Point2D coords = calculateDomainMarkerTextAnchorPoint( 1199 g2, orientation, dataArea, rect, 1200 marker.getLabelOffset(), marker.getLabelOffsetType(), 1201 anchor); 1202 Rectangle2D r = TextUtils.calcAlignedStringBounds(label, 1203 g2, (float) coords.getX(), (float) coords.getY(), 1204 marker.getLabelTextAnchor()); 1205 g2.setPaint(marker.getLabelBackgroundColor()); 1206 g2.fill(r); 1207 g2.setPaint(marker.getLabelPaint()); 1208 TextUtils.drawAlignedString(label, g2, 1209 (float) coords.getX(), (float) coords.getY(), 1210 marker.getLabelTextAnchor()); 1211 } 1212 g2.setComposite(originalComposite); 1213 } 1214 } 1215 1216 /** 1217 * Calculates the {@code (x, y)} coordinates for drawing a marker label. 1218 * 1219 * @param g2 the graphics device. 1220 * @param orientation the plot orientation. 1221 * @param dataArea the data area. 1222 * @param markerArea the rectangle surrounding the marker area. 1223 * @param markerOffset the marker label offset. 1224 * @param labelOffsetType the label offset type. 1225 * @param anchor the label anchor. 1226 * 1227 * @return The coordinates for drawing the marker label. 1228 */ 1229 protected Point2D calculateDomainMarkerTextAnchorPoint(Graphics2D g2, 1230 PlotOrientation orientation, Rectangle2D dataArea, 1231 Rectangle2D markerArea, RectangleInsets markerOffset, 1232 LengthAdjustmentType labelOffsetType, RectangleAnchor anchor) { 1233 1234 Rectangle2D anchorRect = null; 1235 if (orientation == PlotOrientation.HORIZONTAL) { 1236 anchorRect = markerOffset.createAdjustedRectangle(markerArea, 1237 LengthAdjustmentType.CONTRACT, labelOffsetType); 1238 } 1239 else if (orientation == PlotOrientation.VERTICAL) { 1240 anchorRect = markerOffset.createAdjustedRectangle(markerArea, 1241 labelOffsetType, LengthAdjustmentType.CONTRACT); 1242 } 1243 return anchor.getAnchorPoint(anchorRect); 1244 1245 } 1246 1247 /** 1248 * Draws a line on the chart perpendicular to the y-axis to mark a value 1249 * or range of values. 1250 * 1251 * @param g2 the graphics device. 1252 * @param plot the plot. 1253 * @param rangeAxis the range axis. 1254 * @param marker the marker line. 1255 * @param dataArea the axis data area. 1256 */ 1257 @Override 1258 public void drawRangeMarker(Graphics2D g2, XYPlot plot, ValueAxis rangeAxis, 1259 Marker marker, Rectangle2D dataArea) { 1260 1261 if (marker instanceof ValueMarker) { 1262 ValueMarker vm = (ValueMarker) marker; 1263 double value = vm.getValue(); 1264 Range range = rangeAxis.getRange(); 1265 if (!range.contains(value)) { 1266 return; 1267 } 1268 1269 double v = rangeAxis.valueToJava2D(value, dataArea, 1270 plot.getRangeAxisEdge()); 1271 PlotOrientation orientation = plot.getOrientation(); 1272 Line2D line = null; 1273 if (orientation == PlotOrientation.HORIZONTAL) { 1274 line = new Line2D.Double(v, dataArea.getMinY(), v, 1275 dataArea.getMaxY()); 1276 } else if (orientation == PlotOrientation.VERTICAL) { 1277 line = new Line2D.Double(dataArea.getMinX(), v, 1278 dataArea.getMaxX(), v); 1279 } else { 1280 throw new IllegalStateException("Unrecognised orientation."); 1281 } 1282 1283 final Composite originalComposite = g2.getComposite(); 1284 g2.setComposite(AlphaComposite.getInstance( 1285 AlphaComposite.SRC_OVER, marker.getAlpha())); 1286 g2.setPaint(marker.getPaint()); 1287 g2.setStroke(marker.getStroke()); 1288 g2.draw(line); 1289 1290 String label = marker.getLabel(); 1291 RectangleAnchor anchor = marker.getLabelAnchor(); 1292 if (label != null) { 1293 Font labelFont = marker.getLabelFont(); 1294 g2.setFont(labelFont); 1295 Point2D coords = calculateRangeMarkerTextAnchorPoint( 1296 g2, orientation, dataArea, line.getBounds2D(), 1297 marker.getLabelOffset(), 1298 LengthAdjustmentType.EXPAND, anchor); 1299 Rectangle2D r = TextUtils.calcAlignedStringBounds(label, 1300 g2, (float) coords.getX(), (float) coords.getY(), 1301 marker.getLabelTextAnchor()); 1302 g2.setPaint(marker.getLabelBackgroundColor()); 1303 g2.fill(r); 1304 g2.setPaint(marker.getLabelPaint()); 1305 TextUtils.drawAlignedString(label, g2, 1306 (float) coords.getX(), (float) coords.getY(), 1307 marker.getLabelTextAnchor()); 1308 } 1309 g2.setComposite(originalComposite); 1310 } else if (marker instanceof IntervalMarker) { 1311 IntervalMarker im = (IntervalMarker) marker; 1312 double start = im.getStartValue(); 1313 double end = im.getEndValue(); 1314 Range range = rangeAxis.getRange(); 1315 if (!(range.intersects(start, end))) { 1316 return; 1317 } 1318 1319 double start2d = rangeAxis.valueToJava2D(start, dataArea, 1320 plot.getRangeAxisEdge()); 1321 double end2d = rangeAxis.valueToJava2D(end, dataArea, 1322 plot.getRangeAxisEdge()); 1323 double low = Math.min(start2d, end2d); 1324 double high = Math.max(start2d, end2d); 1325 1326 PlotOrientation orientation = plot.getOrientation(); 1327 Rectangle2D rect = null; 1328 if (orientation == PlotOrientation.HORIZONTAL) { 1329 // clip left and right bounds to data area 1330 low = Math.max(low, dataArea.getMinX()); 1331 high = Math.min(high, dataArea.getMaxX()); 1332 rect = new Rectangle2D.Double(low, 1333 dataArea.getMinY(), high - low, 1334 dataArea.getHeight()); 1335 } else if (orientation == PlotOrientation.VERTICAL) { 1336 // clip top and bottom bounds to data area 1337 low = Math.max(low, dataArea.getMinY()); 1338 high = Math.min(high, dataArea.getMaxY()); 1339 rect = new Rectangle2D.Double(dataArea.getMinX(), 1340 low, dataArea.getWidth(), 1341 high - low); 1342 } 1343 1344 final Composite originalComposite = g2.getComposite(); 1345 g2.setComposite(AlphaComposite.getInstance( 1346 AlphaComposite.SRC_OVER, marker.getAlpha())); 1347 Paint p = marker.getPaint(); 1348 if (p instanceof GradientPaint) { 1349 GradientPaint gp = (GradientPaint) p; 1350 GradientPaintTransformer t = im.getGradientPaintTransformer(); 1351 if (t != null) { 1352 gp = t.transform(gp, rect); 1353 } 1354 g2.setPaint(gp); 1355 } else { 1356 g2.setPaint(p); 1357 } 1358 g2.fill(rect); 1359 1360 // now draw the outlines, if visible... 1361 if (im.getOutlinePaint() != null && im.getOutlineStroke() != null) { 1362 if (orientation == PlotOrientation.VERTICAL) { 1363 Line2D line = new Line2D.Double(); 1364 double x0 = dataArea.getMinX(); 1365 double x1 = dataArea.getMaxX(); 1366 g2.setPaint(im.getOutlinePaint()); 1367 g2.setStroke(im.getOutlineStroke()); 1368 if (range.contains(start)) { 1369 line.setLine(x0, start2d, x1, start2d); 1370 g2.draw(line); 1371 } 1372 if (range.contains(end)) { 1373 line.setLine(x0, end2d, x1, end2d); 1374 g2.draw(line); 1375 } 1376 } else { // PlotOrientation.HORIZONTAL 1377 Line2D line = new Line2D.Double(); 1378 double y0 = dataArea.getMinY(); 1379 double y1 = dataArea.getMaxY(); 1380 g2.setPaint(im.getOutlinePaint()); 1381 g2.setStroke(im.getOutlineStroke()); 1382 if (range.contains(start)) { 1383 line.setLine(start2d, y0, start2d, y1); 1384 g2.draw(line); 1385 } 1386 if (range.contains(end)) { 1387 line.setLine(end2d, y0, end2d, y1); 1388 g2.draw(line); 1389 } 1390 } 1391 } 1392 1393 String label = marker.getLabel(); 1394 RectangleAnchor anchor = marker.getLabelAnchor(); 1395 if (label != null) { 1396 Font labelFont = marker.getLabelFont(); 1397 g2.setFont(labelFont); 1398 Point2D coords = calculateRangeMarkerTextAnchorPoint( 1399 g2, orientation, dataArea, rect, 1400 marker.getLabelOffset(), marker.getLabelOffsetType(), 1401 anchor); 1402 Rectangle2D r = TextUtils.calcAlignedStringBounds(label, 1403 g2, (float) coords.getX(), (float) coords.getY(), 1404 marker.getLabelTextAnchor()); 1405 g2.setPaint(marker.getLabelBackgroundColor()); 1406 g2.fill(r); 1407 g2.setPaint(marker.getLabelPaint()); 1408 TextUtils.drawAlignedString(label, g2, 1409 (float) coords.getX(), (float) coords.getY(), 1410 marker.getLabelTextAnchor()); 1411 } 1412 g2.setComposite(originalComposite); 1413 } 1414 } 1415 1416 /** 1417 * Calculates the (x, y) coordinates for drawing a marker label. 1418 * 1419 * @param g2 the graphics device. 1420 * @param orientation the plot orientation. 1421 * @param dataArea the data area. 1422 * @param markerArea the marker area. 1423 * @param markerOffset the marker offset. 1424 * @param labelOffsetForRange ?? 1425 * @param anchor the label anchor. 1426 * 1427 * @return The coordinates for drawing the marker label. 1428 */ 1429 private Point2D calculateRangeMarkerTextAnchorPoint(Graphics2D g2, 1430 PlotOrientation orientation, Rectangle2D dataArea, 1431 Rectangle2D markerArea, RectangleInsets markerOffset, 1432 LengthAdjustmentType labelOffsetForRange, RectangleAnchor anchor) { 1433 1434 Rectangle2D anchorRect = null; 1435 if (orientation == PlotOrientation.HORIZONTAL) { 1436 anchorRect = markerOffset.createAdjustedRectangle(markerArea, 1437 labelOffsetForRange, LengthAdjustmentType.CONTRACT); 1438 } 1439 else if (orientation == PlotOrientation.VERTICAL) { 1440 anchorRect = markerOffset.createAdjustedRectangle(markerArea, 1441 LengthAdjustmentType.CONTRACT, labelOffsetForRange); 1442 } 1443 return anchor.getAnchorPoint(anchorRect); 1444 1445 } 1446 1447 /** 1448 * Returns a clone of the renderer. 1449 * 1450 * @return A clone. 1451 * 1452 * @throws CloneNotSupportedException if the renderer does not support 1453 * cloning. 1454 */ 1455 @Override 1456 protected Object clone() throws CloneNotSupportedException { 1457 AbstractXYItemRenderer clone = (AbstractXYItemRenderer) super.clone(); 1458 // 'plot' : just retain reference, not a deep copy 1459 1460 clone.itemLabelGeneratorMap = CloneUtils.cloneMapValues( 1461 this.itemLabelGeneratorMap); 1462 if (this.defaultItemLabelGenerator != null 1463 && this.defaultItemLabelGenerator instanceof PublicCloneable) { 1464 PublicCloneable pc = (PublicCloneable) this.defaultItemLabelGenerator; 1465 clone.defaultItemLabelGenerator = (XYItemLabelGenerator) pc.clone(); 1466 } 1467 1468 clone.toolTipGeneratorMap = CloneUtils.cloneMapValues( 1469 this.toolTipGeneratorMap); 1470 if (this.defaultToolTipGenerator != null 1471 && this.defaultToolTipGenerator instanceof PublicCloneable) { 1472 PublicCloneable pc = (PublicCloneable) this.defaultToolTipGenerator; 1473 clone.defaultToolTipGenerator = (XYToolTipGenerator) pc.clone(); 1474 } 1475 1476 if (this.legendItemLabelGenerator instanceof PublicCloneable) { 1477 clone.legendItemLabelGenerator = (XYSeriesLabelGenerator) 1478 ObjectUtils.clone(this.legendItemLabelGenerator); 1479 } 1480 if (this.legendItemToolTipGenerator instanceof PublicCloneable) { 1481 clone.legendItemToolTipGenerator = (XYSeriesLabelGenerator) 1482 ObjectUtils.clone(this.legendItemToolTipGenerator); 1483 } 1484 if (this.legendItemURLGenerator instanceof PublicCloneable) { 1485 clone.legendItemURLGenerator = (XYSeriesLabelGenerator) 1486 ObjectUtils.clone(this.legendItemURLGenerator); 1487 } 1488 1489 clone.foregroundAnnotations = (List) ObjectUtils.deepClone( 1490 this.foregroundAnnotations); 1491 clone.backgroundAnnotations = (List) ObjectUtils.deepClone( 1492 this.backgroundAnnotations); 1493 1494 return clone; 1495 } 1496 1497 /** 1498 * Tests this renderer for equality with another object. 1499 * 1500 * @param obj the object ({@code null} permitted). 1501 * 1502 * @return {@code true} or {@code false}. 1503 */ 1504 @Override 1505 public boolean equals(Object obj) { 1506 if (obj == this) { 1507 return true; 1508 } 1509 if (!(obj instanceof AbstractXYItemRenderer)) { 1510 return false; 1511 } 1512 AbstractXYItemRenderer that = (AbstractXYItemRenderer) obj; 1513 if (!this.itemLabelGeneratorMap.equals(that.itemLabelGeneratorMap)) { 1514 return false; 1515 } 1516 if (!ObjectUtils.equal(this.defaultItemLabelGenerator, 1517 that.defaultItemLabelGenerator)) { 1518 return false; 1519 } 1520 if (!this.toolTipGeneratorMap.equals(that.toolTipGeneratorMap)) { 1521 return false; 1522 } 1523 if (!ObjectUtils.equal(this.defaultToolTipGenerator, 1524 that.defaultToolTipGenerator)) { 1525 return false; 1526 } 1527 if (!ObjectUtils.equal(this.urlGenerator, that.urlGenerator)) { 1528 return false; 1529 } 1530 if (!this.foregroundAnnotations.equals(that.foregroundAnnotations)) { 1531 return false; 1532 } 1533 if (!this.backgroundAnnotations.equals(that.backgroundAnnotations)) { 1534 return false; 1535 } 1536 if (!ObjectUtils.equal(this.legendItemLabelGenerator, 1537 that.legendItemLabelGenerator)) { 1538 return false; 1539 } 1540 if (!ObjectUtils.equal(this.legendItemToolTipGenerator, 1541 that.legendItemToolTipGenerator)) { 1542 return false; 1543 } 1544 if (!ObjectUtils.equal(this.legendItemURLGenerator, 1545 that.legendItemURLGenerator)) { 1546 return false; 1547 } 1548 return super.equals(obj); 1549 } 1550 1551 /** 1552 * Returns the drawing supplier from the plot. 1553 * 1554 * @return The drawing supplier (possibly {@code null}). 1555 */ 1556 @Override 1557 public DrawingSupplier getDrawingSupplier() { 1558 DrawingSupplier result = null; 1559 XYPlot p = getPlot(); 1560 if (p != null) { 1561 result = p.getDrawingSupplier(); 1562 } 1563 return result; 1564 } 1565 1566 /** 1567 * Considers the current (x, y) coordinate and updates the crosshair point 1568 * if it meets the criteria (usually means the (x, y) coordinate is the 1569 * closest to the anchor point so far). 1570 * 1571 * @param crosshairState the crosshair state ({@code null} permitted, 1572 * but the method does nothing in that case). 1573 * @param x the x-value (in data space). 1574 * @param y the y-value (in data space). 1575 * @param datasetIndex the index of the dataset for the point. 1576 * @param transX the x-value translated to Java2D space. 1577 * @param transY the y-value translated to Java2D space. 1578 * @param orientation the plot orientation ({@code null} not 1579 * permitted). 1580 * 1581 * @since 1.0.20 1582 */ 1583 protected void updateCrosshairValues(CrosshairState crosshairState, 1584 double x, double y, int datasetIndex, 1585 double transX, double transY, PlotOrientation orientation) { 1586 1587 Args.nullNotPermitted(orientation, "orientation"); 1588 if (crosshairState != null) { 1589 // do we need to update the crosshair values? 1590 if (this.plot.isDomainCrosshairLockedOnData()) { 1591 if (this.plot.isRangeCrosshairLockedOnData()) { 1592 // both axes 1593 crosshairState.updateCrosshairPoint(x, y, datasetIndex, 1594 transX, transY, orientation); 1595 } 1596 else { 1597 // just the domain axis... 1598 crosshairState.updateCrosshairX(x, transX, datasetIndex); 1599 } 1600 } 1601 else { 1602 if (this.plot.isRangeCrosshairLockedOnData()) { 1603 // just the range axis... 1604 crosshairState.updateCrosshairY(y, transY, datasetIndex); 1605 } 1606 } 1607 } 1608 1609 } 1610 1611 /** 1612 * Draws an item label. 1613 * 1614 * @param g2 the graphics device. 1615 * @param orientation the orientation. 1616 * @param dataset the dataset. 1617 * @param series the series index (zero-based). 1618 * @param item the item index (zero-based). 1619 * @param x the x coordinate (in Java2D space). 1620 * @param y the y coordinate (in Java2D space). 1621 * @param negative indicates a negative value (which affects the item 1622 * label position). 1623 */ 1624 protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation, 1625 XYDataset dataset, int series, int item, double x, double y, 1626 boolean negative) { 1627 1628 XYItemLabelGenerator generator = getItemLabelGenerator(series, item); 1629 if (generator != null) { 1630 Font labelFont = getItemLabelFont(series, item); 1631 Paint paint = getItemLabelPaint(series, item); 1632 g2.setFont(labelFont); 1633 g2.setPaint(paint); 1634 String label = generator.generateLabel(dataset, series, item); 1635 1636 // get the label position.. 1637 ItemLabelPosition position; 1638 if (!negative) { 1639 position = getPositiveItemLabelPosition(series, item); 1640 } 1641 else { 1642 position = getNegativeItemLabelPosition(series, item); 1643 } 1644 1645 // work out the label anchor point... 1646 Point2D anchorPoint = calculateLabelAnchorPoint( 1647 position.getItemLabelAnchor(), x, y, orientation); 1648 TextUtils.drawRotatedString(label, g2, 1649 (float) anchorPoint.getX(), (float) anchorPoint.getY(), 1650 position.getTextAnchor(), position.getAngle(), 1651 position.getRotationAnchor()); 1652 } 1653 1654 } 1655 1656 /** 1657 * Draws all the annotations for the specified layer. 1658 * 1659 * @param g2 the graphics device. 1660 * @param dataArea the data area. 1661 * @param domainAxis the domain axis. 1662 * @param rangeAxis the range axis. 1663 * @param layer the layer ({@code null} not permitted). 1664 * @param info the plot rendering info. 1665 */ 1666 @Override 1667 public void drawAnnotations(Graphics2D g2, Rectangle2D dataArea, 1668 ValueAxis domainAxis, ValueAxis rangeAxis, Layer layer, 1669 PlotRenderingInfo info) { 1670 1671 Iterator iterator = null; 1672 if (layer.equals(Layer.FOREGROUND)) { 1673 iterator = this.foregroundAnnotations.iterator(); 1674 } 1675 else if (layer.equals(Layer.BACKGROUND)) { 1676 iterator = this.backgroundAnnotations.iterator(); 1677 } 1678 else { 1679 // should not get here 1680 throw new RuntimeException("Unknown layer."); 1681 } 1682 while (iterator.hasNext()) { 1683 XYAnnotation annotation = (XYAnnotation) iterator.next(); 1684 int index = this.plot.getIndexOf(this); 1685 annotation.draw(g2, this.plot, dataArea, domainAxis, rangeAxis, 1686 index, info); 1687 } 1688 1689 } 1690 1691 /** 1692 * Adds an entity to the collection. Note the the {@code entityX} and 1693 * {@code entityY} coordinates are in Java2D space, should already be 1694 * adjusted for the plot orientation, and will only be used if 1695 * {@code hotspot} is {@code null}. 1696 * 1697 * @param entities the entity collection being populated. 1698 * @param hotspot the entity area (if {@code null} a default will be 1699 * used). 1700 * @param dataset the dataset. 1701 * @param series the series. 1702 * @param item the item. 1703 * @param entityX the entity x-coordinate (in Java2D space, only used if 1704 * {@code hotspot} is {@code null}). 1705 * @param entityY the entity y-coordinate (in Java2D space, only used if 1706 * {@code hotspot} is {@code null}). 1707 */ 1708 protected void addEntity(EntityCollection entities, Shape hotspot, 1709 XYDataset dataset, int series, int item, double entityX, 1710 double entityY) { 1711 1712 if (!getItemCreateEntity(series, item)) { 1713 return; 1714 } 1715 1716 // if not hotspot is provided, we create a default based on the 1717 // provided data coordinates (which are already in Java2D space) 1718 if (hotspot == null) { 1719 double r = getDefaultEntityRadius(); 1720 double w = r * 2; 1721 hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w); 1722 } 1723 String tip = null; 1724 XYToolTipGenerator generator = getToolTipGenerator(series, item); 1725 if (generator != null) { 1726 tip = generator.generateToolTip(dataset, series, item); 1727 } 1728 String url = null; 1729 if (getURLGenerator() != null) { 1730 url = getURLGenerator().generateURL(dataset, series, item); 1731 } 1732 XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, 1733 tip, url); 1734 entities.add(entity); 1735 } 1736 1737 /** 1738 * Utility method delegating to {@link GeneralPath#moveTo} taking double as 1739 * parameters. 1740 * 1741 * @param hotspot the region under construction ({@code null} not 1742 * permitted); 1743 * @param x the x coordinate; 1744 * @param y the y coordinate; 1745 * 1746 * @since 1.0.14 1747 */ 1748 protected static void moveTo(GeneralPath hotspot, double x, double y) { 1749 hotspot.moveTo((float) x, (float) y); 1750 } 1751 1752 /** 1753 * Utility method delegating to {@link GeneralPath#lineTo} taking double as 1754 * parameters. 1755 * 1756 * @param hotspot the region under construction ({@code null} not 1757 * permitted); 1758 * @param x the x coordinate; 1759 * @param y the y coordinate; 1760 * 1761 * @since 1.0.14 1762 */ 1763 protected static void lineTo(GeneralPath hotspot, double x, double y) { 1764 hotspot.lineTo((float) x, (float) y); 1765 } 1766 1767}