001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------ 028 * XYBarRenderer.java 029 * ------------------ 030 * (C) Copyright 2001-2017, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Richard Atkinson; 034 * Christian W. Zuckschwerdt; 035 * Bill Kelemen; 036 * Marc van Glabbeek (bug 1775452); 037 * Richard West, Advanced Micro Devices, Inc.; 038 * 039 * Changes 040 * ------- 041 * 13-Dec-2001 : Version 1, makes VerticalXYBarPlot class redundant (DG); 042 * 23-Jan-2002 : Added DrawInfo parameter to drawItem() method (DG); 043 * 09-Apr-2002 : Removed the translated zero from the drawItem method. Override 044 * the initialise() method to calculate it (DG); 045 * 24-May-2002 : Incorporated tooltips into chart entities (DG); 046 * 25-Jun-2002 : Removed redundant import (DG); 047 * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML 048 * image maps (RA); 049 * 25-Mar-2003 : Implemented Serializable (DG); 050 * 01-May-2003 : Modified drawItem() method signature (DG); 051 * 30-Jul-2003 : Modified entity constructor (CZ); 052 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG); 053 * 24-Aug-2003 : Added null checks in drawItem (BK); 054 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 055 * 07-Oct-2003 : Added renderer state (DG); 056 * 05-Dec-2003 : Changed call to obtain outline paint (DG); 057 * 10-Feb-2004 : Added state class, updated drawItem() method to make 058 * cut-and-paste overriding easier, and replaced property change 059 * with RendererChangeEvent (DG); 060 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); 061 * 26-Apr-2004 : Added gradient paint transformer (DG); 062 * 19-May-2004 : Fixed bug (879709) with bar zero value for secondary axis (DG); 063 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 064 * getYValue() (DG); 065 * 01-Sep-2004 : Added a flag to control whether or not the bar outlines are 066 * drawn (DG); 067 * 03-Sep-2004 : Added option to use y-interval from dataset to determine the 068 * length of the bars (DG); 069 * 08-Sep-2004 : Added equals() method and updated clone() method (DG); 070 * 26-Jan-2005 : Added override for getLegendItem() method (DG); 071 * 20-Apr-2005 : Use generators for label tooltips and URLs (DG); 072 * 19-May-2005 : Added minimal item label implementation - needs improving (DG); 073 * 14-Oct-2005 : Fixed rendering problem with inverted axes (DG); 074 * ------------- JFREECHART 1.0.x --------------------------------------------- 075 * 21-Jun-2006 : Improved item label handling - see bug 1501768 (DG); 076 * 24-Aug-2006 : Added crosshair support (DG); 077 * 13-Dec-2006 : Updated getLegendItems() to return gradient paint 078 * transformer (DG); 079 * 02-Feb-2007 : Changed setUseYInterval() to only notify when the flag 080 * changes (DG); 081 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG); 082 * 09-Feb-2007 : Updated getLegendItem() to observe drawBarOutline flag (DG); 083 * 05-Mar-2007 : Applied patch 1671126 by Sergei Ivanov, to fix rendering with 084 * LogarithmicAxis (DG); 085 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG); 086 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG); 087 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 088 * 15-Jun-2007 : Changed default for drawBarOutline to false (DG); 089 * 26-Sep-2007 : Fixed bug 1775452, problem with bar margins for inverted 090 * axes, thanks to Marc van Glabbeek (DG); 091 * 12-Nov-2007 : Fixed NPE in drawItemLabel() method, thanks to Richard West 092 * (see patch 1827829) (DG); 093 * 17-Jun-2008 : Apply legend font and paint attributes (DG); 094 * 19-Jun-2008 : Added findRangeBounds() method override to fix bug in default 095 * axis range (DG); 096 * 24-Jun-2008 : Added new barPainter mechanism (DG); 097 * 03-Feb-2009 : Added defaultShadowsVisible flag (DG); 098 * 05-Feb-2009 : Added barAlignmentFactor (DG); 099 * 10-May-2012 : Fix findDomainBounds() and findRangeBounds() to account for 100 * non-visible series (DG); 101 * 03-Jul-2013 : Use ParamChecks (DG); 102 * 24-Aug-2014 : Add begin/endElementGroup() (DG); 103 * 18-Feb-2017 : Updates for crosshairs (bug #36) (DG); 104 * 105 */ 106 107package org.jfree.chart.renderer.xy; 108 109import java.awt.Font; 110import java.awt.Graphics2D; 111import java.awt.Paint; 112import java.awt.Shape; 113import java.awt.Stroke; 114import java.awt.geom.Point2D; 115import java.awt.geom.Rectangle2D; 116import java.io.IOException; 117import java.io.ObjectInputStream; 118import java.io.ObjectOutputStream; 119import java.io.Serializable; 120 121import org.jfree.chart.LegendItem; 122import org.jfree.chart.axis.ValueAxis; 123import org.jfree.chart.entity.EntityCollection; 124import org.jfree.chart.event.RendererChangeEvent; 125import org.jfree.chart.labels.ItemLabelAnchor; 126import org.jfree.chart.labels.ItemLabelPosition; 127import org.jfree.chart.labels.XYItemLabelGenerator; 128import org.jfree.chart.labels.XYSeriesLabelGenerator; 129import org.jfree.chart.plot.CrosshairState; 130import org.jfree.chart.plot.PlotOrientation; 131import org.jfree.chart.plot.PlotRenderingInfo; 132import org.jfree.chart.plot.XYPlot; 133import org.jfree.chart.text.TextUtils; 134import org.jfree.chart.ui.GradientPaintTransformer; 135import org.jfree.chart.ui.RectangleEdge; 136import org.jfree.chart.ui.StandardGradientPaintTransformer; 137import org.jfree.chart.util.ObjectUtils; 138import org.jfree.chart.util.Args; 139import org.jfree.chart.util.PublicCloneable; 140import org.jfree.chart.util.SerialUtils; 141import org.jfree.chart.util.ShapeUtils; 142import org.jfree.data.Range; 143import org.jfree.data.xy.IntervalXYDataset; 144import org.jfree.data.xy.XYDataset; 145 146/** 147 * A renderer that draws bars on an {@link XYPlot} (requires an 148 * {@link IntervalXYDataset}). The example shown here is generated by the 149 * {@code XYBarChartDemo1.java} program included in the JFreeChart 150 * demo collection: 151 * <br><br> 152 * <img src="../../../../../images/XYBarRendererSample.png" 153 * alt="XYBarRendererSample.png"> 154 */ 155public class XYBarRenderer extends AbstractXYItemRenderer 156 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable { 157 158 /** For serialization. */ 159 private static final long serialVersionUID = 770559577251370036L; 160 161 /** 162 * The default bar painter assigned to each new instance of this renderer. 163 * 164 * @since 1.0.11 165 */ 166 private static XYBarPainter defaultBarPainter = new GradientXYBarPainter(); 167 168 /** 169 * Returns the default bar painter. 170 * 171 * @return The default bar painter. 172 * 173 * @since 1.0.11 174 */ 175 public static XYBarPainter getDefaultBarPainter() { 176 return XYBarRenderer.defaultBarPainter; 177 } 178 179 /** 180 * Sets the default bar painter. 181 * 182 * @param painter the painter ({@code null} not permitted). 183 * 184 * @since 1.0.11 185 */ 186 public static void setDefaultBarPainter(XYBarPainter painter) { 187 Args.nullNotPermitted(painter, "painter"); 188 XYBarRenderer.defaultBarPainter = painter; 189 } 190 191 /** 192 * The default value for the initialisation of the shadowsVisible flag. 193 */ 194 private static boolean defaultShadowsVisible = true; 195 196 /** 197 * Returns the default value for the {@code shadowsVisible} flag. 198 * 199 * @return A boolean. 200 * 201 * @see #setDefaultShadowsVisible(boolean) 202 * 203 * @since 1.0.13 204 */ 205 public static boolean getDefaultShadowsVisible() { 206 return XYBarRenderer.defaultShadowsVisible; 207 } 208 209 /** 210 * Sets the default value for the shadows visible flag. 211 * 212 * @param visible the new value for the default. 213 * 214 * @see #getDefaultShadowsVisible() 215 * 216 * @since 1.0.13 217 */ 218 public static void setDefaultShadowsVisible(boolean visible) { 219 XYBarRenderer.defaultShadowsVisible = visible; 220 } 221 222 /** 223 * The state class used by this renderer. 224 */ 225 protected class XYBarRendererState extends XYItemRendererState { 226 227 /** Base for bars against the range axis, in Java 2D space. */ 228 private double g2Base; 229 230 /** 231 * Creates a new state object. 232 * 233 * @param info the plot rendering info. 234 */ 235 public XYBarRendererState(PlotRenderingInfo info) { 236 super(info); 237 } 238 239 /** 240 * Returns the base (range) value in Java 2D space. 241 * 242 * @return The base value. 243 */ 244 public double getG2Base() { 245 return this.g2Base; 246 } 247 248 /** 249 * Sets the range axis base in Java2D space. 250 * 251 * @param value the value. 252 */ 253 public void setG2Base(double value) { 254 this.g2Base = value; 255 } 256 } 257 258 /** The default base value for the bars. */ 259 private double base; 260 261 /** 262 * A flag that controls whether the bars use the y-interval supplied by the 263 * dataset. 264 */ 265 private boolean useYInterval; 266 267 /** Percentage margin (to reduce the width of bars). */ 268 private double margin; 269 270 /** A flag that controls whether or not bar outlines are drawn. */ 271 private boolean drawBarOutline; 272 273 /** 274 * An optional class used to transform gradient paint objects to fit each 275 * bar. 276 */ 277 private GradientPaintTransformer gradientPaintTransformer; 278 279 /** 280 * The shape used to represent a bar in each legend item (this should never 281 * be {@code null}). 282 */ 283 private transient Shape legendBar; 284 285 /** 286 * The fallback position if a positive item label doesn't fit inside the 287 * bar. 288 */ 289 private ItemLabelPosition positiveItemLabelPositionFallback; 290 291 /** 292 * The fallback position if a negative item label doesn't fit inside the 293 * bar. 294 */ 295 private ItemLabelPosition negativeItemLabelPositionFallback; 296 297 /** 298 * The bar painter (never {@code null}). 299 * 300 * @since 1.0.11 301 */ 302 private XYBarPainter barPainter; 303 304 /** 305 * The flag that controls whether or not shadows are drawn for the bars. 306 * 307 * @since 1.0.11 308 */ 309 private boolean shadowsVisible; 310 311 /** 312 * The x-offset for the shadow effect. 313 * 314 * @since 1.0.11 315 */ 316 private double shadowXOffset; 317 318 /** 319 * The y-offset for the shadow effect. 320 * 321 * @since 1.0.11 322 */ 323 private double shadowYOffset; 324 325 /** 326 * A factor used to align the bars about the x-value. 327 * 328 * @since 1.0.13 329 */ 330 private double barAlignmentFactor; 331 332 /** 333 * The default constructor. 334 */ 335 public XYBarRenderer() { 336 this(0.0); 337 } 338 339 /** 340 * Constructs a new renderer. 341 * 342 * @param margin the percentage amount to trim from the width of each bar. 343 */ 344 public XYBarRenderer(double margin) { 345 super(); 346 this.margin = margin; 347 this.base = 0.0; 348 this.useYInterval = false; 349 this.gradientPaintTransformer = new StandardGradientPaintTransformer(); 350 this.drawBarOutline = false; 351 this.legendBar = new Rectangle2D.Double(-3.0, -5.0, 6.0, 10.0); 352 this.barPainter = getDefaultBarPainter(); 353 this.shadowsVisible = getDefaultShadowsVisible(); 354 this.shadowXOffset = 4.0; 355 this.shadowYOffset = 4.0; 356 this.barAlignmentFactor = -1.0; 357 } 358 359 /** 360 * Returns the base value for the bars. 361 * 362 * @return The base value for the bars. 363 * 364 * @see #setBase(double) 365 */ 366 public double getBase() { 367 return this.base; 368 } 369 370 /** 371 * Sets the base value for the bars and sends a {@link RendererChangeEvent} 372 * to all registered listeners. The base value is not used if the dataset's 373 * y-interval is being used to determine the bar length. 374 * 375 * @param base the new base value. 376 * 377 * @see #getBase() 378 * @see #getUseYInterval() 379 */ 380 public void setBase(double base) { 381 this.base = base; 382 fireChangeEvent(); 383 } 384 385 /** 386 * Returns a flag that determines whether the y-interval from the dataset is 387 * used to calculate the length of each bar. 388 * 389 * @return A boolean. 390 * 391 * @see #setUseYInterval(boolean) 392 */ 393 public boolean getUseYInterval() { 394 return this.useYInterval; 395 } 396 397 /** 398 * Sets the flag that determines whether the y-interval from the dataset is 399 * used to calculate the length of each bar, and sends a 400 * {@link RendererChangeEvent} to all registered listeners. 401 * 402 * @param use the flag. 403 * 404 * @see #getUseYInterval() 405 */ 406 public void setUseYInterval(boolean use) { 407 if (this.useYInterval != use) { 408 this.useYInterval = use; 409 fireChangeEvent(); 410 } 411 } 412 413 /** 414 * Returns the margin which is a percentage amount by which the bars are 415 * trimmed. 416 * 417 * @return The margin. 418 * 419 * @see #setMargin(double) 420 */ 421 public double getMargin() { 422 return this.margin; 423 } 424 425 /** 426 * Sets the percentage amount by which the bars are trimmed and sends a 427 * {@link RendererChangeEvent} to all registered listeners. 428 * 429 * @param margin the new margin. 430 * 431 * @see #getMargin() 432 */ 433 public void setMargin(double margin) { 434 this.margin = margin; 435 fireChangeEvent(); 436 } 437 438 /** 439 * Returns a flag that controls whether or not bar outlines are drawn. 440 * 441 * @return A boolean. 442 * 443 * @see #setDrawBarOutline(boolean) 444 */ 445 public boolean isDrawBarOutline() { 446 return this.drawBarOutline; 447 } 448 449 /** 450 * Sets the flag that controls whether or not bar outlines are drawn and 451 * sends a {@link RendererChangeEvent} to all registered listeners. 452 * 453 * @param draw the flag. 454 * 455 * @see #isDrawBarOutline() 456 */ 457 public void setDrawBarOutline(boolean draw) { 458 this.drawBarOutline = draw; 459 fireChangeEvent(); 460 } 461 462 /** 463 * Returns the gradient paint transformer (an object used to transform 464 * gradient paint objects to fit each bar). 465 * 466 * @return A transformer ({@code null} possible). 467 * 468 * @see #setGradientPaintTransformer(GradientPaintTransformer) 469 */ 470 public GradientPaintTransformer getGradientPaintTransformer() { 471 return this.gradientPaintTransformer; 472 } 473 474 /** 475 * Sets the gradient paint transformer and sends a 476 * {@link RendererChangeEvent} to all registered listeners. 477 * 478 * @param transformer the transformer ({@code null} permitted). 479 * 480 * @see #getGradientPaintTransformer() 481 */ 482 public void setGradientPaintTransformer( 483 GradientPaintTransformer transformer) { 484 this.gradientPaintTransformer = transformer; 485 fireChangeEvent(); 486 } 487 488 /** 489 * Returns the shape used to represent bars in each legend item. 490 * 491 * @return The shape used to represent bars in each legend item (never 492 * {@code null}). 493 * 494 * @see #setLegendBar(Shape) 495 */ 496 public Shape getLegendBar() { 497 return this.legendBar; 498 } 499 500 /** 501 * Sets the shape used to represent bars in each legend item and sends a 502 * {@link RendererChangeEvent} to all registered listeners. 503 * 504 * @param bar the bar shape ({@code null} not permitted). 505 * 506 * @see #getLegendBar() 507 */ 508 public void setLegendBar(Shape bar) { 509 Args.nullNotPermitted(bar, "bar"); 510 this.legendBar = bar; 511 fireChangeEvent(); 512 } 513 514 /** 515 * Returns the fallback position for positive item labels that don't fit 516 * within a bar. 517 * 518 * @return The fallback position ({@code null} possible). 519 * 520 * @see #setPositiveItemLabelPositionFallback(ItemLabelPosition) 521 * @since 1.0.2 522 */ 523 public ItemLabelPosition getPositiveItemLabelPositionFallback() { 524 return this.positiveItemLabelPositionFallback; 525 } 526 527 /** 528 * Sets the fallback position for positive item labels that don't fit 529 * within a bar, and sends a {@link RendererChangeEvent} to all registered 530 * listeners. 531 * 532 * @param position the position ({@code null} permitted). 533 * 534 * @see #getPositiveItemLabelPositionFallback() 535 * @since 1.0.2 536 */ 537 public void setPositiveItemLabelPositionFallback( 538 ItemLabelPosition position) { 539 this.positiveItemLabelPositionFallback = position; 540 fireChangeEvent(); 541 } 542 543 /** 544 * Returns the fallback position for negative item labels that don't fit 545 * within a bar. 546 * 547 * @return The fallback position ({@code null} possible). 548 * 549 * @see #setNegativeItemLabelPositionFallback(ItemLabelPosition) 550 * @since 1.0.2 551 */ 552 public ItemLabelPosition getNegativeItemLabelPositionFallback() { 553 return this.negativeItemLabelPositionFallback; 554 } 555 556 /** 557 * Sets the fallback position for negative item labels that don't fit 558 * within a bar, and sends a {@link RendererChangeEvent} to all registered 559 * listeners. 560 * 561 * @param position the position ({@code null} permitted). 562 * 563 * @see #getNegativeItemLabelPositionFallback() 564 * @since 1.0.2 565 */ 566 public void setNegativeItemLabelPositionFallback( 567 ItemLabelPosition position) { 568 this.negativeItemLabelPositionFallback = position; 569 fireChangeEvent(); 570 } 571 572 /** 573 * Returns the bar painter. 574 * 575 * @return The bar painter (never {@code null}). 576 * 577 * @since 1.0.11 578 */ 579 public XYBarPainter getBarPainter() { 580 return this.barPainter; 581 } 582 583 /** 584 * Sets the bar painter and sends a {@link RendererChangeEvent} to all 585 * registered listeners. 586 * 587 * @param painter the painter ({@code null} not permitted). 588 * 589 * @since 1.0.11 590 */ 591 public void setBarPainter(XYBarPainter painter) { 592 Args.nullNotPermitted(painter, "painter"); 593 this.barPainter = painter; 594 fireChangeEvent(); 595 } 596 597 /** 598 * Returns the flag that controls whether or not shadows are drawn for 599 * the bars. 600 * 601 * @return A boolean. 602 * 603 * @since 1.0.11 604 */ 605 public boolean getShadowsVisible() { 606 return this.shadowsVisible; 607 } 608 609 /** 610 * Sets the flag that controls whether or not the renderer 611 * draws shadows for the bars, and sends a 612 * {@link RendererChangeEvent} to all registered listeners. 613 * 614 * @param visible the new flag value. 615 * 616 * @since 1.0.11 617 */ 618 public void setShadowVisible(boolean visible) { 619 this.shadowsVisible = visible; 620 fireChangeEvent(); 621 } 622 623 /** 624 * Returns the shadow x-offset. 625 * 626 * @return The shadow x-offset. 627 * 628 * @since 1.0.11 629 */ 630 public double getShadowXOffset() { 631 return this.shadowXOffset; 632 } 633 634 /** 635 * Sets the x-offset for the bar shadow and sends a 636 * {@link RendererChangeEvent} to all registered listeners. 637 * 638 * @param offset the offset. 639 * 640 * @since 1.0.11 641 */ 642 public void setShadowXOffset(double offset) { 643 this.shadowXOffset = offset; 644 fireChangeEvent(); 645 } 646 647 /** 648 * Returns the shadow y-offset. 649 * 650 * @return The shadow y-offset. 651 * 652 * @since 1.0.11 653 */ 654 public double getShadowYOffset() { 655 return this.shadowYOffset; 656 } 657 658 /** 659 * Sets the y-offset for the bar shadow and sends a 660 * {@link RendererChangeEvent} to all registered listeners. 661 * 662 * @param offset the offset. 663 * 664 * @since 1.0.11 665 */ 666 public void setShadowYOffset(double offset) { 667 this.shadowYOffset = offset; 668 fireChangeEvent(); 669 } 670 671 /** 672 * Returns the bar alignment factor. 673 * 674 * @return The bar alignment factor. 675 * 676 * @since 1.0.13 677 */ 678 public double getBarAlignmentFactor() { 679 return this.barAlignmentFactor; 680 } 681 682 /** 683 * Sets the bar alignment factor and sends a {@link RendererChangeEvent} 684 * to all registered listeners. If the alignment factor is outside the 685 * range 0.0 to 1.0, no alignment will be performed by the renderer. 686 * 687 * @param factor the factor. 688 * 689 * @since 1.0.13 690 */ 691 public void setBarAlignmentFactor(double factor) { 692 this.barAlignmentFactor = factor; 693 fireChangeEvent(); 694 } 695 696 /** 697 * Initialises the renderer and returns a state object that should be 698 * passed to all subsequent calls to the drawItem() method. Here we 699 * calculate the Java2D y-coordinate for zero, since all the bars have 700 * their bases fixed at zero. 701 * 702 * @param g2 the graphics device. 703 * @param dataArea the area inside the axes. 704 * @param plot the plot. 705 * @param dataset the data. 706 * @param info an optional info collection object to return data back to 707 * the caller. 708 * 709 * @return A state object. 710 */ 711 @Override 712 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, 713 XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { 714 715 XYBarRendererState state = new XYBarRendererState(info); 716 ValueAxis rangeAxis = plot.getRangeAxisForDataset(plot.indexOf( 717 dataset)); 718 state.setG2Base(rangeAxis.valueToJava2D(this.base, dataArea, 719 plot.getRangeAxisEdge())); 720 return state; 721 722 } 723 724 /** 725 * Returns a default legend item for the specified series. Subclasses 726 * should override this method to generate customised items. 727 * 728 * @param datasetIndex the dataset index (zero-based). 729 * @param series the series index (zero-based). 730 * 731 * @return A legend item for the series. 732 */ 733 @Override 734 public LegendItem getLegendItem(int datasetIndex, int series) { 735 XYPlot xyplot = getPlot(); 736 if (xyplot == null) { 737 return null; 738 } 739 XYDataset dataset = xyplot.getDataset(datasetIndex); 740 if (dataset == null) { 741 return null; 742 } 743 LegendItem result; 744 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator(); 745 String label = lg.generateLabel(dataset, series); 746 String description = label; 747 String toolTipText = null; 748 if (getLegendItemToolTipGenerator() != null) { 749 toolTipText = getLegendItemToolTipGenerator().generateLabel( 750 dataset, series); 751 } 752 String urlText = null; 753 if (getLegendItemURLGenerator() != null) { 754 urlText = getLegendItemURLGenerator().generateLabel(dataset, 755 series); 756 } 757 Shape shape = this.legendBar; 758 Paint paint = lookupSeriesPaint(series); 759 Paint outlinePaint = lookupSeriesOutlinePaint(series); 760 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 761 if (this.drawBarOutline) { 762 result = new LegendItem(label, description, toolTipText, 763 urlText, shape, paint, outlineStroke, outlinePaint); 764 } 765 else { 766 result = new LegendItem(label, description, toolTipText, urlText, 767 shape, paint); 768 } 769 result.setLabelFont(lookupLegendTextFont(series)); 770 Paint labelPaint = lookupLegendTextPaint(series); 771 if (labelPaint != null) { 772 result.setLabelPaint(labelPaint); 773 } 774 result.setDataset(dataset); 775 result.setDatasetIndex(datasetIndex); 776 result.setSeriesKey(dataset.getSeriesKey(series)); 777 result.setSeriesIndex(series); 778 if (getGradientPaintTransformer() != null) { 779 result.setFillPaintTransformer(getGradientPaintTransformer()); 780 } 781 return result; 782 } 783 784 /** 785 * Draws the visual representation of a single data item. 786 * 787 * @param g2 the graphics device. 788 * @param state the renderer state. 789 * @param dataArea the area within which the plot is being drawn. 790 * @param info collects information about the drawing. 791 * @param plot the plot (can be used to obtain standard color 792 * information etc). 793 * @param domainAxis the domain axis. 794 * @param rangeAxis the range axis. 795 * @param dataset the dataset. 796 * @param series the series index (zero-based). 797 * @param item the item index (zero-based). 798 * @param crosshairState crosshair information for the plot 799 * ({@code null} permitted). 800 * @param pass the pass index. 801 */ 802 @Override 803 public void drawItem(Graphics2D g2, XYItemRendererState state, 804 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 805 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 806 int series, int item, CrosshairState crosshairState, int pass) { 807 808 if (!getItemVisible(series, item)) { 809 return; 810 } 811 IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset; 812 813 double value0; 814 double value1; 815 if (this.useYInterval) { 816 value0 = intervalDataset.getStartYValue(series, item); 817 value1 = intervalDataset.getEndYValue(series, item); 818 } else { 819 value0 = this.base; 820 value1 = intervalDataset.getYValue(series, item); 821 } 822 if (Double.isNaN(value0) || Double.isNaN(value1)) { 823 return; 824 } 825 if (value0 <= value1) { 826 if (!rangeAxis.getRange().intersects(value0, value1)) { 827 return; 828 } 829 } else { 830 if (!rangeAxis.getRange().intersects(value1, value0)) { 831 return; 832 } 833 } 834 835 double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, 836 plot.getRangeAxisEdge()); 837 double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, 838 plot.getRangeAxisEdge()); 839 double bottom = Math.min(translatedValue0, translatedValue1); 840 double top = Math.max(translatedValue0, translatedValue1); 841 842 double startX = intervalDataset.getStartXValue(series, item); 843 if (Double.isNaN(startX)) { 844 return; 845 } 846 double endX = intervalDataset.getEndXValue(series, item); 847 if (Double.isNaN(endX)) { 848 return; 849 } 850 if (startX <= endX) { 851 if (!domainAxis.getRange().intersects(startX, endX)) { 852 return; 853 } 854 } else { 855 if (!domainAxis.getRange().intersects(endX, startX)) { 856 return; 857 } 858 } 859 860 // is there an alignment adjustment to be made? 861 if (this.barAlignmentFactor >= 0.0 && this.barAlignmentFactor <= 1.0) { 862 double x = intervalDataset.getXValue(series, item); 863 double interval = endX - startX; 864 startX = x - interval * this.barAlignmentFactor; 865 endX = startX + interval; 866 } 867 868 RectangleEdge location = plot.getDomainAxisEdge(); 869 double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, 870 location); 871 double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, 872 location); 873 874 double translatedWidth = Math.max(1, Math.abs(translatedEndX 875 - translatedStartX)); 876 877 double left = Math.min(translatedStartX, translatedEndX); 878 if (getMargin() > 0.0) { 879 double cut = translatedWidth * getMargin(); 880 translatedWidth = translatedWidth - cut; 881 left = left + cut / 2; 882 } 883 884 Rectangle2D bar = null; 885 PlotOrientation orientation = plot.getOrientation(); 886 if (orientation.isHorizontal()) { 887 // clip left and right bounds to data area 888 bottom = Math.max(bottom, dataArea.getMinX()); 889 top = Math.min(top, dataArea.getMaxX()); 890 bar = new Rectangle2D.Double( 891 bottom, left, top - bottom, translatedWidth); 892 } else if (orientation.isVertical()) { 893 // clip top and bottom bounds to data area 894 bottom = Math.max(bottom, dataArea.getMinY()); 895 top = Math.min(top, dataArea.getMaxY()); 896 bar = new Rectangle2D.Double(left, bottom, translatedWidth, 897 top - bottom); 898 } 899 900 boolean positive = (value1 > 0.0); 901 boolean inverted = rangeAxis.isInverted(); 902 RectangleEdge barBase; 903 if (orientation.isHorizontal()) { 904 if (positive && inverted || !positive && !inverted) { 905 barBase = RectangleEdge.RIGHT; 906 } else { 907 barBase = RectangleEdge.LEFT; 908 } 909 } else { 910 if (positive && !inverted || !positive && inverted) { 911 barBase = RectangleEdge.BOTTOM; 912 } else { 913 barBase = RectangleEdge.TOP; 914 } 915 } 916 917 if (state.getElementHinting()) { 918 beginElementGroup(g2, dataset.getSeriesKey(series), item); 919 } 920 if (getShadowsVisible()) { 921 this.barPainter.paintBarShadow(g2, this, series, item, bar, barBase, 922 !this.useYInterval); 923 } 924 this.barPainter.paintBar(g2, this, series, item, bar, barBase); 925 if (state.getElementHinting()) { 926 endElementGroup(g2); 927 } 928 929 if (isItemLabelVisible(series, item)) { 930 XYItemLabelGenerator generator = getItemLabelGenerator(series, 931 item); 932 drawItemLabel(g2, dataset, series, item, plot, generator, bar, 933 value1 < 0.0); 934 } 935 936 // update the crosshair point 937 double x1 = (startX + endX) / 2.0; 938 double y1 = dataset.getYValue(series, item); 939 double transX1 = domainAxis.valueToJava2D(x1, dataArea, location); 940 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, 941 plot.getRangeAxisEdge()); 942 int datasetIndex = plot.indexOf(dataset); 943 updateCrosshairValues(crosshairState, x1, y1, datasetIndex, 944 transX1, transY1, plot.getOrientation()); 945 946 EntityCollection entities = state.getEntityCollection(); 947 if (entities != null) { 948 addEntity(entities, bar, dataset, series, item, 0.0, 0.0); 949 } 950 951 } 952 953 /** 954 * Draws an item label. This method is provided as an alternative to 955 * {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int, 956 * double, double, boolean)} so that the bar can be used to calculate the 957 * label anchor point. 958 * 959 * @param g2 the graphics device. 960 * @param dataset the dataset. 961 * @param series the series index. 962 * @param item the item index. 963 * @param plot the plot. 964 * @param generator the label generator ({@code null} permitted, in 965 * which case the method does nothing, just returns). 966 * @param bar the bar. 967 * @param negative a flag indicating a negative value. 968 */ 969 protected void drawItemLabel(Graphics2D g2, XYDataset dataset, 970 int series, int item, XYPlot plot, XYItemLabelGenerator generator, 971 Rectangle2D bar, boolean negative) { 972 973 if (generator == null) { 974 return; // nothing to do 975 } 976 String label = generator.generateLabel(dataset, series, item); 977 if (label == null) { 978 return; // nothing to do 979 } 980 981 Font labelFont = getItemLabelFont(series, item); 982 g2.setFont(labelFont); 983 Paint paint = getItemLabelPaint(series, item); 984 g2.setPaint(paint); 985 986 // find out where to place the label... 987 ItemLabelPosition position; 988 if (!negative) { 989 position = getPositiveItemLabelPosition(series, item); 990 } else { 991 position = getNegativeItemLabelPosition(series, item); 992 } 993 994 // work out the label anchor point... 995 Point2D anchorPoint = calculateLabelAnchorPoint( 996 position.getItemLabelAnchor(), bar, plot.getOrientation()); 997 998 if (isInternalAnchor(position.getItemLabelAnchor())) { 999 Shape bounds = TextUtils.calculateRotatedStringBounds(label, 1000 g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), 1001 position.getTextAnchor(), position.getAngle(), 1002 position.getRotationAnchor()); 1003 1004 if (bounds != null) { 1005 if (!bar.contains(bounds.getBounds2D())) { 1006 if (!negative) { 1007 position = getPositiveItemLabelPositionFallback(); 1008 } 1009 else { 1010 position = getNegativeItemLabelPositionFallback(); 1011 } 1012 if (position != null) { 1013 anchorPoint = calculateLabelAnchorPoint( 1014 position.getItemLabelAnchor(), bar, 1015 plot.getOrientation()); 1016 } 1017 } 1018 } 1019 1020 } 1021 1022 if (position != null) { 1023 TextUtils.drawRotatedString(label, g2, 1024 (float) anchorPoint.getX(), (float) anchorPoint.getY(), 1025 position.getTextAnchor(), position.getAngle(), 1026 position.getRotationAnchor()); 1027 } 1028 } 1029 1030 /** 1031 * Calculates the item label anchor point. 1032 * 1033 * @param anchor the anchor. 1034 * @param bar the bar. 1035 * @param orientation the plot orientation. 1036 * 1037 * @return The anchor point. 1038 */ 1039 private Point2D calculateLabelAnchorPoint(ItemLabelAnchor anchor, 1040 Rectangle2D bar, PlotOrientation orientation) { 1041 1042 Point2D result = null; 1043 double offset = getItemLabelAnchorOffset(); 1044 double x0 = bar.getX() - offset; 1045 double x1 = bar.getX(); 1046 double x2 = bar.getX() + offset; 1047 double x3 = bar.getCenterX(); 1048 double x4 = bar.getMaxX() - offset; 1049 double x5 = bar.getMaxX(); 1050 double x6 = bar.getMaxX() + offset; 1051 1052 double y0 = bar.getMaxY() + offset; 1053 double y1 = bar.getMaxY(); 1054 double y2 = bar.getMaxY() - offset; 1055 double y3 = bar.getCenterY(); 1056 double y4 = bar.getMinY() + offset; 1057 double y5 = bar.getMinY(); 1058 double y6 = bar.getMinY() - offset; 1059 1060 if (anchor == ItemLabelAnchor.CENTER) { 1061 result = new Point2D.Double(x3, y3); 1062 } 1063 else if (anchor == ItemLabelAnchor.INSIDE1) { 1064 result = new Point2D.Double(x4, y4); 1065 } 1066 else if (anchor == ItemLabelAnchor.INSIDE2) { 1067 result = new Point2D.Double(x4, y4); 1068 } 1069 else if (anchor == ItemLabelAnchor.INSIDE3) { 1070 result = new Point2D.Double(x4, y3); 1071 } 1072 else if (anchor == ItemLabelAnchor.INSIDE4) { 1073 result = new Point2D.Double(x4, y2); 1074 } 1075 else if (anchor == ItemLabelAnchor.INSIDE5) { 1076 result = new Point2D.Double(x4, y2); 1077 } 1078 else if (anchor == ItemLabelAnchor.INSIDE6) { 1079 result = new Point2D.Double(x3, y2); 1080 } 1081 else if (anchor == ItemLabelAnchor.INSIDE7) { 1082 result = new Point2D.Double(x2, y2); 1083 } 1084 else if (anchor == ItemLabelAnchor.INSIDE8) { 1085 result = new Point2D.Double(x2, y2); 1086 } 1087 else if (anchor == ItemLabelAnchor.INSIDE9) { 1088 result = new Point2D.Double(x2, y3); 1089 } 1090 else if (anchor == ItemLabelAnchor.INSIDE10) { 1091 result = new Point2D.Double(x2, y4); 1092 } 1093 else if (anchor == ItemLabelAnchor.INSIDE11) { 1094 result = new Point2D.Double(x2, y4); 1095 } 1096 else if (anchor == ItemLabelAnchor.INSIDE12) { 1097 result = new Point2D.Double(x3, y4); 1098 } 1099 else if (anchor == ItemLabelAnchor.OUTSIDE1) { 1100 result = new Point2D.Double(x5, y6); 1101 } 1102 else if (anchor == ItemLabelAnchor.OUTSIDE2) { 1103 result = new Point2D.Double(x6, y5); 1104 } 1105 else if (anchor == ItemLabelAnchor.OUTSIDE3) { 1106 result = new Point2D.Double(x6, y3); 1107 } 1108 else if (anchor == ItemLabelAnchor.OUTSIDE4) { 1109 result = new Point2D.Double(x6, y1); 1110 } 1111 else if (anchor == ItemLabelAnchor.OUTSIDE5) { 1112 result = new Point2D.Double(x5, y0); 1113 } 1114 else if (anchor == ItemLabelAnchor.OUTSIDE6) { 1115 result = new Point2D.Double(x3, y0); 1116 } 1117 else if (anchor == ItemLabelAnchor.OUTSIDE7) { 1118 result = new Point2D.Double(x1, y0); 1119 } 1120 else if (anchor == ItemLabelAnchor.OUTSIDE8) { 1121 result = new Point2D.Double(x0, y1); 1122 } 1123 else if (anchor == ItemLabelAnchor.OUTSIDE9) { 1124 result = new Point2D.Double(x0, y3); 1125 } 1126 else if (anchor == ItemLabelAnchor.OUTSIDE10) { 1127 result = new Point2D.Double(x0, y5); 1128 } 1129 else if (anchor == ItemLabelAnchor.OUTSIDE11) { 1130 result = new Point2D.Double(x1, y6); 1131 } 1132 else if (anchor == ItemLabelAnchor.OUTSIDE12) { 1133 result = new Point2D.Double(x3, y6); 1134 } 1135 1136 return result; 1137 1138 } 1139 1140 /** 1141 * Returns {@code true} if the specified anchor point is inside a bar. 1142 * 1143 * @param anchor the anchor point. 1144 * 1145 * @return A boolean. 1146 */ 1147 private boolean isInternalAnchor(ItemLabelAnchor anchor) { 1148 return anchor == ItemLabelAnchor.CENTER 1149 || anchor == ItemLabelAnchor.INSIDE1 1150 || anchor == ItemLabelAnchor.INSIDE2 1151 || anchor == ItemLabelAnchor.INSIDE3 1152 || anchor == ItemLabelAnchor.INSIDE4 1153 || anchor == ItemLabelAnchor.INSIDE5 1154 || anchor == ItemLabelAnchor.INSIDE6 1155 || anchor == ItemLabelAnchor.INSIDE7 1156 || anchor == ItemLabelAnchor.INSIDE8 1157 || anchor == ItemLabelAnchor.INSIDE9 1158 || anchor == ItemLabelAnchor.INSIDE10 1159 || anchor == ItemLabelAnchor.INSIDE11 1160 || anchor == ItemLabelAnchor.INSIDE12; 1161 } 1162 1163 /** 1164 * Returns the lower and upper bounds (range) of the x-values in the 1165 * specified dataset. Since this renderer uses the x-interval in the 1166 * dataset, this is taken into account for the range. 1167 * 1168 * @param dataset the dataset ({@code null} permitted). 1169 * 1170 * @return The range ({@code null} if the dataset is 1171 * {@code null} or empty). 1172 */ 1173 @Override 1174 public Range findDomainBounds(XYDataset dataset) { 1175 return findDomainBounds(dataset, true); 1176 } 1177 1178 /** 1179 * Returns the lower and upper bounds (range) of the y-values in the 1180 * specified dataset. If the renderer is plotting the y-interval from the 1181 * dataset, this is taken into account for the range. 1182 * 1183 * @param dataset the dataset ({@code null} permitted). 1184 * 1185 * @return The range ({@code null} if the dataset is 1186 * {@code null} or empty). 1187 */ 1188 @Override 1189 public Range findRangeBounds(XYDataset dataset) { 1190 return findRangeBounds(dataset, this.useYInterval); 1191 } 1192 1193 /** 1194 * Returns a clone of the renderer. 1195 * 1196 * @return A clone. 1197 * 1198 * @throws CloneNotSupportedException if the renderer cannot be cloned. 1199 */ 1200 @Override 1201 public Object clone() throws CloneNotSupportedException { 1202 XYBarRenderer result = (XYBarRenderer) super.clone(); 1203 if (this.gradientPaintTransformer != null) { 1204 result.gradientPaintTransformer = (GradientPaintTransformer) 1205 ObjectUtils.clone(this.gradientPaintTransformer); 1206 } 1207 result.legendBar = ShapeUtils.clone(this.legendBar); 1208 return result; 1209 } 1210 1211 /** 1212 * Tests this renderer for equality with an arbitrary object. 1213 * 1214 * @param obj the object to test against ({@code null} permitted). 1215 * 1216 * @return A boolean. 1217 */ 1218 @Override 1219 public boolean equals(Object obj) { 1220 if (obj == this) { 1221 return true; 1222 } 1223 if (!(obj instanceof XYBarRenderer)) { 1224 return false; 1225 } 1226 XYBarRenderer that = (XYBarRenderer) obj; 1227 if (this.base != that.base) { 1228 return false; 1229 } 1230 if (this.drawBarOutline != that.drawBarOutline) { 1231 return false; 1232 } 1233 if (this.margin != that.margin) { 1234 return false; 1235 } 1236 if (this.useYInterval != that.useYInterval) { 1237 return false; 1238 } 1239 if (!ObjectUtils.equal(this.gradientPaintTransformer, 1240 that.gradientPaintTransformer)) { 1241 return false; 1242 } 1243 if (!ShapeUtils.equal(this.legendBar, that.legendBar)) { 1244 return false; 1245 } 1246 if (!ObjectUtils.equal(this.positiveItemLabelPositionFallback, 1247 that.positiveItemLabelPositionFallback)) { 1248 return false; 1249 } 1250 if (!ObjectUtils.equal(this.negativeItemLabelPositionFallback, 1251 that.negativeItemLabelPositionFallback)) { 1252 return false; 1253 } 1254 if (!this.barPainter.equals(that.barPainter)) { 1255 return false; 1256 } 1257 if (this.shadowsVisible != that.shadowsVisible) { 1258 return false; 1259 } 1260 if (this.shadowXOffset != that.shadowXOffset) { 1261 return false; 1262 } 1263 if (this.shadowYOffset != that.shadowYOffset) { 1264 return false; 1265 } 1266 if (this.barAlignmentFactor != that.barAlignmentFactor) { 1267 return false; 1268 } 1269 return super.equals(obj); 1270 } 1271 1272 /** 1273 * Provides serialization support. 1274 * 1275 * @param stream the input stream. 1276 * 1277 * @throws IOException if there is an I/O error. 1278 * @throws ClassNotFoundException if there is a classpath problem. 1279 */ 1280 private void readObject(ObjectInputStream stream) 1281 throws IOException, ClassNotFoundException { 1282 stream.defaultReadObject(); 1283 this.legendBar = SerialUtils.readShape(stream); 1284 } 1285 1286 /** 1287 * Provides serialization support. 1288 * 1289 * @param stream the output stream. 1290 * 1291 * @throws IOException if there is an I/O error. 1292 */ 1293 private void writeObject(ObjectOutputStream stream) throws IOException { 1294 stream.defaultWriteObject(); 1295 SerialUtils.writeShape(this.legendBar, stream); 1296 } 1297 1298}