001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------
028 * CombinedDomainXYPlot.java
029 * -------------------------
030 * (C) Copyright 2001-2016, by Bill Kelemen and Contributors.
031 *
032 * Original Author:  Bill Kelemen;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *                   Anthony Boulestreau;
035 *                   David Basten;
036 *                   Kevin Frechette (for ISTI);
037 *                   Nicolas Brodu;
038 *                   Petr Kubanek (bug 1606205);
039 *                   Vladimir Shirokov (bug 986);
040 *
041 * Changes:
042 * --------
043 * 06-Dec-2001 : Version 1 (BK);
044 * 12-Dec-2001 : Removed unnecessary 'throws' clause from constructor (DG);
045 * 18-Dec-2001 : Added plotArea attribute and get/set methods (BK);
046 * 22-Dec-2001 : Fixed bug in chartChanged with multiple combinations of
047 *               CombinedPlots (BK);
048 * 08-Jan-2002 : Moved to new package com.jrefinery.chart.combination (DG);
049 * 25-Feb-2002 : Updated import statements (DG);
050 * 28-Feb-2002 : Readded "this.plotArea = plotArea" that was deleted from
051 *               draw() method (BK);
052 * 26-Mar-2002 : Added an empty zoom method (this method needs to be written so
053 *               that combined plots will support zooming (DG);
054 * 29-Mar-2002 : Changed the method createCombinedAxis adding the creation of
055 *               OverlaidSymbolicAxis and CombinedSymbolicAxis(AB);
056 * 23-Apr-2002 : Renamed CombinedPlot-->MultiXYPlot, and simplified the
057 *               structure (DG);
058 * 23-May-2002 : Renamed (again) MultiXYPlot-->CombinedXYPlot (DG);
059 * 19-Jun-2002 : Added get/setGap() methods suggested by David Basten (DG);
060 * 25-Jun-2002 : Removed redundant imports (DG);
061 * 16-Jul-2002 : Draws shared axis after subplots (to fix missing gridlines),
062 *               added overrides of 'setSeriesPaint()' and 'setXYItemRenderer()'
063 *               that pass changes down to subplots (KF);
064 * 09-Oct-2002 : Added add(XYPlot) method (DG);
065 * 26-Mar-2003 : Implemented Serializable (DG);
066 * 16-May-2003 : Renamed CombinedXYPlot --> CombinedDomainXYPlot (DG);
067 * 04-Aug-2003 : Removed leftover code that was causing domain axis drawing
068 *               problem (DG);
069 * 08-Aug-2003 : Adjusted totalWeight in remove() method (DG);
070 * 21-Aug-2003 : Implemented Cloneable (DG);
071 * 11-Sep-2003 : Fix cloning support (subplots) (NB);
072 * 15-Sep-2003 : Fixed error in cloning (DG);
073 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
074 * 17-Sep-2003 : Updated handling of 'clicks' (DG);
075 * 12-Nov-2004 : Implemented the new Zoomable interface (DG);
076 * 25-Nov-2004 : Small update to clone() implementation (DG);
077 * 21-Feb-2005 : The getLegendItems() method now returns the fixed legend
078 *               items if set (DG);
079 * 05-May-2005 : Removed unused draw() method (DG);
080 * ------------- JFREECHART 1.0.x ---------------------------------------------
081 * 23-Aug-2006 : Override setFixedRangeAxisSpace() to update subplots (DG);
082 * 06-Feb-2007 : Fixed bug 1606205, draw shared axis after subplots (DG);
083 * 23-Mar-2007 : Reverted previous patch (bug fix 1606205) (DG);
084 * 17-Apr-2007 : Added null argument checks to findSubplot() (DG);
085 * 27-Nov-2007 : Modified setFixedRangeAxisSpaceForSubplots() so as not to
086 *               trigger change event in subplots (DG);
087 * 28-Jan-2008 : Reset fixed range axis space in subplots for each call to
088 *               draw() (DG);
089 * 27-Mar-2008 : Add documentation for getDataRange() method (DG);
090 * 31-Mar-2008 : Updated getSubplots() to return EMPTY_LIST for null
091 *               subplots, as suggested by Richard West (DG);
092 * 28-Apr-2008 : Fixed zooming problem (see bug 1950037) (DG);
093 * 11-Aug-2008 : Don't store totalWeight of subplots, calculate it as
094 *               required (DG);
095 * 21-Dec-2011 : Apply patch 3447161 by Ulrich Voigt and Martin Hoeller (MH);
096 * 21-Jul-2014 : Override isRangePannable() and setRangePannable() - motivated 
097 *               by patch #304 by Ulrich Voigt (DG);
098 * 31-Aug-2014 : Fix range axis bounds (bug 986, patch by Vladimir 
099 *               Shirokov) (DG);
100 */
101
102package org.jfree.chart.plot;
103
104import java.awt.Graphics2D;
105import java.awt.geom.Point2D;
106import java.awt.geom.Rectangle2D;
107import java.util.Collections;
108import java.util.Iterator;
109import java.util.List;
110
111import org.jfree.chart.LegendItemCollection;
112import org.jfree.chart.axis.AxisSpace;
113import org.jfree.chart.axis.AxisState;
114import org.jfree.chart.axis.NumberAxis;
115import org.jfree.chart.axis.ValueAxis;
116import org.jfree.chart.event.PlotChangeEvent;
117import org.jfree.chart.event.PlotChangeListener;
118import org.jfree.chart.renderer.xy.XYItemRenderer;
119import org.jfree.chart.ui.RectangleEdge;
120import org.jfree.chart.ui.RectangleInsets;
121import org.jfree.chart.util.ObjectUtils;
122import org.jfree.chart.util.Args;
123import org.jfree.chart.util.ShadowGenerator;
124import org.jfree.data.Range;
125import org.jfree.data.general.DatasetChangeEvent;
126import org.jfree.data.xy.XYDataset;
127
128/**
129 * An extension of {@link XYPlot} that contains multiple subplots that share a
130 * common domain axis.
131 */
132public class CombinedDomainXYPlot extends XYPlot
133        implements PlotChangeListener {
134
135    /** For serialization. */
136    private static final long serialVersionUID = -7765545541261907383L;
137
138    /** Storage for the subplot references (possibly empty but never null). */
139    private List<XYPlot> subplots;
140
141    /** The gap between subplots. */
142    private double gap = 5.0;
143
144    /** Temporary storage for the subplot areas. */
145    private transient Rectangle2D[] subplotAreas;
146    // TODO:  the subplot areas needs to be moved out of the plot into the plot
147    //        state
148
149    /**
150     * Default constructor.
151     */
152    public CombinedDomainXYPlot() {
153        this(new NumberAxis());
154    }
155
156    /**
157     * Creates a new combined plot that shares a domain axis among multiple
158     * subplots.
159     *
160     * @param domainAxis  the shared axis.
161     */
162    public CombinedDomainXYPlot(ValueAxis domainAxis) {
163        super(null,        // no data in the parent plot
164              domainAxis,
165              null,        // no range axis
166              null);       // no renderer
167        this.subplots = new java.util.ArrayList<XYPlot>();
168    }
169
170    /**
171     * Returns a string describing the type of plot.
172     *
173     * @return The type of plot.
174     */
175    @Override
176    public String getPlotType() {
177        return "Combined_Domain_XYPlot";
178    }
179
180    /**
181     * Returns the gap between subplots, measured in Java2D units.
182     *
183     * @return The gap (in Java2D units).
184     *
185     * @see #setGap(double)
186     */
187    public double getGap() {
188        return this.gap;
189    }
190
191    /**
192     * Sets the amount of space between subplots and sends a
193     * {@link PlotChangeEvent} to all registered listeners.
194     *
195     * @param gap  the gap between subplots (in Java2D units).
196     *
197     * @see #getGap()
198     */
199    public void setGap(double gap) {
200        this.gap = gap;
201        fireChangeEvent();
202    }
203
204    /**
205     * Returns {@code true} if the range is pannable for at least one subplot,
206     * and {@code false} otherwise.
207     * 
208     * @return A boolean. 
209     */
210    @Override
211    public boolean isRangePannable() {
212        for (XYPlot subplot : this.subplots) {
213            if (subplot.isRangePannable()) {
214                return true;
215            }
216        }
217        return false;
218    }
219    
220    /**
221     * Sets the flag, on each of the subplots, that controls whether or not the 
222     * range is pannable.
223     * 
224     * @param pannable  the new flag value. 
225     */
226    @Override
227    public void setRangePannable(boolean pannable) {
228        for (XYPlot subplot : this.subplots) {
229            subplot.setRangePannable(pannable);
230        }        
231    }
232
233    /**
234     * Sets the orientation for the plot (also changes the orientation for all
235     * the subplots to match).
236     *
237     * @param orientation  the orientation ({@code null} not allowed).
238     */
239    @Override
240    public void setOrientation(PlotOrientation orientation) {
241        super.setOrientation(orientation);
242        for (XYPlot p : this.subplots) {
243            p.setOrientation(orientation);
244        }
245    }
246
247    /**
248     * Sets the shadow generator for the plot (and all subplots) and sends
249     * a {@link PlotChangeEvent} to all registered listeners.
250     * 
251     * @param generator  the new generator ({@code null} permitted).
252     */
253    @Override
254    public void setShadowGenerator(ShadowGenerator generator) {
255        setNotify(false);
256        super.setShadowGenerator(generator);
257        for (XYPlot p : this.subplots) {
258            p.setShadowGenerator(generator);
259        }
260        setNotify(true);
261    }
262
263    /**
264     * Returns a range representing the extent of the data values in this plot
265     * (obtained from the subplots) that will be rendered against the specified
266     * axis.  NOTE: This method is intended for internal JFreeChart use, and
267     * is public only so that code in the axis classes can call it.  Since
268     * only the domain axis is shared between subplots, the JFreeChart code
269     * will only call this method for the domain values (although this is not
270     * checked/enforced).
271     *
272     * @param axis  the axis.
273     *
274     * @return The range (possibly {@code null}).
275     */
276    @Override
277    public Range getDataRange(ValueAxis axis) {
278        if (this.subplots == null) {
279            return null;
280        }
281        Range result = null;
282        for (XYPlot p : this.subplots) {
283            result = Range.combine(result, p.getDataRange(axis));
284        }
285        return result;
286    }
287
288    /**
289     * Adds a subplot (with a default 'weight' of 1) and sends a
290     * {@link PlotChangeEvent} to all registered listeners.
291     * <P>
292     * The domain axis for the subplot will be set to {@code null}.  You
293     * must ensure that the subplot has a non-null range axis.
294     *
295     * @param subplot  the subplot ({@code null} not permitted).
296     */
297    public void add(XYPlot subplot) {
298        // defer argument checking
299        add(subplot, 1);
300    }
301
302    /**
303     * Adds a subplot with the specified weight and sends a
304     * {@link PlotChangeEvent} to all registered listeners.  The weight
305     * determines how much space is allocated to the subplot relative to all
306     * the other subplots.
307     * <P>
308     * The domain axis for the subplot will be set to {@code null}.  You
309     * must ensure that the subplot has a non-null range axis.
310     *
311     * @param subplot  the subplot ({@code null} not permitted).
312     * @param weight  the weight (must be &gt;= 1).
313     */
314    public void add(XYPlot subplot, int weight) {
315        Args.nullNotPermitted(subplot, "subplot");
316        if (weight <= 0) {
317            throw new IllegalArgumentException("Require weight >= 1.");
318        }
319
320        // store the plot and its weight
321        subplot.setParent(this);
322        subplot.setWeight(weight);
323        subplot.setInsets(RectangleInsets.ZERO_INSETS, false);
324        subplot.setDomainAxis(null);
325        subplot.addChangeListener(this);
326        this.subplots.add(subplot);
327
328        ValueAxis axis = getDomainAxis();
329        if (axis != null) {
330            axis.configure();
331        }
332        fireChangeEvent();
333    }
334
335    /**
336     * Removes a subplot from the combined chart and sends a
337     * {@link PlotChangeEvent} to all registered listeners.
338     *
339     * @param subplot  the subplot ({@code null} not permitted).
340     */
341    public void remove(XYPlot subplot) {
342        Args.nullNotPermitted(subplot, "subplot");
343        int position = -1;
344        int size = this.subplots.size();
345        int i = 0;
346        while (position == -1 && i < size) {
347            if (this.subplots.get(i) == subplot) {
348                position = i;
349            }
350            i++;
351        }
352        if (position != -1) {
353            this.subplots.remove(position);
354            subplot.setParent(null);
355            subplot.removeChangeListener(this);
356            ValueAxis domain = getDomainAxis();
357            if (domain != null) {
358                domain.configure();
359            }
360            fireChangeEvent();
361        }
362    }
363
364    /**
365     * Returns the list of subplots.  The returned list may be empty, but is
366     * never {@code null}.
367     *
368     * @return An unmodifiable list of subplots.
369     */
370    public List getSubplots() {
371        return Collections.unmodifiableList(this.subplots);
372    }
373
374    /**
375     * Calculates the axis space required.
376     *
377     * @param g2  the graphics device.
378     * @param plotArea  the plot area.
379     *
380     * @return The space.
381     */
382    @Override
383    protected AxisSpace calculateAxisSpace(Graphics2D g2,
384            Rectangle2D plotArea) {
385
386        AxisSpace space = new AxisSpace();
387        PlotOrientation orientation = getOrientation();
388
389        // work out the space required by the domain axis...
390        AxisSpace fixed = getFixedDomainAxisSpace();
391        if (fixed != null) {
392            if (orientation == PlotOrientation.HORIZONTAL) {
393                space.setLeft(fixed.getLeft());
394                space.setRight(fixed.getRight());
395            }
396            else if (orientation == PlotOrientation.VERTICAL) {
397                space.setTop(fixed.getTop());
398                space.setBottom(fixed.getBottom());
399            }
400        }
401        else {
402            ValueAxis xAxis = getDomainAxis();
403            RectangleEdge xEdge = Plot.resolveDomainAxisLocation(
404                    getDomainAxisLocation(), orientation);
405            if (xAxis != null) {
406                space = xAxis.reserveSpace(g2, this, plotArea, xEdge, space);
407            }
408        }
409
410        Rectangle2D adjustedPlotArea = space.shrink(plotArea, null);
411
412        // work out the maximum height or width of the non-shared axes...
413        int n = this.subplots.size();
414        int totalWeight = 0;
415        for (int i = 0; i < n; i++) {
416            XYPlot sub = (XYPlot) this.subplots.get(i);
417            totalWeight += sub.getWeight();
418        }
419        this.subplotAreas = new Rectangle2D[n];
420        double x = adjustedPlotArea.getX();
421        double y = adjustedPlotArea.getY();
422        double usableSize = 0.0;
423        if (orientation == PlotOrientation.HORIZONTAL) {
424            usableSize = adjustedPlotArea.getWidth() - this.gap * (n - 1);
425        }
426        else if (orientation == PlotOrientation.VERTICAL) {
427            usableSize = adjustedPlotArea.getHeight() - this.gap * (n - 1);
428        }
429
430        for (int i = 0; i < n; i++) {
431            XYPlot plot = (XYPlot) this.subplots.get(i);
432
433            // calculate sub-plot area
434            if (orientation == PlotOrientation.HORIZONTAL) {
435                double w = usableSize * plot.getWeight() / totalWeight;
436                this.subplotAreas[i] = new Rectangle2D.Double(x, y, w,
437                        adjustedPlotArea.getHeight());
438                x = x + w + this.gap;
439            }
440            else if (orientation == PlotOrientation.VERTICAL) {
441                double h = usableSize * plot.getWeight() / totalWeight;
442                this.subplotAreas[i] = new Rectangle2D.Double(x, y,
443                        adjustedPlotArea.getWidth(), h);
444                y = y + h + this.gap;
445            }
446
447            AxisSpace subSpace = plot.calculateRangeAxisSpace(g2,
448                    this.subplotAreas[i], null);
449            space.ensureAtLeast(subSpace);
450
451        }
452
453        return space;
454    }
455
456    /**
457     * Draws the plot within the specified area on a graphics device.
458     *
459     * @param g2  the graphics device.
460     * @param area  the plot area (in Java2D space).
461     * @param anchor  an anchor point in Java2D space ({@code null}
462     *                permitted).
463     * @param parentState  the state from the parent plot, if there is one
464     *                     ({@code null} permitted).
465     * @param info  collects chart drawing information ({@code null}
466     *              permitted).
467     */
468    @Override
469    public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
470            PlotState parentState, PlotRenderingInfo info) {
471
472        // set up info collection...
473        if (info != null) {
474            info.setPlotArea(area);
475        }
476
477        // adjust the drawing area for plot insets (if any)...
478        RectangleInsets insets = getInsets();
479        insets.trim(area);
480
481        setFixedRangeAxisSpaceForSubplots(null);
482        AxisSpace space = calculateAxisSpace(g2, area);
483        Rectangle2D dataArea = space.shrink(area, null);
484
485        // set the width and height of non-shared axis of all sub-plots
486        setFixedRangeAxisSpaceForSubplots(space);
487
488        // draw the shared axis
489        ValueAxis axis = getDomainAxis();
490        RectangleEdge edge = getDomainAxisEdge();
491        double cursor = RectangleEdge.coordinate(dataArea, edge);
492        AxisState axisState = axis.draw(g2, cursor, area, dataArea, edge, info);
493        if (parentState == null) {
494            parentState = new PlotState();
495        }
496        parentState.getSharedAxisStates().put(axis, axisState);
497
498        // draw all the subplots
499        for (int i = 0; i < this.subplots.size(); i++) {
500            XYPlot plot = (XYPlot) this.subplots.get(i);
501            PlotRenderingInfo subplotInfo = null;
502            if (info != null) {
503                subplotInfo = new PlotRenderingInfo(info.getOwner());
504                info.addSubplotInfo(subplotInfo);
505            }
506            plot.draw(g2, this.subplotAreas[i], anchor, parentState,
507                    subplotInfo);
508        }
509
510        if (info != null) {
511            info.setDataArea(dataArea);
512        }
513
514    }
515
516    /**
517     * Returns a collection of legend items for the plot.
518     *
519     * @return The legend items.
520     */
521    @Override
522    public LegendItemCollection getLegendItems() {
523        LegendItemCollection result = getFixedLegendItems();
524        if (result == null) {
525            result = new LegendItemCollection();
526            if (this.subplots != null) {
527                Iterator iterator = this.subplots.iterator();
528                while (iterator.hasNext()) {
529                    XYPlot plot = (XYPlot) iterator.next();
530                    LegendItemCollection more = plot.getLegendItems();
531                    result.addAll(more);
532                }
533            }
534        }
535        return result;
536    }
537
538    /**
539     * Multiplies the range on the range axis/axes by the specified factor.
540     *
541     * @param factor  the zoom factor.
542     * @param info  the plot rendering info ({@code null} not permitted).
543     * @param source  the source point ({@code null} not permitted).
544     */
545    @Override
546    public void zoomRangeAxes(double factor, PlotRenderingInfo info,
547                              Point2D source) {
548        zoomRangeAxes(factor, info, source, false);
549    }
550
551    /**
552     * Multiplies the range on the range axis/axes by the specified factor.
553     *
554     * @param factor  the zoom factor.
555     * @param state  the plot state.
556     * @param source  the source point (in Java2D coordinates).
557     * @param useAnchor  use source point as zoom anchor?
558     */
559    @Override
560    public void zoomRangeAxes(double factor, PlotRenderingInfo state,
561            Point2D source, boolean useAnchor) {
562        // delegate 'state' and 'source' argument checks...
563        XYPlot subplot = findSubplot(state, source);
564        if (subplot != null) {
565            subplot.zoomRangeAxes(factor, state, source, useAnchor);
566        } else {
567            // if the source point doesn't fall within a subplot, we do the
568            // zoom on all subplots...
569            for (XYPlot p : this.subplots) {
570                p.zoomRangeAxes(factor, state, source, useAnchor);
571            }
572        }
573    }
574
575    /**
576     * Zooms in on the range axes.
577     *
578     * @param lowerPercent  the lower bound.
579     * @param upperPercent  the upper bound.
580     * @param info  the plot rendering info ({@code null} not permitted).
581     * @param source  the source point ({@code null} not permitted).
582     */
583    @Override
584    public void zoomRangeAxes(double lowerPercent, double upperPercent,
585                              PlotRenderingInfo info, Point2D source) {
586        // delegate 'info' and 'source' argument checks...
587        XYPlot subplot = findSubplot(info, source);
588        if (subplot != null) {
589            subplot.zoomRangeAxes(lowerPercent, upperPercent, info, source);
590        } else {
591            // if the source point doesn't fall within a subplot, we do the
592            // zoom on all subplots...
593            for (XYPlot p : this.subplots) {
594                p.zoomRangeAxes(lowerPercent, upperPercent, info, source);
595            }
596        }
597    }
598
599    /**
600     * Pans all range axes by the specified percentage.
601     *
602     * @param panRange the distance to pan (as a percentage of the axis length).
603     * @param info  the plot info ({@code null} not permitted).
604     * @param source the source point where the pan action started.
605     *
606     * @since 1.0.15
607     */
608    @Override
609    public void panRangeAxes(double panRange, PlotRenderingInfo info,
610            Point2D source) {
611        XYPlot subplot = findSubplot(info, source);
612        if (subplot == null) {
613            return;
614        }
615        if (!subplot.isRangePannable()) {
616            return;
617        }
618        PlotRenderingInfo subplotInfo = info.getSubplotInfo(
619                info.getSubplotIndex(source));
620        if (subplotInfo == null) {
621            return;
622        }
623        for (int i = 0; i < subplot.getRangeAxisCount(); i++) {
624            ValueAxis rangeAxis = subplot.getRangeAxis(i);
625            if (rangeAxis != null) {
626                rangeAxis.pan(panRange);
627            }
628        }
629    }
630
631    /**
632     * Returns the subplot (if any) that contains the (x, y) point (specified
633     * in Java2D space).
634     *
635     * @param info  the chart rendering info ({@code null} not permitted).
636     * @param source  the source point ({@code null} not permitted).
637     *
638     * @return A subplot (possibly {@code null}).
639     */
640    public XYPlot findSubplot(PlotRenderingInfo info, Point2D source) {
641        Args.nullNotPermitted(info, "info");
642        Args.nullNotPermitted(source, "source");
643        XYPlot result = null;
644        int subplotIndex = info.getSubplotIndex(source);
645        if (subplotIndex >= 0) {
646            result =  (XYPlot) this.subplots.get(subplotIndex);
647        }
648        return result;
649    }
650
651    /**
652     * Sets the item renderer FOR ALL SUBPLOTS.  Registered listeners are
653     * notified that the plot has been modified.
654     * <P>
655     * Note: usually you will want to set the renderer independently for each
656     * subplot, which is NOT what this method does.
657     *
658     * @param renderer the new renderer.
659     */
660    @Override
661    public void setRenderer(XYItemRenderer renderer) {
662        super.setRenderer(renderer);  // not strictly necessary, since the
663                                      // renderer set for the
664                                      // parent plot is not used
665        for (XYPlot p : this.subplots) {
666            p.setRenderer(renderer);
667        }
668    }
669
670    /**
671     * Sets the fixed range axis space and sends a {@link PlotChangeEvent} to
672     * all registered listeners.
673     *
674     * @param space  the space ({@code null} permitted).
675     */
676    @Override
677    public void setFixedRangeAxisSpace(AxisSpace space) {
678        super.setFixedRangeAxisSpace(space);
679        setFixedRangeAxisSpaceForSubplots(space);
680        fireChangeEvent();
681    }
682
683    /**
684     * Sets the size (width or height, depending on the orientation of the
685     * plot) for the domain axis of each subplot.
686     *
687     * @param space  the space.
688     */
689    protected void setFixedRangeAxisSpaceForSubplots(AxisSpace space) {
690        for (XYPlot p : this.subplots) {
691            p.setFixedRangeAxisSpace(space, false);
692        }
693    }
694
695    /**
696     * Handles a 'click' on the plot by updating the anchor values.
697     *
698     * @param x  x-coordinate, where the click occurred.
699     * @param y  y-coordinate, where the click occurred.
700     * @param info  object containing information about the plot dimensions.
701     */
702    @Override
703    public void handleClick(int x, int y, PlotRenderingInfo info) {
704        Rectangle2D dataArea = info.getDataArea();
705        if (dataArea.contains(x, y)) {
706            for (int i = 0; i < this.subplots.size(); i++) {
707                XYPlot subplot = (XYPlot) this.subplots.get(i);
708                PlotRenderingInfo subplotInfo = info.getSubplotInfo(i);
709                subplot.handleClick(x, y, subplotInfo);
710            }
711        }
712    }
713
714    /**
715     * Receives notification of a change to the plot's dataset.
716     * <P>
717     * The axis ranges are updated if necessary.
718     *
719     * @param event  information about the event (not used here).
720     */
721    @Override
722    public void datasetChanged(DatasetChangeEvent event) {
723        super.datasetChanged(event);
724        if (this.subplots == null) {
725            return;  // this can happen during plot construction
726        }
727        XYDataset dataset = null;
728        if (event.getDataset() instanceof XYDataset) {
729            dataset = (XYDataset) event.getDataset();
730        }
731        for (XYPlot subplot : this.subplots) {
732            if (subplot.indexOf(dataset) >= 0) {
733                subplot.configureRangeAxes();
734            }
735        }
736    }
737
738    /**
739     * Receives a {@link PlotChangeEvent} and responds by notifying all
740     * listeners.
741     *
742     * @param event  the event.
743     */
744    @Override
745    public void plotChanged(PlotChangeEvent event) {
746        notifyListeners(event);
747    }
748
749    /**
750     * Tests this plot for equality with another object.
751     *
752     * @param obj  the other object.
753     *
754     * @return {@code true} or {@code false}.
755     */
756    @Override
757    public boolean equals(Object obj) {
758        if (obj == this) {
759            return true;
760        }
761        if (!(obj instanceof CombinedDomainXYPlot)) {
762            return false;
763        }
764        CombinedDomainXYPlot that = (CombinedDomainXYPlot) obj;
765        if (this.gap != that.gap) {
766            return false;
767        }
768        if (!ObjectUtils.equal(this.subplots, that.subplots)) {
769            return false;
770        }
771        return super.equals(obj);
772    }
773
774    /**
775     * Returns a clone of the annotation.
776     *
777     * @return A clone.
778     *
779     * @throws CloneNotSupportedException  this class will not throw this
780     *         exception, but subclasses (if any) might.
781     */
782    @Override
783    public Object clone() throws CloneNotSupportedException {
784
785        CombinedDomainXYPlot result = (CombinedDomainXYPlot) super.clone();
786        result.subplots = (List) ObjectUtils.deepClone(this.subplots);
787        for (Iterator it = result.subplots.iterator(); it.hasNext();) {
788            Plot child = (Plot) it.next();
789            child.setParent(result);
790        }
791
792        // after setting up all the subplots, the shared domain axis may need
793        // reconfiguring
794        ValueAxis domainAxis = result.getDomainAxis();
795        if (domainAxis != null) {
796            domainAxis.configure();
797        }
798
799        return result;
800
801    }
802
803}