Package org.jfree.chart.plot

Source Code of org.jfree.chart.plot.CombinedDomainXYPlot

/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2011, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* -------------------------
* CombinedDomainXYPlot.java
* -------------------------
* (C) Copyright 2001-2008, by Bill Kelemen and Contributors.
*
* Original Author:  Bill Kelemen;
* Contributor(s):   David Gilbert (for Object Refinery Limited);
*                   Anthony Boulestreau;
*                   David Basten;
*                   Kevin Frechette (for ISTI);
*                   Nicolas Brodu;
*                   Petr Kubanek (bug 1606205);
*
* Changes:
* --------
* 06-Dec-2001 : Version 1 (BK);
* 12-Dec-2001 : Removed unnecessary 'throws' clause from constructor (DG);
* 18-Dec-2001 : Added plotArea attribute and get/set methods (BK);
* 22-Dec-2001 : Fixed bug in chartChanged with multiple combinations of
*               CombinedPlots (BK);
* 08-Jan-2002 : Moved to new package com.jrefinery.chart.combination (DG);
* 25-Feb-2002 : Updated import statements (DG);
* 28-Feb-2002 : Readded "this.plotArea = plotArea" that was deleted from
*               draw() method (BK);
* 26-Mar-2002 : Added an empty zoom method (this method needs to be written so
*               that combined plots will support zooming (DG);
* 29-Mar-2002 : Changed the method createCombinedAxis adding the creation of
*               OverlaidSymbolicAxis and CombinedSymbolicAxis(AB);
* 23-Apr-2002 : Renamed CombinedPlot-->MultiXYPlot, and simplified the
*               structure (DG);
* 23-May-2002 : Renamed (again) MultiXYPlot-->CombinedXYPlot (DG);
* 19-Jun-2002 : Added get/setGap() methods suggested by David Basten (DG);
* 25-Jun-2002 : Removed redundant imports (DG);
* 16-Jul-2002 : Draws shared axis after subplots (to fix missing gridlines),
*               added overrides of 'setSeriesPaint()' and 'setXYItemRenderer()'
*               that pass changes down to subplots (KF);
* 09-Oct-2002 : Added add(XYPlot) method (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 16-May-2003 : Renamed CombinedXYPlot --> CombinedDomainXYPlot (DG);
* 04-Aug-2003 : Removed leftover code that was causing domain axis drawing
*               problem (DG);
* 08-Aug-2003 : Adjusted totalWeight in remove() method (DG);
* 21-Aug-2003 : Implemented Cloneable (DG);
* 11-Sep-2003 : Fix cloning support (subplots) (NB);
* 15-Sep-2003 : Fixed error in cloning (DG);
* 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
* 17-Sep-2003 : Updated handling of 'clicks' (DG);
* 12-Nov-2004 : Implemented the new Zoomable interface (DG);
* 25-Nov-2004 : Small update to clone() implementation (DG);
* 21-Feb-2005 : The getLegendItems() method now returns the fixed legend
*               items if set (DG);
* 05-May-2005 : Removed unused draw() method (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 23-Aug-2006 : Override setFixedRangeAxisSpace() to update subplots (DG);
* 06-Feb-2007 : Fixed bug 1606205, draw shared axis after subplots (DG);
* 23-Mar-2007 : Reverted previous patch (bug fix 1606205) (DG);
* 17-Apr-2007 : Added null argument checks to findSubplot() (DG);
* 27-Nov-2007 : Modified setFixedRangeAxisSpaceForSubplots() so as not to
*               trigger change event in subplots (DG);
* 28-Jan-2008 : Reset fixed range axis space in subplots for each call to
*               draw() (DG);
* 27-Mar-2008 : Add documentation for getDataRange() method (DG);
* 31-Mar-2008 : Updated getSubplots() to return EMPTY_LIST for null
*               subplots, as suggested by Richard West (DG);
* 28-Apr-2008 : Fixed zooming problem (see bug 1950037) (DG);
* 11-Aug-2008 : Don't store totalWeight of subplots, calculate it as
*               required (DG);
*
*/

package org.jfree.chart.plot;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import java.util.Map;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.AxisCollection;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.event.PlotChangeListener;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.Range;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.ObjectUtilities;

/**
* An extension of {@link XYPlot} that contains multiple subplots that share a
* common domain axis.
*/
public class CombinedDomainXYPlot extends XYPlot
        implements PlotChangeListener
{

   /**
    * For serialization.
    */
   private static final long serialVersionUID = -7765545541261907383L;

   /**
    * Storage for the subplot references.
    */
   // bantchao private List subplots;
   protected List subplots;

   /**
    * The gap between subplots.
    */
   private double gap = 2.0;

  
  
  
   private double _dblCombinedDomainAxisBoundariesShift_ = 0.0;
  
   /**
    * Temporary storage for the subplot areas.
    */
   private transient Rectangle2D[] subplotAreas;
   // TODO:  the subplot areas needs to be moved out of the plot into the plot
   //        state

   /**
    * Default constructor.
    */
   public CombinedDomainXYPlot()
   {
      this(new NumberAxis());
   }

   /**
    * Creates a new combined plot that shares a domain axis among multiple
    * subplots.
    *
    * @param domainAxis the shared axis.
    */
   public CombinedDomainXYPlot(ValueAxis domainAxis)
   {

      super(
              null, // no data in the parent plot
              domainAxis,
              null, // no range axis
              null // no rendereer
              );

      this.subplots = new java.util.ArrayList();

   }
  
  
   public double getDomainAxisBoundariesShift()
   {
      return this._dblCombinedDomainAxisBoundariesShift_;
   }

   public void setDomainAxisBoundariesShift(double dbl)
   {
      this._dblCombinedDomainAxisBoundariesShift_ = dbl;
   }
  
  

   /**
    * Returns a string describing the type of plot.
    *
    * @return The type of plot.
    */
   @Override
   public String getPlotType()
   {
      return "Combined_Domain_XYPlot";
   }

   /**
    * Sets the orientation for the plot (also changes the orientation for all
    * the subplots to match).
    *
    * @param orientation the orientation (<code>null</code> not allowed).
    */
   @Override
   public void setOrientation(PlotOrientation orientation)
   {

      super.setOrientation(orientation);
      Iterator iterator = this.subplots.iterator();
      while (iterator.hasNext())
      {
         XYPlot plot = (XYPlot) iterator.next();
         plot.setOrientation(orientation);
      }

   }

   /**
    * Returns a range representing the extent of the data values in this plot
    * (obtained from the subplots) that will be rendered against the specified
    * axis. NOTE: This method is intended for internal JFreeChart use, and is
    * public only so that code in the axis classes can call it. Since only the
    * domain axis is shared between subplots, the JFreeChart code will only call
    * this method for the domain values (although this is not checked/enforced).
    *
    * @param axis the axis.
    *
    * @return The range (possibly <code>null</code>).
    */
   @Override
   public Range getDataRange(ValueAxis axis)
   {
      Range result = null;
      if (this.subplots != null)
      {
         Iterator iterator = this.subplots.iterator();
         while (iterator.hasNext())
         {
            XYPlot subplot = (XYPlot) iterator.next();
            result = Range.combine(result, subplot.getDataRange(axis));
         }
      }
      return result;
   }

   /**
    * Returns the gap between subplots, measured in Java2D units.
    *
    * @return The gap (in Java2D units).
    */
   public double getGap()
   {
      return this.gap;
   }

   /**
    * Sets the amount of space between subplots and sends a
    * {@link PlotChangeEvent} to all registered listeners.
    *
    * @param gap the gap between subplots (in Java2D units).
    */
   public void setGap(double gap)
   {
      this.gap = gap;
      fireChangeEvent();
   }

   /**
    * Adds a subplot (with a default 'weight' of 1) and sends a
    * {@link PlotChangeEvent} to all registered listeners. <P> The domain axis
    * for the subplot will be set to
    * <code>null</code>. You must ensure that the subplot has a non-null range
    * axis.
    *
    * @param subplot the subplot (<code>null</code> not permitted).
    */
   public void add(XYPlot subplot)
   {
      // defer argument checking
      add(subplot, 1);
   }

   /**
    * Adds a subplot with the specified weight and sends a
    * {@link PlotChangeEvent} to all registered listeners. The weight determines
    * how much space is allocated to the subplot relative to all the other
    * subplots. <P> The domain axis for the subplot will be set to
    * <code>null</code>. You must ensure that the subplot has a non-null range
    * axis.
    *
    * @param subplot the subplot (<code>null</code> not permitted).
    * @param weight the weight (must be >= 1).
    */
   public void add(XYPlot subplot, int weight)
   {

      if (subplot == null)
      {
         throw new IllegalArgumentException("Null 'subplot' argument.");
      }
      if (weight <= 0)
      {
         throw new IllegalArgumentException("Require weight >= 1.");
      }

      // store the plot and its weight
      subplot.setParent(this);
      subplot.setWeight(weight);
      subplot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0), false);
      subplot.setDomainAxis(null);
      subplot.addChangeListener(this);
      this.subplots.add(subplot);

      ValueAxis axis = getDomainAxis();
      if (axis != null)
      {
         axis.configure();
      }
      fireChangeEvent();
   }

   /**
    * Removes a subplot from the combined chart and sends a
    * {@link PlotChangeEvent} to all registered listeners.
    *
    * @param subplot the subplot (<code>null</code> not permitted).
    */
   public void remove(XYPlot subplot)
   {
      if (subplot == null)
      {
         throw new IllegalArgumentException(" Null 'subplot' argument.");
      }
      int position = -1;
      int size = this.subplots.size();
      int i = 0;
      while (position == -1 && i < size)
      {
         if (this.subplots.get(i) == subplot)
         {
            position = i;
         }
         i++;
      }
      if (position != -1)
      {
         this.subplots.remove(position);
         subplot.setParent(null);
         subplot.removeChangeListener(this);
         ValueAxis domain = getDomainAxis();
         if (domain != null)
         {
            domain.configure();
         }
         fireChangeEvent();
      }
   }

   /**
    * Returns the list of subplots. The returned list may be empty, but is never
    * <code>null</code>.
    *
    * @return An unmodifiable list of subplots.
    */
   public List getSubplots()
   {
      if (this.subplots != null)
      {
         return Collections.unmodifiableList(this.subplots);
      }
      else
      {
         return Collections.EMPTY_LIST;
      }
   }

   /**
    * Calculates the axis space required.
    *
    * @param g2 the graphics device.
    * @param plotArea the plot area.
    *
    * @return The space.
    */
   @Override
   protected AxisSpace calculateAxisSpace(
           Graphics2D g2,
           Rectangle2D plotArea)
   {

      AxisSpace space = new AxisSpace();
      PlotOrientation orientation = getOrientation();

      // work out the space required by the domain axis...
      AxisSpace fixed = getFixedDomainAxisSpace();

      if (fixed != null)
      {
         if (orientation == PlotOrientation.HORIZONTAL)
         {
            space.setLeft(fixed.getLeft());
            space.setRight(fixed.getRight());
         }
         else if (orientation == PlotOrientation.VERTICAL)
         {
            space.setTop(fixed.getTop());
            space.setBottom(fixed.getBottom());
         }
      }
      else
      {
         // reserve space for the domain axes...
         for (int i = 0; i < super.getDomainAxes().size(); i++)
         {
            Axis axisCur = (Axis) super.getDomainAxes().get(i);

            if (axisCur != null)
            {
               RectangleEdge edge = getDomainAxisEdge(i);
               space = axisCur.reserveSpace(g2, this, plotArea, edge, space);
            }
         }


         /*ValueAxis xAxis = getDomainAxis();
        
         RectangleEdge xEdge = Plot.resolveDomainAxisLocation(
         getDomainAxisLocation(), orientation);
        
         if (xAxis != null)
         {
         space = xAxis.reserveSpace(g2, this, plotArea, xEdge, space);
         }*/
      }



      Rectangle2D adjustedPlotArea = space.shrink(plotArea, null);

      // work out the maximum height or width of the non-shared axes...
      int n = this.subplots.size();
      int totalWeight = 0;

      for (int i = 0; i < n; i++)
      {
         XYPlot sub = (XYPlot) this.subplots.get(i);
         totalWeight += sub.getWeight();
      }


      this.subplotAreas = new Rectangle2D[n];

      double x = adjustedPlotArea.getX();
      double y = adjustedPlotArea.getY();
      double usableSize = 0.0;


      if (orientation == PlotOrientation.HORIZONTAL)
      {
         usableSize = adjustedPlotArea.getWidth() - this.gap * (n - 1);
      }
      else if (orientation == PlotOrientation.VERTICAL)
      {
         usableSize = adjustedPlotArea.getHeight() - this.gap * (n - 1);
      }

      for (int i = 0; i < n; i++)
      {
         XYPlot plot = (XYPlot) this.subplots.get(i);

         // calculate sub-plot area
         if (orientation == PlotOrientation.HORIZONTAL)
         {
            double w = usableSize * plot.getWeight() / totalWeight;

            this.subplotAreas[i] = new Rectangle2D.Double(
                    x,
                    y,
                    w,
                    adjustedPlotArea.getHeight());

            x += w + this.gap;
         }
         else if (orientation == PlotOrientation.VERTICAL)
         {
            double h = usableSize * plot.getWeight() / totalWeight;

            this.subplotAreas[i] = new Rectangle2D.Double(x, y,
                    adjustedPlotArea.getWidth(), h);

            y += h + this.gap;
         }

         AxisSpace subSpace = plot.calculateRangeAxisSpace(g2,
                 this.subplotAreas[i], null);
        
        
        

         space.ensureAtLeast(subSpace);

      }

      /*AxisSpace newSpace = new AxisSpace();
      newSpace.setBottom(space.getBottom() + 4);
      newSpace.setLeft(space.getLeft());
      newSpace.setRight(space.getRight());
      newSpace.setTop(space.getTop() + 4);*/
     
     
      return space;
   }

   protected Map drawAxesShared(
           Graphics2D g2,
           Rectangle2D plotArea,
           Rectangle2D dataArea,
           PlotRenderingInfo plotState)
   {
      Map axisStateMap = new HashMap();
     
     
      AxisCollection axisCollection = new AxisCollection();

      // add domain axes to lists...
      for (int index = 0; index < super.getDomainAxes().size(); index++)
      {
         ValueAxis axis = (ValueAxis) super.getDomainAxes().get(index);
        
         if (axis != null)
         {
            axisCollection.add(axis, getDomainAxisEdge(index));
         }
      }
     
     
      // draw the top axes
      double cursor = dataArea.getMinY() - this.axisOffset.calculateTopOutset(
              dataArea.getHeight());
     
      Iterator iterator = axisCollection.getAxesAtTop().iterator();
     
      while (iterator.hasNext())
      {
         ValueAxis axis = (ValueAxis) iterator.next();
        
         AxisState info = axis.draw(g2, cursor, plotArea, dataArea,
                 RectangleEdge.TOP, plotState);
        
         cursor = info.getCursor();
         axisStateMap.put(axis, info);
      }

      // draw the bottom axes
      cursor = dataArea.getMaxY()
              + this.axisOffset.calculateBottomOutset(dataArea.getHeight());
     
      iterator = axisCollection.getAxesAtBottom().iterator();
     
      while (iterator.hasNext())
      {
         ValueAxis axis = (ValueAxis) iterator.next();
        
         AxisState info = axis.draw(
                 g2,
                 cursor,
                 plotArea,
                 dataArea,
                 RectangleEdge.BOTTOM,
                 plotState);
        
         cursor = info.getCursor();
         axisStateMap.put(axis, info);
      }

      // draw the left axes
      cursor = dataArea.getMinX()
              - this.axisOffset.calculateLeftOutset(dataArea.getWidth());
     
      iterator = axisCollection.getAxesAtLeft().iterator();
     
     
      while (iterator.hasNext())
      {
         ValueAxis axis = (ValueAxis) iterator.next();
        
         AxisState info = axis.draw(g2,
                 cursor,
                 plotArea,
                 dataArea,
                 RectangleEdge.LEFT,
                 plotState);
        
         cursor = info.getCursor();
         axisStateMap.put(axis, info);
      }

      // draw the right axes
      cursor = dataArea.getMaxX()
              + this.axisOffset.calculateRightOutset(dataArea.getWidth());
     
      iterator = axisCollection.getAxesAtRight().iterator();
     
      while (iterator.hasNext())
      {
         ValueAxis axis = (ValueAxis) iterator.next();
         AxisState info = axis.draw(g2, cursor, plotArea, dataArea,
                 RectangleEdge.RIGHT, plotState);
         cursor = info.getCursor();
         axisStateMap.put(axis, info);
      }
     
     
//--
     /* ValueAxis axis = getDomainAxis();
      RectangleEdge edge = getDomainAxisEdge();
      double cursor = RectangleEdge.coordinate(dataArea, edge);
      AxisState axisState = axis.draw(g2, cursor, plotArea, dataArea, edge, plotState);
      axisStateMap.put(axis, axisState);*/
      //--
     
     
      return axisStateMap;
   }

   /**
    * Draws the plot within the specified area on a graphics device.
    *
    * @param g2 the graphics device.
    * @param area the plot area (in Java2D space).
    * @param anchor an anchor point in Java2D space (<code>null</code>
    * permitted).
    * @param parentState the state from the parent plot, if there is one
    * (<code>null</code> permitted).
    * @param info collects chart drawing information (<code>null</code>
    * permitted).
    */
   @Override
   public void draw(Graphics2D g2,
           Rectangle2D area,
           Point2D anchor,
           PlotState parentState,
           PlotRenderingInfo info)
   {

      // set up info collection...
      if (info != null)
      {
         info.setPlotArea(area);
      }

      // adjust the drawing area for plot insets (if any)...
      RectangleInsets insets = getInsets();
      insets.trim(area);

      setFixedRangeAxisSpaceForSubplots(null);
     
      //--beg modified amadeus
      //This code has been modified to fix a bug with Combine Domain XY Plot axis
      //The axis dimensioin were too big.
      AxisSpace space = calculateAxisSpace(g2, area);
     
      space.setBottom(space.getBottom() + this._dblCombinedDomainAxisBoundariesShift_);
      space.setTop(space.getTop() + this._dblCombinedDomainAxisBoundariesShift_);
     
     
      Rectangle2D dataArea = space.shrink(area, null);
     
      space.setBottom(space.getBottom() - this._dblCombinedDomainAxisBoundariesShift_);
      space.setTop(space.getTop() - this._dblCombinedDomainAxisBoundariesShift_);
     
      dataArea = integerise(dataArea);
      //end modified amadeus
    
      //dataArea = new Rectangle2D.Double();
      //dataArea.setRect(56, 27, 327, 240);

      // set the width and height of non-shared axis of all sub-plots
      setFixedRangeAxisSpaceForSubplots(space);

      /*// draw the shared axis // orig
      ValueAxis axis = getDomainAxis();
      RectangleEdge edge = getDomainAxisEdge();
      double cursor = RectangleEdge.coordinate(dataArea, edge);
      AxisState axisState = axis.draw(g2, cursor, area, dataArea, edge, info);*/


      //--beg modified amadeus
      // draw the shared axes
      Map axisStateMap = drawAxesShared(g2, area, dataArea, info);

      AxisState axisState = (AxisState) axisStateMap.get(
              getDomainAxis());

      ValueAxis axis = getDomainAxis();

      //-- end modified amadeus

      if (parentState == null)
         parentState = new PlotState();


      parentState.getSharedAxisStates().put(axis, axisState);

      // draw all the subplots
      for (int i = 0; i < this.subplots.size(); i++)
      {
         XYPlot plot = (XYPlot) this.subplots.get(i);
         PlotRenderingInfo subplotInfo = null;
         if (info != null)
         {
            subplotInfo = new PlotRenderingInfo(info.getOwner());
            info.addSubplotInfo(subplotInfo);
         }
         plot.draw(g2, this.subplotAreas[i], anchor, parentState,
                 subplotInfo);
      }

      if (info != null)
      {
         info.setDataArea(dataArea);
      }

   }

   /**
    * Returns a collection of legend items for the plot.
    *
    * @return The legend items.
    */
   @Override
   public LegendItemCollection getLegendItems()
   {
      LegendItemCollection result = getFixedLegendItems();
      if (result == null)
      {
         result = new LegendItemCollection();
         if (this.subplots != null)
         {
            Iterator iterator = this.subplots.iterator();
            while (iterator.hasNext())
            {
               XYPlot plot = (XYPlot) iterator.next();
               LegendItemCollection more = plot.getLegendItems();
               result.addAll(more);
            }
         }
      }
      return result;
   }

   /**
    * Multiplies the range on the range axis/axes by the specified factor.
    *
    * @param factor the zoom factor.
    * @param info the plot rendering info (<code>null</code> not permitted).
    * @param source the source point (<code>null</code> not permitted).
    */
   @Override
   public void zoomRangeAxes(double factor, PlotRenderingInfo info,
           Point2D source)
   {
      zoomRangeAxes(factor, info, source, false);
   }

   /**
    * Multiplies the range on the range axis/axes by the specified factor.
    *
    * @param factor the zoom factor.
    * @param state the plot state.
    * @param source the source point (in Java2D coordinates).
    * @param useAnchor use source point as zoom anchor?
    */
   @Override
   public void zoomRangeAxes(double factor, PlotRenderingInfo state,
           Point2D source, boolean useAnchor)
   {
      // delegate 'state' and 'source' argument checks...
      XYPlot subplot = findSubplot(state, source);
      if (subplot != null)
      {
         subplot.zoomRangeAxes(factor, state, source, useAnchor);
      }
      else
      {
         // if the source point doesn't fall within a subplot, we do the
         // zoom on all subplots...
         Iterator iterator = getSubplots().iterator();
         while (iterator.hasNext())
         {
            subplot = (XYPlot) iterator.next();
            subplot.zoomRangeAxes(factor, state, source, useAnchor);
         }
      }
   }

   /**
    * Zooms in on the range axes.
    *
    * @param lowerPercent the lower bound.
    * @param upperPercent the upper bound.
    * @param info the plot rendering info (<code>null</code> not permitted).
    * @param source the source point (<code>null</code> not permitted).
    */
   @Override
   public void zoomRangeAxes(double lowerPercent, double upperPercent,
           PlotRenderingInfo info, Point2D source)
   {
      // delegate 'info' and 'source' argument checks...
      XYPlot subplot = findSubplot(info, source);
      if (subplot != null)
      {
         subplot.zoomRangeAxes(lowerPercent, upperPercent, info, source);
      }
      else
      {
         // if the source point doesn't fall within a subplot, we do the
         // zoom on all subplots...
         Iterator iterator = getSubplots().iterator();
         while (iterator.hasNext())
         {
            subplot = (XYPlot) iterator.next();
            subplot.zoomRangeAxes(lowerPercent, upperPercent, info, source);
         }
      }
   }

   /**
    * Returns the subplot (if any) that contains the (x, y) point (specified in
    * Java2D space).
    *
    * @param info the chart rendering info (<code>null</code> not permitted).
    * @param source the source point (<code>null</code> not permitted).
    *
    * @return A subplot (possibly <code>null</code>).
    */
   public XYPlot findSubplot(PlotRenderingInfo info, Point2D source)
   {
      if (info == null)
      {
         throw new IllegalArgumentException("Null 'info' argument.");
      }
      if (source == null)
      {
         throw new IllegalArgumentException("Null 'source' argument.");
      }
      XYPlot result = null;
      int subplotIndex = info.getSubplotIndex(source);
      if (subplotIndex >= 0)
      {
         result = (XYPlot) this.subplots.get(subplotIndex);
      }
      return result;
   }

   /**
    * Sets the item renderer FOR ALL SUBPLOTS. Registered listeners are notified
    * that the plot has been modified. <P> Note: usually you will want to set
    * the renderer independently for each subplot, which is NOT what this method
    * does.
    *
    * @param renderer the new renderer.
    */
   @Override
   public void setRenderer(XYItemRenderer renderer)
   {

      super.setRenderer(renderer)// not strictly necessary, since the
      // renderer set for the
      // parent plot is not used

      Iterator iterator = this.subplots.iterator();
      while (iterator.hasNext())
      {
         XYPlot plot = (XYPlot) iterator.next();
         plot.setRenderer(renderer);
      }

   }

   /**
    * Sets the fixed range axis space and sends a {@link PlotChangeEvent} to all
    * registered listeners.
    *
    * @param space the space (<code>null</code> permitted).
    */
   @Override
   public void setFixedRangeAxisSpace(AxisSpace space)
   {
      super.setFixedRangeAxisSpace(space);
      setFixedRangeAxisSpaceForSubplots(space);
      fireChangeEvent();
   }

   /**
    * Sets the size (width or height, depending on the orientation of the plot)
    * for the domain axis of each subplot.
    *
    * @param space the space.
    */
   protected void setFixedRangeAxisSpaceForSubplots(AxisSpace space)
   {
      Iterator iterator = this.subplots.iterator();
      while (iterator.hasNext())
      {
         XYPlot plot = (XYPlot) iterator.next();
         plot.setFixedRangeAxisSpace(space, false);
      }
   }

   /**
    * Handles a 'click' on the plot by updating the anchor values.
    *
    * @param x x-coordinate, where the click occured.
    * @param y y-coordinate, where the click occured.
    * @param info object containing information about the plot dimensions.
    */
   @Override
   public void handleClick(int x, int y, PlotRenderingInfo info)
   {
      Rectangle2D dataArea = info.getDataArea();
      if (dataArea.contains(x, y))
      {
         for (int i = 0; i < this.subplots.size(); i++)
         {
            XYPlot subplot = (XYPlot) this.subplots.get(i);
            PlotRenderingInfo subplotInfo = info.getSubplotInfo(i);
            subplot.handleClick(x, y, subplotInfo);
         }
      }
   }

   /**
    * Receives a {@link PlotChangeEvent} and responds by notifying all
    * listeners.
    *
    * @param event the event.
    */
   @Override
   public void plotChanged(PlotChangeEvent event)
   {
      notifyListeners(event);
   }

   /**
    * Tests this plot for equality with another object.
    *
    * @param obj the other object.
    *
    * @return <code>true</code> or <code>false</code>.
    */
   @Override
   public boolean equals(Object obj)
   {
      if (obj == this)
      {
         return true;
      }
      if (!(obj instanceof CombinedDomainXYPlot))
      {
         return false;
      }
      CombinedDomainXYPlot that = (CombinedDomainXYPlot) obj;
      if (this.gap != that.gap)
      {
         return false;
      }
      if (!ObjectUtilities.equal(this.subplots, that.subplots))
      {
         return false;
      }
      return super.equals(obj);
   }

   /**
    * Returns a clone of the annotation.
    *
    * @return A clone.
    *
    * @throws CloneNotSupportedException this class will not throw this
    * exception, but subclasses (if any) might.
    */
   @Override
   public Object clone() throws CloneNotSupportedException
   {

      CombinedDomainXYPlot result = (CombinedDomainXYPlot) super.clone();
      result.subplots = (List) ObjectUtilities.deepClone(this.subplots);
      for (Iterator it = result.subplots.iterator(); it.hasNext();)
      {
         Plot child = (Plot) it.next();
         child.setParent(result);
      }

      // after setting up all the subplots, the shared domain axis may need
      // reconfiguring
      ValueAxis domainAxis = result.getDomainAxis();
      if (domainAxis != null)
      {
         domainAxis.configure();
      }

      return result;

   }

}
TOP

Related Classes of org.jfree.chart.plot.CombinedDomainXYPlot

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.