001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, by David Gilbert 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 * CombinedRangeXYPlot.java 029 * ------------------------ 030 * (C) Copyright 2001-2021, by Bill Kelemen and Contributors. 031 * 032 * Original Author: Bill Kelemen; 033 * Contributor(s): David Gilbert; 034 * Anthony Boulestreau; 035 * David Basten; 036 * Kevin Frechette (for ISTI); 037 * Arnaud Lelievre; 038 * Nicolas Brodu; 039 * Petr Kubanek (bug 1606205); 040 */ 041 042package org.jfree.chart.plot; 043 044import java.awt.Graphics2D; 045import java.awt.geom.Point2D; 046import java.awt.geom.Rectangle2D; 047import java.util.ArrayList; 048import java.util.Collections; 049import java.util.List; 050import java.util.Objects; 051import org.jfree.chart.ChartElementVisitor; 052 053import org.jfree.chart.legend.LegendItemCollection; 054import org.jfree.chart.axis.AxisSpace; 055import org.jfree.chart.axis.AxisState; 056import org.jfree.chart.axis.NumberAxis; 057import org.jfree.chart.axis.ValueAxis; 058import org.jfree.chart.event.PlotChangeEvent; 059import org.jfree.chart.event.PlotChangeListener; 060import org.jfree.chart.renderer.xy.XYItemRenderer; 061import org.jfree.chart.api.RectangleEdge; 062import org.jfree.chart.api.RectangleInsets; 063import org.jfree.chart.internal.CloneUtils; 064import org.jfree.chart.internal.Args; 065import org.jfree.chart.util.ShadowGenerator; 066import org.jfree.data.Range; 067 068/** 069 * An extension of {@link XYPlot} that contains multiple subplots that share a 070 * common range axis. 071 */ 072public class CombinedRangeXYPlot<S extends Comparable<S>> extends XYPlot<S> 073 implements PlotChangeListener { 074 075 /** For serialization. */ 076 private static final long serialVersionUID = -5177814085082031168L; 077 078 /** Storage for the subplot references. */ 079 private List<XYPlot> subplots; 080 081 /** The gap between subplots. */ 082 private double gap = 5.0; 083 084 /** Temporary storage for the subplot areas. */ 085 private transient Rectangle2D[] subplotAreas; 086 087 /** 088 * Default constructor. 089 */ 090 public CombinedRangeXYPlot() { 091 this(new NumberAxis()); 092 } 093 094 /** 095 * Creates a new plot. 096 * 097 * @param rangeAxis the shared axis. 098 */ 099 public CombinedRangeXYPlot(ValueAxis rangeAxis) { 100 super(null, // no data in the parent plot 101 null, rangeAxis, null); 102 this.subplots = new ArrayList<>(); 103 } 104 105 /** 106 * Returns a string describing the type of plot. 107 * 108 * @return The type of plot. 109 */ 110 @Override 111 public String getPlotType() { 112 return localizationResources.getString("Combined_Range_XYPlot"); 113 } 114 115 /** 116 * Returns the space between subplots. 117 * 118 * @return The gap. 119 * 120 * @see #setGap(double) 121 */ 122 public double getGap() { 123 return this.gap; 124 } 125 126 /** 127 * Sets the amount of space between subplots. 128 * 129 * @param gap the gap between subplots. 130 * 131 * @see #getGap() 132 */ 133 public void setGap(double gap) { 134 this.gap = gap; 135 } 136 137 /** 138 * Returns {@code true} if the domain is pannable for at least one subplot, 139 * and {@code false} otherwise. 140 * 141 * @return A boolean. 142 */ 143 @Override 144 public boolean isDomainPannable() { 145 for (XYPlot subplot : this.subplots) { 146 if (subplot.isDomainPannable()) { 147 return true; 148 } 149 } 150 return false; 151 } 152 153 /** 154 * Sets the flag, on each of the subplots, that controls whether or not the 155 * domain is pannable. 156 * 157 * @param pannable the new flag value. 158 */ 159 @Override 160 public void setDomainPannable(boolean pannable) { 161 for (XYPlot subplot : this.subplots) { 162 subplot.setDomainPannable(pannable); 163 } 164 } 165 166 /** 167 * Adds a subplot, with a default 'weight' of 1. 168 * <br><br> 169 * You must ensure that the subplot has a non-null domain axis. The range 170 * axis for the subplot will be set to {@code null}. 171 * 172 * @param subplot the subplot. 173 */ 174 public void add(XYPlot subplot) { 175 add(subplot, 1); 176 } 177 178 /** 179 * Adds a subplot with a particular weight (greater than or equal to one). 180 * The weight determines how much space is allocated to the subplot 181 * relative to all the other subplots. 182 * <br><br> 183 * You must ensure that the subplot has a non-null domain axis. The range 184 * axis for the subplot will be set to {@code null}. 185 * 186 * @param subplot the subplot ({@code null} not permitted). 187 * @param weight the weight (must be 1 or greater). 188 */ 189 public void add(XYPlot subplot, int weight) { 190 Args.nullNotPermitted(subplot, "subplot"); 191 if (weight <= 0) { 192 String msg = "The 'weight' must be positive."; 193 throw new IllegalArgumentException(msg); 194 } 195 196 // store the plot and its weight 197 subplot.setParent(this); 198 subplot.setWeight(weight); 199 subplot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0)); 200 subplot.setRangeAxis(null); 201 subplot.addChangeListener(this); 202 this.subplots.add(subplot); 203 configureRangeAxes(); 204 fireChangeEvent(); 205 206 } 207 208 /** 209 * Removes a subplot from the combined chart. 210 * 211 * @param subplot the subplot ({@code null} not permitted). 212 */ 213 public void remove(XYPlot subplot) { 214 Args.nullNotPermitted(subplot, "subplot"); 215 int position = -1; 216 int size = this.subplots.size(); 217 int i = 0; 218 while (position == -1 && i < size) { 219 if (this.subplots.get(i) == subplot) { 220 position = i; 221 } 222 i++; 223 } 224 if (position != -1) { 225 this.subplots.remove(position); 226 subplot.setParent(null); 227 subplot.removeChangeListener(this); 228 configureRangeAxes(); 229 fireChangeEvent(); 230 } 231 } 232 233 /** 234 * Returns the list of subplots. The returned list may be empty, but is 235 * never {@code null}. 236 * 237 * @return An unmodifiable list of subplots. 238 */ 239 public List<XYPlot> getSubplots() { 240 if (this.subplots != null) { 241 return Collections.unmodifiableList(this.subplots); 242 } 243 else { 244 return Collections.EMPTY_LIST; 245 } 246 } 247 248 /** 249 * Calculates the space required for the axes. 250 * 251 * @param g2 the graphics device. 252 * @param plotArea the plot area. 253 * 254 * @return The space required for the axes. 255 */ 256 @Override 257 protected AxisSpace calculateAxisSpace(Graphics2D g2, 258 Rectangle2D plotArea) { 259 260 AxisSpace space = new AxisSpace(); 261 PlotOrientation orientation = getOrientation(); 262 263 // work out the space required by the domain axis... 264 AxisSpace fixed = getFixedRangeAxisSpace(); 265 if (fixed != null) { 266 if (orientation == PlotOrientation.VERTICAL) { 267 space.setLeft(fixed.getLeft()); 268 space.setRight(fixed.getRight()); 269 } 270 else if (orientation == PlotOrientation.HORIZONTAL) { 271 space.setTop(fixed.getTop()); 272 space.setBottom(fixed.getBottom()); 273 } 274 } 275 else { 276 ValueAxis valueAxis = getRangeAxis(); 277 RectangleEdge valueEdge = Plot.resolveRangeAxisLocation( 278 getRangeAxisLocation(), orientation 279 ); 280 if (valueAxis != null) { 281 space = valueAxis.reserveSpace(g2, this, plotArea, valueEdge, 282 space); 283 } 284 } 285 286 Rectangle2D adjustedPlotArea = space.shrink(plotArea, null); 287 // work out the maximum height or width of the non-shared axes... 288 int n = this.subplots.size(); 289 int totalWeight = 0; 290 for (int i = 0; i < n; i++) { 291 XYPlot sub = (XYPlot) this.subplots.get(i); 292 totalWeight += sub.getWeight(); 293 } 294 295 // calculate plotAreas of all sub-plots, maximum vertical/horizontal 296 // axis width/height 297 this.subplotAreas = new Rectangle2D[n]; 298 double x = adjustedPlotArea.getX(); 299 double y = adjustedPlotArea.getY(); 300 double usableSize = 0.0; 301 if (orientation == PlotOrientation.VERTICAL) { 302 usableSize = adjustedPlotArea.getWidth() - this.gap * (n - 1); 303 } 304 else if (orientation == PlotOrientation.HORIZONTAL) { 305 usableSize = adjustedPlotArea.getHeight() - this.gap * (n - 1); 306 } 307 308 for (int i = 0; i < n; i++) { 309 XYPlot plot = (XYPlot) this.subplots.get(i); 310 311 // calculate sub-plot area 312 if (orientation == PlotOrientation.VERTICAL) { 313 double w = usableSize * plot.getWeight() / totalWeight; 314 this.subplotAreas[i] = new Rectangle2D.Double(x, y, w, 315 adjustedPlotArea.getHeight()); 316 x = x + w + this.gap; 317 } 318 else if (orientation == PlotOrientation.HORIZONTAL) { 319 double h = usableSize * plot.getWeight() / totalWeight; 320 this.subplotAreas[i] = new Rectangle2D.Double(x, y, 321 adjustedPlotArea.getWidth(), h); 322 y = y + h + this.gap; 323 } 324 325 AxisSpace subSpace = plot.calculateDomainAxisSpace(g2, 326 this.subplotAreas[i], null); 327 space.ensureAtLeast(subSpace); 328 329 } 330 331 return space; 332 } 333 334 /** 335 * Receives a chart element visitor. Many plot subclasses will override 336 * this method to handle their subcomponents. 337 * 338 * @param visitor the visitor ({@code null} not permitted). 339 */ 340 @Override 341 public void receive(ChartElementVisitor visitor) { 342 subplots.forEach(subplot -> { 343 subplot.receive(visitor); 344 }); 345 super.receive(visitor); 346 347 } 348 349 /** 350 * Draws the plot within the specified area on a graphics device. 351 * 352 * @param g2 the graphics device. 353 * @param area the plot area (in Java2D space). 354 * @param anchor an anchor point in Java2D space ({@code null} permitted). 355 * @param parentState the state from the parent plot, if there is one 356 * ({@code null} permitted). 357 * @param info collects chart drawing information ({@code null} 358 * permitted). 359 */ 360 @Override 361 public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, 362 PlotState parentState, PlotRenderingInfo info) { 363 364 // set up info collection... 365 if (info != null) { 366 info.setPlotArea(area); 367 } 368 369 // adjust the drawing area for plot insets (if any)... 370 RectangleInsets insets = getInsets(); 371 insets.trim(area); 372 373 AxisSpace space = calculateAxisSpace(g2, area); 374 Rectangle2D dataArea = space.shrink(area, null); 375 //this.axisOffset.trim(dataArea); 376 377 // set the width and height of non-shared axis of all sub-plots 378 setFixedDomainAxisSpaceForSubplots(space); 379 380 // draw the shared axis 381 ValueAxis axis = getRangeAxis(); 382 RectangleEdge edge = getRangeAxisEdge(); 383 double cursor = RectangleEdge.coordinate(dataArea, edge); 384 AxisState axisState = axis.draw(g2, cursor, area, dataArea, edge, info); 385 386 if (parentState == null) { 387 parentState = new PlotState(); 388 } 389 parentState.getSharedAxisStates().put(axis, axisState); 390 391 // draw all the charts 392 for (int i = 0; i < this.subplots.size(); i++) { 393 XYPlot plot = (XYPlot) this.subplots.get(i); 394 PlotRenderingInfo subplotInfo = null; 395 if (info != null) { 396 subplotInfo = new PlotRenderingInfo(info.getOwner()); 397 info.addSubplotInfo(subplotInfo); 398 } 399 plot.draw(g2, this.subplotAreas[i], anchor, parentState, 400 subplotInfo); 401 } 402 403 if (info != null) { 404 info.setDataArea(dataArea); 405 } 406 407 } 408 409 /** 410 * Returns a collection of legend items for the plot. 411 * 412 * @return The legend items. 413 */ 414 @Override 415 public LegendItemCollection getLegendItems() { 416 LegendItemCollection result = getFixedLegendItems(); 417 if (result == null) { 418 result = new LegendItemCollection(); 419 420 if (this.subplots != null) { 421 for (XYPlot plot : this.subplots) { 422 LegendItemCollection more = plot.getLegendItems(); 423 result.addAll(more); 424 } 425 } 426 } 427 return result; 428 } 429 430 /** 431 * Multiplies the range on the domain axis/axes by the specified factor. 432 * 433 * @param factor the zoom factor. 434 * @param info the plot rendering info ({@code null} not permitted). 435 * @param source the source point ({@code null} not permitted). 436 */ 437 @Override 438 public void zoomDomainAxes(double factor, PlotRenderingInfo info, 439 Point2D source) { 440 zoomDomainAxes(factor, info, source, false); 441 } 442 443 /** 444 * Multiplies the range on the domain axis/axes by the specified factor. 445 * 446 * @param factor the zoom factor. 447 * @param info the plot rendering info ({@code null} not permitted). 448 * @param source the source point ({@code null} not permitted). 449 * @param useAnchor zoom about the anchor point? 450 */ 451 @Override 452 public void zoomDomainAxes(double factor, PlotRenderingInfo info, 453 Point2D source, boolean useAnchor) { 454 // delegate 'info' and 'source' argument checks... 455 XYPlot subplot = findSubplot(info, source); 456 if (subplot != null) { 457 subplot.zoomDomainAxes(factor, info, source, useAnchor); 458 } 459 else { 460 // if the source point doesn't fall within a subplot, we do the 461 // zoom on all subplots... 462 for (XYPlot plot : getSubplots()) { 463 plot.zoomDomainAxes(factor, info, source, useAnchor); 464 } 465 } 466 } 467 468 /** 469 * Zooms in on the domain axes. 470 * 471 * @param lowerPercent the lower bound. 472 * @param upperPercent the upper bound. 473 * @param info the plot rendering info ({@code null} not permitted). 474 * @param source the source point ({@code null} not permitted). 475 */ 476 @Override 477 public void zoomDomainAxes(double lowerPercent, double upperPercent, 478 PlotRenderingInfo info, Point2D source) { 479 // delegate 'info' and 'source' argument checks... 480 XYPlot subplot = findSubplot(info, source); 481 if (subplot != null) { 482 subplot.zoomDomainAxes(lowerPercent, upperPercent, info, source); 483 } 484 else { 485 // if the source point doesn't fall within a subplot, we do the 486 // zoom on all subplots... 487 for (XYPlot plot : getSubplots()) { 488 plot.zoomDomainAxes(lowerPercent, upperPercent, info, source); 489 } 490 } 491 } 492 493 /** 494 * Pans all domain axes by the specified percentage. 495 * 496 * @param panRange the distance to pan (as a percentage of the axis length). 497 * @param info the plot info 498 * @param source the source point where the pan action started. 499 */ 500 @Override 501 public void panDomainAxes(double panRange, PlotRenderingInfo info, 502 Point2D source) { 503 504 XYPlot subplot = findSubplot(info, source); 505 if (subplot == null) { 506 return; 507 } 508 if (!subplot.isDomainPannable()) { 509 return; 510 } 511 PlotRenderingInfo subplotInfo = info.getSubplotInfo( 512 info.getSubplotIndex(source)); 513 if (subplotInfo == null) { 514 return; 515 } 516 517 for (int i = 0; i < subplot.getDomainAxisCount(); i++) { 518 ValueAxis domainAxis = subplot.getDomainAxis(i); 519 if (domainAxis != null) { 520 domainAxis.pan(panRange); 521 } 522 } 523 } 524 525 /** 526 * Returns the subplot (if any) that contains the (x, y) point (specified 527 * in Java2D space). 528 * 529 * @param info the chart rendering info ({@code null} not permitted). 530 * @param source the source point ({@code null} not permitted). 531 * 532 * @return A subplot (possibly {@code null}). 533 */ 534 public XYPlot findSubplot(PlotRenderingInfo info, Point2D source) { 535 Args.nullNotPermitted(info, "info"); 536 Args.nullNotPermitted(source, "source"); 537 XYPlot result = null; 538 int subplotIndex = info.getSubplotIndex(source); 539 if (subplotIndex >= 0) { 540 result = (XYPlot) this.subplots.get(subplotIndex); 541 } 542 return result; 543 } 544 545 /** 546 * Sets the item renderer FOR ALL SUBPLOTS. Registered listeners are 547 * notified that the plot has been modified. 548 * <P> 549 * Note: usually you will want to set the renderer independently for each 550 * subplot, which is NOT what this method does. 551 * 552 * @param renderer the new renderer. 553 */ 554 @Override 555 public void setRenderer(XYItemRenderer renderer) { 556 super.setRenderer(renderer); // not strictly necessary, since the 557 // renderer set for the 558 // parent plot is not used 559 560 for (XYPlot plot : this.subplots) { 561 plot.setRenderer(renderer); 562 } 563 } 564 565 /** 566 * Sets the orientation for the plot (and all its subplots). 567 * 568 * @param orientation the orientation. 569 */ 570 @Override 571 public void setOrientation(PlotOrientation orientation) { 572 super.setOrientation(orientation); 573 for (XYPlot plot : this.subplots) { 574 plot.setOrientation(orientation); 575 } 576 } 577 578 /** 579 * Sets the shadow generator for the plot (and all subplots) and sends 580 * a {@link PlotChangeEvent} to all registered listeners. 581 * 582 * @param generator the new generator ({@code null} permitted). 583 */ 584 @Override 585 public void setShadowGenerator(ShadowGenerator generator) { 586 setNotify(false); 587 super.setShadowGenerator(generator); 588 for (XYPlot plot : this.subplots) { 589 plot.setShadowGenerator(generator); 590 } 591 setNotify(true); 592 } 593 594 /** 595 * Returns a range representing the extent of the data values in this plot 596 * (obtained from the subplots) that will be rendered against the specified 597 * axis. NOTE: This method is intended for internal JFreeChart use, and 598 * is public only so that code in the axis classes can call it. Since 599 * only the range axis is shared between subplots, the JFreeChart code 600 * will only call this method for the range values (although this is not 601 * checked/enforced). 602 * 603 * @param axis the axis. 604 * 605 * @return The range. 606 */ 607 @Override 608 public Range getDataRange(ValueAxis axis) { 609 Range result = null; 610 if (this.subplots != null) { 611 for (XYPlot subplot : this.subplots) { 612 result = Range.combine(result, subplot.getDataRange(axis)); 613 } 614 } 615 return result; 616 } 617 618 /** 619 * Sets the space (width or height, depending on the orientation of the 620 * plot) for the domain axis of each subplot. 621 * 622 * @param space the space. 623 */ 624 protected void setFixedDomainAxisSpaceForSubplots(AxisSpace space) { 625 for (XYPlot plot : this.subplots) { 626 plot.setFixedDomainAxisSpace(space, false); 627 } 628 } 629 630 /** 631 * Handles a 'click' on the plot by updating the anchor values... 632 * 633 * @param x x-coordinate, where the click occured. 634 * @param y y-coordinate, where the click occured. 635 * @param info object containing information about the plot dimensions. 636 */ 637 @Override 638 public void handleClick(int x, int y, PlotRenderingInfo info) { 639 Rectangle2D dataArea = info.getDataArea(); 640 if (dataArea.contains(x, y)) { 641 for (int i = 0; i < this.subplots.size(); i++) { 642 XYPlot subplot = (XYPlot) this.subplots.get(i); 643 PlotRenderingInfo subplotInfo = info.getSubplotInfo(i); 644 subplot.handleClick(x, y, subplotInfo); 645 } 646 } 647 } 648 649 /** 650 * Receives a {@link PlotChangeEvent} and responds by notifying all 651 * listeners. 652 * 653 * @param event the event. 654 */ 655 @Override 656 public void plotChanged(PlotChangeEvent event) { 657 notifyListeners(event); 658 } 659 660 /** 661 * Tests this plot for equality with another object. 662 * 663 * @param obj the other object. 664 * 665 * @return {@code true} or {@code false}. 666 */ 667 @Override 668 public boolean equals(Object obj) { 669 if (obj == this) { 670 return true; 671 } 672 if (!(obj instanceof CombinedRangeXYPlot)) { 673 return false; 674 } 675 CombinedRangeXYPlot that = (CombinedRangeXYPlot) obj; 676 if (this.gap != that.gap) { 677 return false; 678 } 679 if (!Objects.equals(this.subplots, that.subplots)) { 680 return false; 681 } 682 return super.equals(obj); 683 } 684 685 /** 686 * Returns a clone of the plot. 687 * 688 * @return A clone. 689 * 690 * @throws CloneNotSupportedException this class will not throw this 691 * exception, but subclasses (if any) might. 692 */ 693 @Override 694 public Object clone() throws CloneNotSupportedException { 695 696 CombinedRangeXYPlot<S> result = (CombinedRangeXYPlot) super.clone(); 697 result.subplots = (List) CloneUtils.cloneList(this.subplots); 698 for (XYPlot<S> child : result.subplots) { 699 child.setParent(result); 700 } 701 702 // after setting up all the subplots, the shared range axis may need 703 // reconfiguring 704 ValueAxis rangeAxis = result.getRangeAxis(); 705 if (rangeAxis != null) { 706 rangeAxis.configure(); 707 } 708 709 return result; 710 } 711 712}