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 * BoxAndWhiskerRenderer.java 029 * -------------------------- 030 * (C) Copyright 2003-2017, by David Browning and Contributors. 031 * 032 * Original Author: David Browning (for the Australian Institute of Marine 033 * Science); 034 * Contributor(s): David Gilbert (for Object Refinery Limited); 035 * Tim Bardzil; 036 * Rob Van der Sanden (patches 1866446 and 1888422); 037 * Peter Becker (patches 2868585 and 2868608); 038 * Martin Krauskopf (patch 3421088); 039 * Martin Hoeller; 040 * John Matthews; 041 * 042 * Changes 043 * ------- 044 * 21-Aug-2003 : Version 1, contributed by David Browning (for the Australian 045 * Institute of Marine Science); 046 * 01-Sep-2003 : Incorporated outlier and farout symbols for low values 047 * also (DG); 048 * 08-Sep-2003 : Changed ValueAxis API (DG); 049 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 050 * 07-Oct-2003 : Added renderer state (DG); 051 * 12-Nov-2003 : Fixed casting bug reported by Tim Bardzil (DG); 052 * 13-Nov-2003 : Added drawHorizontalItem() method contributed by Tim 053 * Bardzil (DG); 054 * 25-Apr-2004 : Added fillBox attribute, equals() method and added 055 * serialization code (DG); 056 * 29-Apr-2004 : Changed drawing of upper and lower shadows - see bug report 057 * 944011 (DG); 058 * 05-Nov-2004 : Modified drawItem() signature (DG); 059 * 09-Mar-2005 : Override getLegendItem() method so that legend item shapes 060 * are shown as blocks (DG); 061 * 20-Apr-2005 : Generate legend labels, tooltips and URLs (DG); 062 * 09-Jun-2005 : Updated equals() to handle GradientPaint (DG); 063 * ------------- JFREECHART 1.0.x --------------------------------------------- 064 * 12-Oct-2006 : Source reformatting and API doc updates (DG); 065 * 12-Oct-2006 : Fixed bug 1572478, potential NullPointerException (DG); 066 * 05-Feb-2006 : Added event notifications to a couple of methods (DG); 067 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG); 068 * 11-May-2007 : Added check for visibility in getLegendItem() (DG); 069 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG); 070 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 071 * 03-Jan-2008 : Check visibility of average marker before drawing it (DG); 072 * 15-Jan-2008 : Add getMaximumBarWidth() and setMaximumBarWidth() 073 * methods (RVdS); 074 * 14-Feb-2008 : Fix bar position for horizontal chart, see patch 075 * 1888422 (RVdS); 076 * 27-Mar-2008 : Boxes should use outlinePaint/Stroke settings (DG); 077 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 078 * 02-Oct-2008 : Check item visibility in drawItem() method (DG); 079 * 21-Jan-2009 : Added flags to control visibility of mean and median 080 * indicators (DG); 081 * 28-Sep-2009 : Added fireChangeEvent() to setMedianVisible (DG); 082 * 28-Sep-2009 : Added useOutlinePaintForWhiskers flag, see patch 2868585 083 * by Peter Becker (DG); 084 * 28-Sep-2009 : Added whiskerWidth attribute, see patch 2868608 by Peter 085 * Becker (DG); 086 * 11-Oct-2011 : applied patch #3421088 from Martin Krauskopf to fix bug (MH); 087 * 03-Jul-2013 : Use ParamChecks (DG); 088 * 18-Jul-2016 : Fix drawing issue with horizontal orientation (JM); 089 * 090 */ 091 092package org.jfree.chart.renderer.category; 093 094import java.awt.Color; 095import java.awt.Graphics2D; 096import java.awt.Paint; 097import java.awt.Shape; 098import java.awt.Stroke; 099import java.awt.geom.Ellipse2D; 100import java.awt.geom.Line2D; 101import java.awt.geom.Point2D; 102import java.awt.geom.Rectangle2D; 103import java.io.IOException; 104import java.io.ObjectInputStream; 105import java.io.ObjectOutputStream; 106import java.io.Serializable; 107import java.util.ArrayList; 108import java.util.Collections; 109import java.util.Iterator; 110import java.util.List; 111 112import org.jfree.chart.LegendItem; 113import org.jfree.chart.axis.CategoryAxis; 114import org.jfree.chart.axis.ValueAxis; 115import org.jfree.chart.entity.EntityCollection; 116import org.jfree.chart.event.RendererChangeEvent; 117import org.jfree.chart.plot.CategoryPlot; 118import org.jfree.chart.plot.PlotOrientation; 119import org.jfree.chart.plot.PlotRenderingInfo; 120import org.jfree.chart.renderer.Outlier; 121import org.jfree.chart.renderer.OutlierList; 122import org.jfree.chart.renderer.OutlierListCollection; 123import org.jfree.chart.ui.RectangleEdge; 124import org.jfree.chart.util.PaintUtils; 125import org.jfree.chart.util.Args; 126import org.jfree.chart.util.PublicCloneable; 127import org.jfree.chart.util.SerialUtils; 128import org.jfree.data.Range; 129import org.jfree.data.category.CategoryDataset; 130import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset; 131 132/** 133 * A box-and-whisker renderer. This renderer requires a 134 * {@link BoxAndWhiskerCategoryDataset} and is for use with the 135 * {@link CategoryPlot} class. The example shown here is generated 136 * by the {@code BoxAndWhiskerChartDemo1.java} program included in the 137 * JFreeChart Demo Collection: 138 * <br><br> 139 * <img src="../../../../../images/BoxAndWhiskerRendererSample.png" 140 * alt="BoxAndWhiskerRendererSample.png"> 141 */ 142public class BoxAndWhiskerRenderer extends AbstractCategoryItemRenderer 143 implements Cloneable, PublicCloneable, Serializable { 144 145 /** For serialization. */ 146 private static final long serialVersionUID = 632027470694481177L; 147 148 /** The color used to paint the median line and average marker. */ 149 private transient Paint artifactPaint; 150 151 /** A flag that controls whether or not the box is filled. */ 152 private boolean fillBox; 153 154 /** The margin between items (boxes) within a category. */ 155 private double itemMargin; 156 157 /** 158 * The maximum bar width as percentage of the available space in the plot. 159 * Take care with the encoding - for example, 0.05 is five percent. 160 */ 161 private double maximumBarWidth; 162 163 /** 164 * A flag that controls whether or not the median indicator is drawn. 165 * 166 * @since 1.0.13 167 */ 168 private boolean medianVisible; 169 170 /** 171 * A flag that controls whether or not the mean indicator is drawn. 172 * 173 * @since 1.0.13 174 */ 175 private boolean meanVisible; 176 177 /** 178 * A flag that, if {@code true}, causes the whiskers to be drawn 179 * using the outline paint for the series. The default value is 180 * {@code false} and in that case the regular series paint is used. 181 * 182 * @since 1.0.14 183 */ 184 private boolean useOutlinePaintForWhiskers; 185 186 /** 187 * The width of the whiskers as fraction of the bar width. 188 * 189 * @since 1.0.14 190 */ 191 private double whiskerWidth; 192 193 /** 194 * Default constructor. 195 */ 196 public BoxAndWhiskerRenderer() { 197 this.artifactPaint = Color.BLACK; 198 this.fillBox = true; 199 this.itemMargin = 0.20; 200 this.maximumBarWidth = 1.0; 201 this.medianVisible = true; 202 this.meanVisible = true; 203 this.useOutlinePaintForWhiskers = false; 204 this.whiskerWidth = 1.0; 205 setDefaultLegendShape(new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0)); 206 } 207 208 /** 209 * Returns the paint used to color the median and average markers. 210 * 211 * @return The paint used to draw the median and average markers (never 212 * {@code null}). 213 * 214 * @see #setArtifactPaint(Paint) 215 */ 216 public Paint getArtifactPaint() { 217 return this.artifactPaint; 218 } 219 220 /** 221 * Sets the paint used to color the median and average markers and sends 222 * a {@link RendererChangeEvent} to all registered listeners. 223 * 224 * @param paint the paint ({@code null} not permitted). 225 * 226 * @see #getArtifactPaint() 227 */ 228 public void setArtifactPaint(Paint paint) { 229 Args.nullNotPermitted(paint, "paint"); 230 this.artifactPaint = paint; 231 fireChangeEvent(); 232 } 233 234 /** 235 * Returns the flag that controls whether or not the box is filled. 236 * 237 * @return A boolean. 238 * 239 * @see #setFillBox(boolean) 240 */ 241 public boolean getFillBox() { 242 return this.fillBox; 243 } 244 245 /** 246 * Sets the flag that controls whether or not the box is filled and sends a 247 * {@link RendererChangeEvent} to all registered listeners. 248 * 249 * @param flag the flag. 250 * 251 * @see #getFillBox() 252 */ 253 public void setFillBox(boolean flag) { 254 this.fillBox = flag; 255 fireChangeEvent(); 256 } 257 258 /** 259 * Returns the item margin. This is a percentage of the available space 260 * that is allocated to the space between items in the chart. 261 * 262 * @return The margin. 263 * 264 * @see #setItemMargin(double) 265 */ 266 public double getItemMargin() { 267 return this.itemMargin; 268 } 269 270 /** 271 * Sets the item margin and sends a {@link RendererChangeEvent} to all 272 * registered listeners. 273 * 274 * @param margin the margin (a percentage). 275 * 276 * @see #getItemMargin() 277 */ 278 public void setItemMargin(double margin) { 279 this.itemMargin = margin; 280 fireChangeEvent(); 281 } 282 283 /** 284 * Returns the maximum bar width as a percentage of the available drawing 285 * space. Take care with the encoding, for example 0.10 is ten percent. 286 * 287 * @return The maximum bar width. 288 * 289 * @see #setMaximumBarWidth(double) 290 * 291 * @since 1.0.10 292 */ 293 public double getMaximumBarWidth() { 294 return this.maximumBarWidth; 295 } 296 297 /** 298 * Sets the maximum bar width, which is specified as a percentage of the 299 * available space for all bars, and sends a {@link RendererChangeEvent} 300 * to all registered listeners. 301 * 302 * @param percent the maximum bar width (a percentage, where 0.10 is ten 303 * percent). 304 * 305 * @see #getMaximumBarWidth() 306 * 307 * @since 1.0.10 308 */ 309 public void setMaximumBarWidth(double percent) { 310 this.maximumBarWidth = percent; 311 fireChangeEvent(); 312 } 313 314 /** 315 * Returns the flag that controls whether or not the mean indicator is 316 * draw for each item. 317 * 318 * @return A boolean. 319 * 320 * @see #setMeanVisible(boolean) 321 * 322 * @since 1.0.13 323 */ 324 public boolean isMeanVisible() { 325 return this.meanVisible; 326 } 327 328 /** 329 * Sets the flag that controls whether or not the mean indicator is drawn 330 * for each item, and sends a {@link RendererChangeEvent} to all 331 * registered listeners. 332 * 333 * @param visible the new flag value. 334 * 335 * @see #isMeanVisible() 336 * 337 * @since 1.0.13 338 */ 339 public void setMeanVisible(boolean visible) { 340 if (this.meanVisible == visible) { 341 return; 342 } 343 this.meanVisible = visible; 344 fireChangeEvent(); 345 } 346 347 /** 348 * Returns the flag that controls whether or not the median indicator is 349 * draw for each item. 350 * 351 * @return A boolean. 352 * 353 * @see #setMedianVisible(boolean) 354 * 355 * @since 1.0.13 356 */ 357 public boolean isMedianVisible() { 358 return this.medianVisible; 359 } 360 361 /** 362 * Sets the flag that controls whether or not the median indicator is drawn 363 * for each item, and sends a {@link RendererChangeEvent} to all 364 * registered listeners. 365 * 366 * @param visible the new flag value. 367 * 368 * @see #isMedianVisible() 369 * 370 * @since 1.0.13 371 */ 372 public void setMedianVisible(boolean visible) { 373 if (this.medianVisible == visible) { 374 return; 375 } 376 this.medianVisible = visible; 377 fireChangeEvent(); 378 } 379 380 /** 381 * Returns the flag that, if {@code true}, causes the whiskers to 382 * be drawn using the series outline paint. 383 * 384 * @return A boolean. 385 * 386 * @since 1.0.14 387 */ 388 public boolean getUseOutlinePaintForWhiskers() { 389 return this.useOutlinePaintForWhiskers; 390 } 391 392 /** 393 * Sets the flag that, if {@code true}, causes the whiskers to 394 * be drawn using the series outline paint, and sends a 395 * {@link RendererChangeEvent} to all registered listeners. 396 * 397 * @param flag the new flag value. 398 * 399 * @since 1.0.14 400 */ 401 public void setUseOutlinePaintForWhiskers(boolean flag) { 402 if (this.useOutlinePaintForWhiskers == flag) { 403 return; 404 } 405 this.useOutlinePaintForWhiskers = flag; 406 fireChangeEvent(); 407 } 408 409 /** 410 * Returns the width of the whiskers as fraction of the bar width. 411 * 412 * @return The width of the whiskers. 413 * 414 * @see #setWhiskerWidth(double) 415 * 416 * @since 1.0.14 417 */ 418 public double getWhiskerWidth() { 419 return this.whiskerWidth; 420 } 421 422 /** 423 * Sets the width of the whiskers as a fraction of the bar width and sends 424 * a {@link RendererChangeEvent} to all registered listeners. 425 * 426 * @param width a value between 0 and 1 indicating how wide the 427 * whisker is supposed to be compared to the bar. 428 * @see #getWhiskerWidth() 429 * @see CategoryItemRendererState#getBarWidth() 430 * 431 * @since 1.0.14 432 */ 433 public void setWhiskerWidth(double width) { 434 if (width < 0 || width > 1) { 435 throw new IllegalArgumentException( 436 "Value for whisker width out of range"); 437 } 438 if (width == this.whiskerWidth) { 439 return; 440 } 441 this.whiskerWidth = width; 442 fireChangeEvent(); 443 } 444 445 /** 446 * Returns a legend item for a series. 447 * 448 * @param datasetIndex the dataset index (zero-based). 449 * @param series the series index (zero-based). 450 * 451 * @return The legend item (possibly {@code null}). 452 */ 453 @Override 454 public LegendItem getLegendItem(int datasetIndex, int series) { 455 456 CategoryPlot cp = getPlot(); 457 if (cp == null) { 458 return null; 459 } 460 461 // check that a legend item needs to be displayed... 462 if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) { 463 return null; 464 } 465 466 CategoryDataset dataset = cp.getDataset(datasetIndex); 467 String label = getLegendItemLabelGenerator().generateLabel(dataset, 468 series); 469 String description = label; 470 String toolTipText = null; 471 if (getLegendItemToolTipGenerator() != null) { 472 toolTipText = getLegendItemToolTipGenerator().generateLabel( 473 dataset, series); 474 } 475 String urlText = null; 476 if (getLegendItemURLGenerator() != null) { 477 urlText = getLegendItemURLGenerator().generateLabel(dataset, 478 series); 479 } 480 Shape shape = lookupLegendShape(series); 481 Paint paint = lookupSeriesPaint(series); 482 Paint outlinePaint = lookupSeriesOutlinePaint(series); 483 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 484 LegendItem result = new LegendItem(label, description, toolTipText, 485 urlText, shape, paint, outlineStroke, outlinePaint); 486 result.setLabelFont(lookupLegendTextFont(series)); 487 Paint labelPaint = lookupLegendTextPaint(series); 488 if (labelPaint != null) { 489 result.setLabelPaint(labelPaint); 490 } 491 result.setDataset(dataset); 492 result.setDatasetIndex(datasetIndex); 493 result.setSeriesKey(dataset.getRowKey(series)); 494 result.setSeriesIndex(series); 495 return result; 496 497 } 498 499 /** 500 * Returns the range of values from the specified dataset that the 501 * renderer will require to display all the data. 502 * 503 * @param dataset the dataset. 504 * 505 * @return The range. 506 */ 507 @Override 508 public Range findRangeBounds(CategoryDataset dataset) { 509 return super.findRangeBounds(dataset, true); 510 } 511 512 /** 513 * Initialises the renderer. This method gets called once at the start of 514 * the process of drawing a chart. 515 * 516 * @param g2 the graphics device. 517 * @param dataArea the area in which the data is to be plotted. 518 * @param plot the plot. 519 * @param rendererIndex the renderer index. 520 * @param info collects chart rendering information for return to caller. 521 * 522 * @return The renderer state. 523 */ 524 @Override 525 public CategoryItemRendererState initialise(Graphics2D g2, 526 Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, 527 PlotRenderingInfo info) { 528 529 CategoryItemRendererState state = super.initialise(g2, dataArea, plot, 530 rendererIndex, info); 531 // calculate the box width 532 CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); 533 CategoryDataset dataset = plot.getDataset(rendererIndex); 534 if (dataset != null) { 535 int columns = dataset.getColumnCount(); 536 int rows = dataset.getRowCount(); 537 double space = 0.0; 538 PlotOrientation orientation = plot.getOrientation(); 539 if (orientation == PlotOrientation.HORIZONTAL) { 540 space = dataArea.getHeight(); 541 } 542 else if (orientation == PlotOrientation.VERTICAL) { 543 space = dataArea.getWidth(); 544 } 545 double maxWidth = space * getMaximumBarWidth(); 546 double categoryMargin = 0.0; 547 double currentItemMargin = 0.0; 548 if (columns > 1) { 549 categoryMargin = domainAxis.getCategoryMargin(); 550 } 551 if (rows > 1) { 552 currentItemMargin = getItemMargin(); 553 } 554 double used = space * (1 - domainAxis.getLowerMargin() 555 - domainAxis.getUpperMargin() 556 - categoryMargin - currentItemMargin); 557 if ((rows * columns) > 0) { 558 state.setBarWidth(Math.min(used / (dataset.getColumnCount() 559 * dataset.getRowCount()), maxWidth)); 560 } 561 else { 562 state.setBarWidth(Math.min(used, maxWidth)); 563 } 564 } 565 return state; 566 567 } 568 569 /** 570 * Draw a single data item. 571 * 572 * @param g2 the graphics device. 573 * @param state the renderer state. 574 * @param dataArea the area in which the data is drawn. 575 * @param plot the plot. 576 * @param domainAxis the domain axis. 577 * @param rangeAxis the range axis. 578 * @param dataset the data (must be an instance of 579 * {@link BoxAndWhiskerCategoryDataset}). 580 * @param row the row index (zero-based). 581 * @param column the column index (zero-based). 582 * @param pass the pass index. 583 */ 584 @Override 585 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 586 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 587 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, 588 int pass) { 589 590 // do nothing if item is not visible 591 if (!getItemVisible(row, column)) { 592 return; 593 } 594 595 if (!(dataset instanceof BoxAndWhiskerCategoryDataset)) { 596 throw new IllegalArgumentException( 597 "BoxAndWhiskerRenderer.drawItem() : the data should be " 598 + "of type BoxAndWhiskerCategoryDataset only."); 599 } 600 601 PlotOrientation orientation = plot.getOrientation(); 602 603 if (orientation == PlotOrientation.HORIZONTAL) { 604 drawHorizontalItem(g2, state, dataArea, plot, domainAxis, 605 rangeAxis, dataset, row, column); 606 } 607 else if (orientation == PlotOrientation.VERTICAL) { 608 drawVerticalItem(g2, state, dataArea, plot, domainAxis, 609 rangeAxis, dataset, row, column); 610 } 611 612 } 613 614 /** 615 * Draws the visual representation of a single data item when the plot has 616 * a horizontal orientation. 617 * 618 * @param g2 the graphics device. 619 * @param state the renderer state. 620 * @param dataArea the area within which the plot is being drawn. 621 * @param plot the plot (can be used to obtain standard color 622 * information etc). 623 * @param domainAxis the domain axis. 624 * @param rangeAxis the range axis. 625 * @param dataset the dataset (must be an instance of 626 * {@link BoxAndWhiskerCategoryDataset}). 627 * @param row the row index (zero-based). 628 * @param column the column index (zero-based). 629 */ 630 public void drawHorizontalItem(Graphics2D g2, 631 CategoryItemRendererState state, Rectangle2D dataArea, 632 CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, 633 CategoryDataset dataset, int row, int column) { 634 635 BoxAndWhiskerCategoryDataset bawDataset 636 = (BoxAndWhiskerCategoryDataset) dataset; 637 638 double categoryEnd = domainAxis.getCategoryEnd(column, 639 getColumnCount(), dataArea, plot.getDomainAxisEdge()); 640 double categoryStart = domainAxis.getCategoryStart(column, 641 getColumnCount(), dataArea, plot.getDomainAxisEdge()); 642 double categoryWidth = Math.abs(categoryEnd - categoryStart); 643 644 double yy = categoryStart; 645 int seriesCount = getRowCount(); 646 int categoryCount = getColumnCount(); 647 648 if (seriesCount > 1) { 649 double seriesGap = dataArea.getHeight() * getItemMargin() 650 / (categoryCount * (seriesCount - 1)); 651 double usedWidth = (state.getBarWidth() * seriesCount) 652 + (seriesGap * (seriesCount - 1)); 653 // offset the start of the boxes if the total width used is smaller 654 // than the category width 655 double offset = (categoryWidth - usedWidth) / 2; 656 yy = yy + offset + (row * (state.getBarWidth() + seriesGap)); 657 } 658 else { 659 // offset the start of the box if the box width is smaller than 660 // the category width 661 double offset = (categoryWidth - state.getBarWidth()) / 2; 662 yy = yy + offset; 663 } 664 665 g2.setPaint(getItemPaint(row, column)); 666 Stroke s = getItemStroke(row, column); 667 g2.setStroke(s); 668 669 RectangleEdge location = plot.getRangeAxisEdge(); 670 671 Number xQ1 = bawDataset.getQ1Value(row, column); 672 Number xQ3 = bawDataset.getQ3Value(row, column); 673 Number xMax = bawDataset.getMaxRegularValue(row, column); 674 Number xMin = bawDataset.getMinRegularValue(row, column); 675 676 Shape box = null; 677 if (xQ1 != null && xQ3 != null && xMax != null && xMin != null) { 678 679 double xxQ1 = rangeAxis.valueToJava2D(xQ1.doubleValue(), dataArea, 680 location); 681 double xxQ3 = rangeAxis.valueToJava2D(xQ3.doubleValue(), dataArea, 682 location); 683 double xxMax = rangeAxis.valueToJava2D(xMax.doubleValue(), dataArea, 684 location); 685 double xxMin = rangeAxis.valueToJava2D(xMin.doubleValue(), dataArea, 686 location); 687 double yymid = yy + state.getBarWidth() / 2.0; 688 double halfW = (state.getBarWidth() / 2.0) * this.whiskerWidth; 689 690 // draw the box... 691 box = new Rectangle2D.Double(Math.min(xxQ1, xxQ3), yy, 692 Math.abs(xxQ1 - xxQ3), state.getBarWidth()); 693 if (this.fillBox) { 694 g2.fill(box); 695 } 696 697 Paint outlinePaint = getItemOutlinePaint(row, column); 698 if (this.useOutlinePaintForWhiskers) { 699 g2.setPaint(outlinePaint); 700 } 701 // draw the upper shadow... 702 g2.draw(new Line2D.Double(xxMax, yymid, xxQ3, yymid)); 703 g2.draw(new Line2D.Double(xxMax, yymid - halfW, xxMax, 704 yymid + halfW)); 705 706 // draw the lower shadow... 707 g2.draw(new Line2D.Double(xxMin, yymid, xxQ1, yymid)); 708 g2.draw(new Line2D.Double(xxMin, yymid - halfW, xxMin, 709 yymid + halfW)); 710 711 g2.setStroke(getItemOutlineStroke(row, column)); 712 g2.setPaint(outlinePaint); 713 g2.draw(box); 714 } 715 716 // draw mean - SPECIAL AIMS REQUIREMENT... 717 g2.setPaint(this.artifactPaint); 718 double aRadius; // average radius 719 if (this.meanVisible) { 720 Number xMean = bawDataset.getMeanValue(row, column); 721 if (xMean != null) { 722 double xxMean = rangeAxis.valueToJava2D(xMean.doubleValue(), 723 dataArea, location); 724 aRadius = state.getBarWidth() / 4; 725 // here we check that the average marker will in fact be 726 // visible before drawing it... 727 if ((xxMean > (dataArea.getMinX() - aRadius)) 728 && (xxMean < (dataArea.getMaxX() + aRadius))) { 729 Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xxMean 730 - aRadius, yy + aRadius, aRadius * 2, aRadius * 2); 731 g2.fill(avgEllipse); 732 g2.draw(avgEllipse); 733 } 734 } 735 } 736 737 // draw median... 738 if (this.medianVisible) { 739 Number xMedian = bawDataset.getMedianValue(row, column); 740 if (xMedian != null) { 741 double xxMedian = rangeAxis.valueToJava2D(xMedian.doubleValue(), 742 dataArea, location); 743 g2.draw(new Line2D.Double(xxMedian, yy, xxMedian, 744 yy + state.getBarWidth())); 745 } 746 } 747 748 // collect entity and tool tip information... 749 if (state.getInfo() != null && box != null) { 750 EntityCollection entities = state.getEntityCollection(); 751 if (entities != null) { 752 addItemEntity(entities, dataset, row, column, box); 753 } 754 } 755 756 } 757 758 /** 759 * Draws the visual representation of a single data item when the plot has 760 * a vertical orientation. 761 * 762 * @param g2 the graphics device. 763 * @param state the renderer state. 764 * @param dataArea the area within which the plot is being drawn. 765 * @param plot the plot (can be used to obtain standard color information 766 * etc). 767 * @param domainAxis the domain axis. 768 * @param rangeAxis the range axis. 769 * @param dataset the dataset (must be an instance of 770 * {@link BoxAndWhiskerCategoryDataset}). 771 * @param row the row index (zero-based). 772 * @param column the column index (zero-based). 773 */ 774 public void drawVerticalItem(Graphics2D g2, CategoryItemRendererState state, 775 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 776 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column) { 777 778 BoxAndWhiskerCategoryDataset bawDataset 779 = (BoxAndWhiskerCategoryDataset) dataset; 780 781 double categoryEnd = domainAxis.getCategoryEnd(column, 782 getColumnCount(), dataArea, plot.getDomainAxisEdge()); 783 double categoryStart = domainAxis.getCategoryStart(column, 784 getColumnCount(), dataArea, plot.getDomainAxisEdge()); 785 double categoryWidth = categoryEnd - categoryStart; 786 787 double xx = categoryStart; 788 int seriesCount = getRowCount(); 789 int categoryCount = getColumnCount(); 790 791 if (seriesCount > 1) { 792 double seriesGap = dataArea.getWidth() * getItemMargin() 793 / (categoryCount * (seriesCount - 1)); 794 double usedWidth = (state.getBarWidth() * seriesCount) 795 + (seriesGap * (seriesCount - 1)); 796 // offset the start of the boxes if the total width used is smaller 797 // than the category width 798 double offset = (categoryWidth - usedWidth) / 2; 799 xx = xx + offset + (row * (state.getBarWidth() + seriesGap)); 800 } 801 else { 802 // offset the start of the box if the box width is smaller than the 803 // category width 804 double offset = (categoryWidth - state.getBarWidth()) / 2; 805 xx = xx + offset; 806 } 807 808 double yyAverage; 809 double yyOutlier; 810 811 Paint itemPaint = getItemPaint(row, column); 812 g2.setPaint(itemPaint); 813 Stroke s = getItemStroke(row, column); 814 g2.setStroke(s); 815 816 double aRadius = 0; // average radius 817 818 RectangleEdge location = plot.getRangeAxisEdge(); 819 820 Number yQ1 = bawDataset.getQ1Value(row, column); 821 Number yQ3 = bawDataset.getQ3Value(row, column); 822 Number yMax = bawDataset.getMaxRegularValue(row, column); 823 Number yMin = bawDataset.getMinRegularValue(row, column); 824 Shape box = null; 825 if (yQ1 != null && yQ3 != null && yMax != null && yMin != null) { 826 827 double yyQ1 = rangeAxis.valueToJava2D(yQ1.doubleValue(), dataArea, 828 location); 829 double yyQ3 = rangeAxis.valueToJava2D(yQ3.doubleValue(), dataArea, 830 location); 831 double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(), 832 dataArea, location); 833 double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(), 834 dataArea, location); 835 double xxmid = xx + state.getBarWidth() / 2.0; 836 double halfW = (state.getBarWidth() / 2.0) * this.whiskerWidth; 837 838 // draw the body... 839 box = new Rectangle2D.Double(xx, Math.min(yyQ1, yyQ3), 840 state.getBarWidth(), Math.abs(yyQ1 - yyQ3)); 841 if (this.fillBox) { 842 g2.fill(box); 843 } 844 845 Paint outlinePaint = getItemOutlinePaint(row, column); 846 if (this.useOutlinePaintForWhiskers) { 847 g2.setPaint(outlinePaint); 848 } 849 // draw the upper shadow... 850 g2.draw(new Line2D.Double(xxmid, yyMax, xxmid, yyQ3)); 851 g2.draw(new Line2D.Double(xxmid - halfW, yyMax, xxmid + halfW, yyMax)); 852 853 // draw the lower shadow... 854 g2.draw(new Line2D.Double(xxmid, yyMin, xxmid, yyQ1)); 855 g2.draw(new Line2D.Double(xxmid - halfW, yyMin, xxmid + halfW, yyMin)); 856 857 g2.setStroke(getItemOutlineStroke(row, column)); 858 g2.setPaint(outlinePaint); 859 g2.draw(box); 860 } 861 862 g2.setPaint(this.artifactPaint); 863 864 // draw mean - SPECIAL AIMS REQUIREMENT... 865 if (this.meanVisible) { 866 Number yMean = bawDataset.getMeanValue(row, column); 867 if (yMean != null) { 868 yyAverage = rangeAxis.valueToJava2D(yMean.doubleValue(), 869 dataArea, location); 870 aRadius = state.getBarWidth() / 4; 871 // here we check that the average marker will in fact be 872 // visible before drawing it... 873 if ((yyAverage > (dataArea.getMinY() - aRadius)) 874 && (yyAverage < (dataArea.getMaxY() + aRadius))) { 875 Ellipse2D.Double avgEllipse = new Ellipse2D.Double( 876 xx + aRadius, yyAverage - aRadius, aRadius * 2, 877 aRadius * 2); 878 g2.fill(avgEllipse); 879 g2.draw(avgEllipse); 880 } 881 } 882 } 883 884 // draw median... 885 if (this.medianVisible) { 886 Number yMedian = bawDataset.getMedianValue(row, column); 887 if (yMedian != null) { 888 double yyMedian = rangeAxis.valueToJava2D( 889 yMedian.doubleValue(), dataArea, location); 890 g2.draw(new Line2D.Double(xx, yyMedian, 891 xx + state.getBarWidth(), yyMedian)); 892 } 893 } 894 895 // draw yOutliers... 896 double maxAxisValue = rangeAxis.valueToJava2D( 897 rangeAxis.getUpperBound(), dataArea, location) + aRadius; 898 double minAxisValue = rangeAxis.valueToJava2D( 899 rangeAxis.getLowerBound(), dataArea, location) - aRadius; 900 901 g2.setPaint(itemPaint); 902 903 // draw outliers 904 double oRadius = state.getBarWidth() / 3; // outlier radius 905 List outliers = new ArrayList(); 906 OutlierListCollection outlierListCollection 907 = new OutlierListCollection(); 908 909 // From outlier array sort out which are outliers and put these into a 910 // list If there are any farouts, set the flag on the 911 // OutlierListCollection 912 List yOutliers = bawDataset.getOutliers(row, column); 913 if (yOutliers != null) { 914 for (int i = 0; i < yOutliers.size(); i++) { 915 double outlier = ((Number) yOutliers.get(i)).doubleValue(); 916 Number minOutlier = bawDataset.getMinOutlier(row, column); 917 Number maxOutlier = bawDataset.getMaxOutlier(row, column); 918 Number minRegular = bawDataset.getMinRegularValue(row, column); 919 Number maxRegular = bawDataset.getMaxRegularValue(row, column); 920 if (outlier > maxOutlier.doubleValue()) { 921 outlierListCollection.setHighFarOut(true); 922 } 923 else if (outlier < minOutlier.doubleValue()) { 924 outlierListCollection.setLowFarOut(true); 925 } 926 else if (outlier > maxRegular.doubleValue()) { 927 yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea, 928 location); 929 outliers.add(new Outlier(xx + state.getBarWidth() / 2.0, 930 yyOutlier, oRadius)); 931 } 932 else if (outlier < minRegular.doubleValue()) { 933 yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea, 934 location); 935 outliers.add(new Outlier(xx + state.getBarWidth() / 2.0, 936 yyOutlier, oRadius)); 937 } 938 Collections.sort(outliers); 939 } 940 941 // Process outliers. Each outlier is either added to the 942 // appropriate outlier list or a new outlier list is made 943 for (Iterator iterator = outliers.iterator(); iterator.hasNext();) { 944 Outlier outlier = (Outlier) iterator.next(); 945 outlierListCollection.add(outlier); 946 } 947 948 for (Iterator iterator = outlierListCollection.iterator(); 949 iterator.hasNext();) { 950 OutlierList list = (OutlierList) iterator.next(); 951 Outlier outlier = list.getAveragedOutlier(); 952 Point2D point = outlier.getPoint(); 953 954 if (list.isMultiple()) { 955 drawMultipleEllipse(point, state.getBarWidth(), oRadius, 956 g2); 957 } 958 else { 959 drawEllipse(point, oRadius, g2); 960 } 961 } 962 963 // draw farout indicators 964 if (outlierListCollection.isHighFarOut()) { 965 drawHighFarOut(aRadius / 2.0, g2, 966 xx + state.getBarWidth() / 2.0, maxAxisValue); 967 } 968 969 if (outlierListCollection.isLowFarOut()) { 970 drawLowFarOut(aRadius / 2.0, g2, 971 xx + state.getBarWidth() / 2.0, minAxisValue); 972 } 973 } 974 // collect entity and tool tip information... 975 if (state.getInfo() != null && box != null) { 976 EntityCollection entities = state.getEntityCollection(); 977 if (entities != null) { 978 addItemEntity(entities, dataset, row, column, box); 979 } 980 } 981 982 } 983 984 /** 985 * Draws a dot to represent an outlier. 986 * 987 * @param point the location. 988 * @param oRadius the radius. 989 * @param g2 the graphics device. 990 */ 991 private void drawEllipse(Point2D point, double oRadius, Graphics2D g2) { 992 Ellipse2D dot = new Ellipse2D.Double(point.getX() + oRadius / 2, 993 point.getY(), oRadius, oRadius); 994 g2.draw(dot); 995 } 996 997 /** 998 * Draws two dots to represent the average value of more than one outlier. 999 * 1000 * @param point the location 1001 * @param boxWidth the box width. 1002 * @param oRadius the radius. 1003 * @param g2 the graphics device. 1004 */ 1005 private void drawMultipleEllipse(Point2D point, double boxWidth, 1006 double oRadius, Graphics2D g2) { 1007 1008 Ellipse2D dot1 = new Ellipse2D.Double(point.getX() - (boxWidth / 2) 1009 + oRadius, point.getY(), oRadius, oRadius); 1010 Ellipse2D dot2 = new Ellipse2D.Double(point.getX() + (boxWidth / 2), 1011 point.getY(), oRadius, oRadius); 1012 g2.draw(dot1); 1013 g2.draw(dot2); 1014 } 1015 1016 /** 1017 * Draws a triangle to indicate the presence of far-out values. 1018 * 1019 * @param aRadius the radius. 1020 * @param g2 the graphics device. 1021 * @param xx the x coordinate. 1022 * @param m the y coordinate. 1023 */ 1024 private void drawHighFarOut(double aRadius, Graphics2D g2, double xx, 1025 double m) { 1026 double side = aRadius * 2; 1027 g2.draw(new Line2D.Double(xx - side, m + side, xx + side, m + side)); 1028 g2.draw(new Line2D.Double(xx - side, m + side, xx, m)); 1029 g2.draw(new Line2D.Double(xx + side, m + side, xx, m)); 1030 } 1031 1032 /** 1033 * Draws a triangle to indicate the presence of far-out values. 1034 * 1035 * @param aRadius the radius. 1036 * @param g2 the graphics device. 1037 * @param xx the x coordinate. 1038 * @param m the y coordinate. 1039 */ 1040 private void drawLowFarOut(double aRadius, Graphics2D g2, double xx, 1041 double m) { 1042 double side = aRadius * 2; 1043 g2.draw(new Line2D.Double(xx - side, m - side, xx + side, m - side)); 1044 g2.draw(new Line2D.Double(xx - side, m - side, xx, m)); 1045 g2.draw(new Line2D.Double(xx + side, m - side, xx, m)); 1046 } 1047 1048 /** 1049 * Tests this renderer for equality with an arbitrary object. 1050 * 1051 * @param obj the object ({@code null} permitted). 1052 * 1053 * @return {@code true} or {@code false}. 1054 */ 1055 @Override 1056 public boolean equals(Object obj) { 1057 if (obj == this) { 1058 return true; 1059 } 1060 if (!(obj instanceof BoxAndWhiskerRenderer)) { 1061 return false; 1062 } 1063 BoxAndWhiskerRenderer that = (BoxAndWhiskerRenderer) obj; 1064 if (this.fillBox != that.fillBox) { 1065 return false; 1066 } 1067 if (this.itemMargin != that.itemMargin) { 1068 return false; 1069 } 1070 if (this.maximumBarWidth != that.maximumBarWidth) { 1071 return false; 1072 } 1073 if (this.meanVisible != that.meanVisible) { 1074 return false; 1075 } 1076 if (this.medianVisible != that.medianVisible) { 1077 return false; 1078 } 1079 if (this.useOutlinePaintForWhiskers 1080 != that.useOutlinePaintForWhiskers) { 1081 return false; 1082 } 1083 if (this.whiskerWidth != that.whiskerWidth) { 1084 return false; 1085 } 1086 if (!PaintUtils.equal(this.artifactPaint, that.artifactPaint)) { 1087 return false; 1088 } 1089 return super.equals(obj); 1090 } 1091 1092 /** 1093 * Provides serialization support. 1094 * 1095 * @param stream the output stream. 1096 * 1097 * @throws IOException if there is an I/O error. 1098 */ 1099 private void writeObject(ObjectOutputStream stream) throws IOException { 1100 stream.defaultWriteObject(); 1101 SerialUtils.writePaint(this.artifactPaint, stream); 1102 } 1103 1104 /** 1105 * Provides serialization support. 1106 * 1107 * @param stream the input stream. 1108 * 1109 * @throws IOException if there is an I/O error. 1110 * @throws ClassNotFoundException if there is a classpath problem. 1111 */ 1112 private void readObject(ObjectInputStream stream) 1113 throws IOException, ClassNotFoundException { 1114 stream.defaultReadObject(); 1115 this.artifactPaint = SerialUtils.readPaint(stream); 1116 } 1117 1118}