Package com.positive.charts.plot

Source Code of com.positive.charts.plot.CategoryPlot

package com.positive.charts.plot;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import com.positive.charting.annotations.CategoryAnnotation;
import com.positive.charts.axis.Axis;
import com.positive.charts.axis.AxisCollection;
import com.positive.charts.axis.AxisLocation;
import com.positive.charts.axis.AxisSpace;
import com.positive.charts.axis.AxisState;
import com.positive.charts.axis.CategoryAnchor;
import com.positive.charts.axis.CategoryAxis;
import com.positive.charts.axis.ValueAxis;
import com.positive.charts.axis.ticks.ValueTick;
import com.positive.charts.block.RectangleInsets;
import com.positive.charts.common.Layer;
import com.positive.charts.common.RectangleEdge;
import com.positive.charts.common.SortOrder;
import com.positive.charts.data.Range;
import com.positive.charts.data.category.CategoryDataset;
import com.positive.charts.data.util.DatasetUtilities;
import com.positive.charts.data.util.ObjectList;
import com.positive.charts.data.util.ObjectUtilities;
import com.positive.charts.data.util.PublicCloneable;
import com.positive.charts.event.ChartChangeEventType;
import com.positive.charts.event.DatasetChangeEvent;
import com.positive.charts.event.PlotChangeEvent;
import com.positive.charts.event.RendererChangeEvent;
import com.positive.charts.event.RendererChangeListener;
import com.positive.charts.legend.LegendItem;
import com.positive.charts.legend.LegendItemCollection;
import com.positive.charts.renderer.category.CategoryItemRenderer;
import com.positive.charts.renderer.category.CategoryItemRendererState;
import com.positive.charts.util.GCUtilities;
import com.positive.charts.util.Line;
import com.positive.charts.util.RectangleUtil;
import com.positive.charts.util.Stroke;

/**
* A general plotting class that uses data from a {@link CategoryDataset} and
* renders each data item using a {@link CategoryItemRenderer}.
*/
public class CategoryPlot extends Plot implements ValueAxisPlot, Zoomable,
    RendererChangeListener, Cloneable, PublicCloneable, Serializable {

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

  public static String COLOR_GRIDLINE_PAINT = "CategoryPlot.defaultGridLineColor";

  public static String FONT_VALUE_LABEL = "CategoryPlot.defaultValueLabelFont";

  /** A stroke style used to draw range gridlines. */
  public static final String STROKE_RANGE_GRIDLINE = "CategoryPlot.rangeGridlineStroke"; //$NON-NLS-1$

  /**
   * The default visibility of the grid lines plotted against the domain axis.
   */
  public static final boolean DEFAULT_DOMAIN_GRIDLINES_VISIBLE = false;

  /**
   * The default visibility of the grid lines plotted against the range axis.
   */
  public static final boolean DEFAULT_RANGE_GRIDLINES_VISIBLE = true;

  /** The default grid line stroke. */
  public static final Stroke DEFAULT_GRIDLINE_STROKE = new Stroke(1,
      SWT.CAP_FLAT, SWT.JOIN_BEVEL);

  /** The default grid line paint. */
  // public static final Color DEFAULT_GRIDLINE_PAINT = Color.lightGray;
  /** The plot orientation. */
  private PlotOrientation orientation;

  /** The offset between the data area and the axes. */
  private RectangleInsets axisOffset;

  /** Storage for the domain axes. */
  private final ObjectList domainAxes;

  /** Storage for the domain axis locations. */
  private final ObjectList domainAxisLocations;

  /**
   * A flag that controls whether or not the shared domain axis is drawn (only
   * relevant when the plot is being used as a subplot).
   */
  private boolean drawSharedDomainAxis;

  /** Storage for the range axes. */
  private final ObjectList rangeAxes;

  /** Storage for the range axis locations. */
  private final ObjectList rangeAxisLocations;

  /** Storage for the datasets. */
  private final ObjectList datasets;

  /** Storage for keys that map datasets to domain axes. */
  private final ObjectList datasetToDomainAxisMap;

  /** Storage for keys that map datasets to range axes. */
  private final ObjectList datasetToRangeAxisMap;

  /** Storage for the renderers. */
  private final ObjectList renderers;

  /** The dataset rendering order. */
  private DatasetRenderingOrder renderingOrder = DatasetRenderingOrder.REVERSE;

  /**
   * Controls the order in which the columns are traversed when rendering the
   * data items.
   */
  private SortOrder columnRenderingOrder = SortOrder.ASCENDING;

  /**
   * Controls the order in which the rows are traversed when rendering the
   * data items.
   */
  private SortOrder rowRenderingOrder = SortOrder.ASCENDING;

  /**
   * A flag that controls whether the grid-lines for the domain axis are
   * visible.
   */
  private boolean domainGridlinesVisible;

  /** The position of the domain gridlines relative to the category. */
  private CategoryAnchor domainGridlinePosition;

  /** The stroke used to draw the domain grid-lines. */
  private transient Stroke domainGridlineStroke;

  /** The paint used to draw the domain grid-lines. */
  private transient Color domainGridlinePaint;

  /**
   * A flag that controls whether the grid-lines for the range axis are
   * visible.
   */
  private boolean rangeGridlinesVisible;

  /** The stroke used to draw the range axis grid-lines. */
  private transient Stroke rangeGridlineStroke;

  /** The paint used to draw the range axis grid-lines. */
  private transient Color rangeGridlinePaint;

  /** The anchor value. */
  private double anchorValue;

  /** A flag that controls whether or not a range crosshair is drawn.. */
  private boolean rangeCrosshairVisible;

  /** The range crosshair value. */
  private double rangeCrosshairValue;

  /** The pen/brush used to draw the crosshair (if any). */
  private transient Stroke rangeCrosshairStroke;

  /** The color used to draw the crosshair (if any). */
  private transient Color rangeCrosshairPaint;

  /**
   * A flag that controls whether or not the crosshair locks onto actual data
   * points.
   */
  private boolean rangeCrosshairLockedOnData = true;

  /** A map containing lists of markers for the domain axes. */
  private final Map foregroundDomainMarkers;

  /** A map containing lists of markers for the domain axes. */
  private final Map backgroundDomainMarkers;

  /** A map containing lists of markers for the range axes. */
  private final Map foregroundRangeMarkers;

  /** A map containing lists of markers for the range axes. */
  private final Map backgroundRangeMarkers;

  /**
   * A (possibly empty) list of annotations for the plot. The list should be
   * initialised in the constructor and never allowed to be <code>null</code>.
   */
  private final List annotations;

  /**
   * The weight for the plot (only relevant when the plot is used as a subplot
   * within a combined plot).
   */
  private int weight;

  /** The fixed space for the domain axis. */
  private AxisSpace fixedDomainAxisSpace;

  /** The fixed space for the range axis. */
  private AxisSpace fixedRangeAxisSpace;

  /**
   * An optional collection of legend items that can be returned by the
   * getLegendItems() method.
   */
  private LegendItemCollection fixedLegendItems;

  /**
   * Default constructor.
   */
  public CategoryPlot() {
    this(null, null, null, null);
  }

  /**
   * Creates a new plot.
   *
   * @param dataset
   *            the dataset (<code>null</code> permitted).
   * @param domainAxis
   *            the domain axis (<code>null</code> permitted).
   * @param rangeAxis
   *            the range axis (<code>null</code> permitted).
   * @param renderer
   *            the item renderer (<code>null</code> permitted).
   *
   */
  public CategoryPlot(final CategoryDataset dataset,
      final CategoryAxis domainAxis, final ValueAxis rangeAxis,
      final CategoryItemRenderer renderer) {

    super();

    this.orientation = PlotOrientation.VERTICAL;

    // allocate storage for dataset, axes and renderers
    this.domainAxes = new ObjectList();
    this.domainAxisLocations = new ObjectList();
    this.rangeAxes = new ObjectList();
    this.rangeAxisLocations = new ObjectList();

    this.datasetToDomainAxisMap = new ObjectList();
    this.datasetToRangeAxisMap = new ObjectList();

    this.renderers = new ObjectList();

    this.datasets = new ObjectList();
    this.datasets.set(0, dataset);
    if (dataset != null) {
      dataset.addChangeListener(this);
    }

    this.axisOffset = RectangleInsets.ZERO_INSETS;

    this.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT, false);
    this.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT, false);

    this.renderers.set(0, renderer);
    if (renderer != null) {
      renderer.setPlot(this);
      renderer.addChangeListener(this);
    }

    this.domainAxes.set(0, domainAxis);
    this.mapDatasetToDomainAxis(0, 0);
    if (domainAxis != null) {
      domainAxis.setPlot(this);
      domainAxis.addChangeListener(this);
    }
    this.drawSharedDomainAxis = false;

    this.rangeAxes.set(0, rangeAxis);
    this.mapDatasetToRangeAxis(0, 0);
    if (rangeAxis != null) {
      rangeAxis.setPlot(this);
      rangeAxis.addChangeListener(this);
    }

    this.configureDomainAxes();
    this.configureRangeAxes();

    this.domainGridlinesVisible = DEFAULT_DOMAIN_GRIDLINES_VISIBLE;
    this.domainGridlinePosition = CategoryAnchor.MIDDLE;
    this.domainGridlineStroke = DEFAULT_GRIDLINE_STROKE;

    this.rangeGridlinesVisible = DEFAULT_RANGE_GRIDLINES_VISIBLE;
    this.rangeGridlineStroke = DEFAULT_GRIDLINE_STROKE;

    this.foregroundDomainMarkers = new HashMap();
    this.backgroundDomainMarkers = new HashMap();
    this.foregroundRangeMarkers = new HashMap();
    this.backgroundRangeMarkers = new HashMap();

    // TODO : SIMPLIFICATION was done.
    // Marker baseline = new ValueMarker(0.0, new Color(0.8f, 0.8f, 0.8f,
    // 0.5f), new BasicStroke(1.0f), new Color(0.85f, 0.85f, 0.95f,
    // 0.5f), new BasicStroke(1.0f), 0.6f);
    // Marker baseline = new ValueMarker();
    // addRangeMarker(baseline, Layer.BACKGROUND);

    this.anchorValue = 0.0;
    this.annotations = new java.util.ArrayList();

  }

  /**
   * Adds an annotation to the plot and sends a {@link PlotChangeEvent} to all
   * registered listeners.
   *
   * @param annotation
   *            the annotation (<code>null</code> not permitted).
   */
  public void addAnnotation(final CategoryAnnotation annotation) {
    if (annotation == null) {
      throw new IllegalArgumentException("Null 'annotation' argument.");
    }
    this.annotations.add(annotation);
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Adds a marker for display (in the foreground) against the domain axis and
   * sends a {@link PlotChangeEvent} to all registered listeners. Typically a
   * marker will be drawn by the renderer as a line perpendicular to the
   * domain axis, however this is entirely up to the renderer.
   *
   * @param marker
   *            the marker (<code>null</code> not permitted).
   */
  public void addDomainMarker(final CategoryMarker marker) {
    this.addDomainMarker(marker, Layer.FOREGROUND);
  }

  /**
   * Adds a marker for display against the domain axis and sends a
   * {@link PlotChangeEvent} to all registered listeners. Typically a marker
   * will be drawn by the renderer as a line perpendicular to the domain axis,
   * however this is entirely up to the renderer.
   *
   * @param marker
   *            the marker (<code>null</code> not permitted).
   * @param layer
   *            the layer (foreground or background) (<code>null</code> not
   *            permitted).
   */
  public void addDomainMarker(final CategoryMarker marker, final Layer layer) {
    this.addDomainMarker(0, marker, layer);
  }

  /**
   * Adds a marker for display by a particular renderer.
   * <P>
   * Typically a marker will be drawn by the renderer as a line perpendicular
   * to a domain axis, however this is entirely up to the renderer.
   *
   * @param index
   *            the renderer index.
   * @param marker
   *            the marker (<code>null</code> not permitted).
   * @param layer
   *            the layer (<code>null</code> not permitted).
   */
  public void addDomainMarker(final int index, final CategoryMarker marker,
      final Layer layer) {
    if (marker == null) {
      throw new IllegalArgumentException("Null 'marker' not permitted.");
    }
    if (layer == null) {
      throw new IllegalArgumentException("Null 'layer' not permitted.");
    }
    Collection markers;
    if (layer == Layer.FOREGROUND) {
      markers = (Collection) this.foregroundDomainMarkers
          .get(new Integer(index));
      if (markers == null) {
        markers = new java.util.ArrayList();
        this.foregroundDomainMarkers.put(new Integer(index), markers);
      }
      markers.add(marker);
    } else if (layer == Layer.BACKGROUND) {
      markers = (Collection) this.backgroundDomainMarkers
          .get(new Integer(index));
      if (markers == null) {
        markers = new java.util.ArrayList();
        this.backgroundDomainMarkers.put(new Integer(index), markers);
      }
      markers.add(marker);
    }
    marker.addChangeListener(this);
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Adds a marker for display by a particular renderer.
   * <P>
   * Typically a marker will be drawn by the renderer as a line perpendicular
   * to a range axis, however this is entirely up to the renderer.
   *
   * @param index
   *            the renderer index.
   * @param marker
   *            the marker.
   * @param layer
   *            the layer.
   */
  public void addRangeMarker(final int index, final Marker marker,
      final Layer layer) {
    Collection markers;
    if (layer == Layer.FOREGROUND) {
      markers = (Collection) this.foregroundRangeMarkers.get(new Integer(
          index));
      if (markers == null) {
        markers = new java.util.ArrayList();
        this.foregroundRangeMarkers.put(new Integer(index), markers);
      }
      markers.add(marker);
    } else if (layer == Layer.BACKGROUND) {
      markers = (Collection) this.backgroundRangeMarkers.get(new Integer(
          index));
      if (markers == null) {
        markers = new java.util.ArrayList();
        this.backgroundRangeMarkers.put(new Integer(index), markers);
      }
      markers.add(marker);
    }
    marker.addChangeListener(this);
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Adds a marker for display (in the foreground) against the range axis and
   * sends a {@link PlotChangeEvent} to all registered listeners. Typically a
   * marker will be drawn by the renderer as a line perpendicular to the range
   * axis, however this is entirely up to the renderer.
   *
   * @param marker
   *            the marker (<code>null</code> not permitted).
   */
  public void addRangeMarker(final Marker marker) {
    this.addRangeMarker(marker, Layer.FOREGROUND);
  }

  /**
   * Adds a marker for display against the range axis and sends a
   * {@link PlotChangeEvent} to all registered listeners. Typically a marker
   * will be drawn by the renderer as a line perpendicular to the range axis,
   * however this is entirely up to the renderer.
   *
   * @param marker
   *            the marker (<code>null</code> not permitted).
   * @param layer
   *            the layer (foreground or background) (<code>null</code> not
   *            permitted).
   */
  public void addRangeMarker(final Marker marker, final Layer layer) {
    this.addRangeMarker(0, marker, layer);
  }

  /**
   * Calculates the space required for the axes.
   *
   * @param g2
   *            the graphics device.
   * @param plotArea
   *            the plot area.
   *
   * @return The space required for the axes.
   */
  protected AxisSpace calculateAxisSpace(final GC g2, final Rectangle plotArea) {
    AxisSpace space = new AxisSpace();
    space = this.calculateRangeAxisSpace(g2, plotArea, space);
    space = this.calculateDomainAxisSpace(g2, plotArea, space);
    return space;
  }

  public Rectangle calculateDataArea(final GC g2, final Rectangle area) {
    final Rectangle newRectangle = new Rectangle(0, 0, 0, 0);
    RectangleUtil.setRect(newRectangle, area);
    final AxisSpace space = this.calculateAxisSpace(g2, newRectangle);
    final Rectangle dataArea = space.shrink(newRectangle, null);
    this.axisOffset.trim(dataArea);
    return dataArea;
  }

  /**
   * Calculates the space required for the domain axis/axes.
   *
   * @param g2
   *            the graphics device.
   * @param plotArea
   *            the plot area.
   * @param space
   *            a carrier for the result (<code>null</code> permitted).
   *
   * @return The required space.
   */
  protected AxisSpace calculateDomainAxisSpace(final GC g2,
      final Rectangle plotArea, AxisSpace space) {

    if (space == null) {
      space = new AxisSpace();
    }

    // reserve some space for the domain axis...
    if (this.fixedDomainAxisSpace != null) {
      if (this.orientation == PlotOrientation.HORIZONTAL) {
        space.ensureAtLeast(this.fixedDomainAxisSpace.getLeft(),
            RectangleEdge.LEFT);
        space.ensureAtLeast(this.fixedDomainAxisSpace.getRight(),
            RectangleEdge.RIGHT);
      } else if (this.orientation == PlotOrientation.VERTICAL) {
        space.ensureAtLeast(this.fixedDomainAxisSpace.getTop(),
            RectangleEdge.TOP);
        space.ensureAtLeast(this.fixedDomainAxisSpace.getBottom(),
            RectangleEdge.BOTTOM);
      }
    } else {
      // reserve space for the primary domain axis...
      final RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
          this.getDomainAxisLocation(), this.orientation);
      if (this.drawSharedDomainAxis) {
        space = this.getDomainAxis().reserveSpace(g2, this, plotArea,
            domainEdge, space);
      }

      // reserve space for any domain axes...
      for (int i = 0; i < this.domainAxes.size(); i++) {
        final Axis xAxis = (Axis) this.domainAxes.get(i);
        if (xAxis != null) {
          final RectangleEdge edge = this.getDomainAxisEdge(i);
          space = xAxis.reserveSpace(g2, this, plotArea, edge, space);
        }
      }
    }

    return space;

  }

  /**
   * Calculates the space required for the range axis/axes.
   *
   * @param g2
   *            the graphics device.
   * @param plotArea
   *            the plot area.
   * @param space
   *            a carrier for the result (<code>null</code> permitted).
   *
   * @return The required space.
   */
  protected AxisSpace calculateRangeAxisSpace(final GC g2,
      final Rectangle plotArea, AxisSpace space) {

    if (space == null) {
      space = new AxisSpace();
    }

    // reserve some space for the range axis...
    if (this.fixedRangeAxisSpace != null) {
      if (this.orientation == PlotOrientation.HORIZONTAL) {
        space.ensureAtLeast(this.fixedRangeAxisSpace.getTop(),
            RectangleEdge.TOP);
        space.ensureAtLeast(this.fixedRangeAxisSpace.getBottom(),
            RectangleEdge.BOTTOM);
      } else if (this.orientation == PlotOrientation.VERTICAL) {
        space.ensureAtLeast(this.fixedRangeAxisSpace.getLeft(),
            RectangleEdge.LEFT);
        space.ensureAtLeast(this.fixedRangeAxisSpace.getRight(),
            RectangleEdge.RIGHT);
      }
    } else {
      // reserve space for the range axes (if any)...
      for (int i = 0; i < this.rangeAxes.size(); i++) {
        final Axis yAxis = (Axis) this.rangeAxes.get(i);
        if (yAxis != null) {
          final RectangleEdge edge = this.getRangeAxisEdge(i);
          space = yAxis.reserveSpace(g2, this, plotArea, edge, space);
        }
      }
    }
    return space;

  }

  public double calculateTopCursorPosition(final Rectangle dataArea) {
    final double cursor = RectangleUtil.getMinY(dataArea)
        - this.axisOffset.calculateTopOutset(dataArea.height);
    return cursor;
  }

  /**
   * Clears all the annotations and sends a {@link PlotChangeEvent} to all
   * registered listeners.
   */
  public void clearAnnotations() {
    this.annotations.clear();
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears the domain axes from the plot and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   */
  public void clearDomainAxes() {
    for (int i = 0; i < this.domainAxes.size(); i++) {
      final CategoryAxis axis = (CategoryAxis) this.domainAxes.get(i);
      if (axis != null) {
        axis.removeChangeListener(this);
      }
    }
    this.domainAxes.clear();
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears all the domain markers for the plot and sends a
   * {@link PlotChangeEvent} to all registered listeners.
   */
  public void clearDomainMarkers() {
    if (this.backgroundDomainMarkers != null) {
      final Set keys = this.backgroundDomainMarkers.keySet();
      final Iterator iterator = keys.iterator();
      while (iterator.hasNext()) {
        final Integer key = (Integer) iterator.next();
        this.clearDomainMarkers(key.intValue());
      }
      this.backgroundDomainMarkers.clear();
    }
    if (this.foregroundDomainMarkers != null) {
      final Set keys = this.foregroundDomainMarkers.keySet();
      final Iterator iterator = keys.iterator();
      while (iterator.hasNext()) {
        final Integer key = (Integer) iterator.next();
        this.clearDomainMarkers(key.intValue());
      }
      this.foregroundDomainMarkers.clear();
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears all the domain markers for the specified renderer.
   *
   * @param index
   *            the renderer index.
   */
  public void clearDomainMarkers(final int index) {
    final Integer key = new Integer(index);
    if (this.backgroundDomainMarkers != null) {
      final Collection markers = (Collection) this.backgroundDomainMarkers
          .get(key);
      if (markers != null) {
        final Iterator iterator = markers.iterator();
        while (iterator.hasNext()) {
          final Marker m = (Marker) iterator.next();
          m.removeChangeListener(this);
        }
        markers.clear();
      }
    }
    if (this.foregroundDomainMarkers != null) {
      final Collection markers = (Collection) this.foregroundDomainMarkers
          .get(key);
      if (markers != null) {
        final Iterator iterator = markers.iterator();
        while (iterator.hasNext()) {
          final Marker m = (Marker) iterator.next();
          m.removeChangeListener(this);
        }
        markers.clear();
      }
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears the range axes from the plot and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   */
  public void clearRangeAxes() {
    for (int i = 0; i < this.rangeAxes.size(); i++) {
      final ValueAxis axis = (ValueAxis) this.rangeAxes.get(i);
      if (axis != null) {
        axis.removeChangeListener(this);
      }
    }
    this.rangeAxes.clear();
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears all the range markers for the plot and sends a
   * {@link PlotChangeEvent} to all registered listeners.
   */
  public void clearRangeMarkers() {
    if (this.backgroundRangeMarkers != null) {
      final Set keys = this.backgroundRangeMarkers.keySet();
      final Iterator iterator = keys.iterator();
      while (iterator.hasNext()) {
        final Integer key = (Integer) iterator.next();
        this.clearRangeMarkers(key.intValue());
      }
      this.backgroundRangeMarkers.clear();
    }
    if (this.foregroundRangeMarkers != null) {
      final Set keys = this.foregroundRangeMarkers.keySet();
      final Iterator iterator = keys.iterator();
      while (iterator.hasNext()) {
        final Integer key = (Integer) iterator.next();
        this.clearRangeMarkers(key.intValue());
      }
      this.foregroundRangeMarkers.clear();
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Clears all the range markers for the specified renderer.
   *
   * @param index
   *            the renderer index.
   */
  public void clearRangeMarkers(final int index) {
    final Integer key = new Integer(index);
    if (this.backgroundRangeMarkers != null) {
      final Collection markers = (Collection) this.backgroundRangeMarkers
          .get(key);
      if (markers != null) {
        final Iterator iterator = markers.iterator();
        while (iterator.hasNext()) {
          final Marker m = (Marker) iterator.next();
          m.removeChangeListener(this);
        }
        markers.clear();
      }
    }
    if (this.foregroundRangeMarkers != null) {
      final Collection markers = (Collection) this.foregroundRangeMarkers
          .get(key);
      if (markers != null) {
        final Iterator iterator = markers.iterator();
        while (iterator.hasNext()) {
          final Marker m = (Marker) iterator.next();
          m.removeChangeListener(this);
        }
        markers.clear();
      }
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Returns a clone of the plot.
   *
   * @return A clone.
   *
   * @throws CloneNotSupportedException
   *             if the cloning is not supported.
   */
  public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
  }

  /**
   * Configures the domain axes.
   */
  public void configureDomainAxes() {
    for (int i = 0; i < this.domainAxes.size(); i++) {
      final CategoryAxis axis = (CategoryAxis) this.domainAxes.get(i);
      if (axis != null) {
        axis.configure();
      }
    }
  }

  /**
   * Configures the range axes.
   */
  public void configureRangeAxes() {
    for (int i = 0; i < this.rangeAxes.size(); i++) {
      final ValueAxis axis = (ValueAxis) this.rangeAxes.get(i);
      if (axis != null) {
        axis.configure();
      }
    }
  }

  /**
   * Receives notification of a change to the plot's dataset.
   * <P>
   * The range axis bounds will be recalculated if necessary.
   *
   * @param event
   *            information about the event (not used here).
   */
  public void datasetChanged(final DatasetChangeEvent event) {

    final int count = this.rangeAxes.size();
    for (int axisIndex = 0; axisIndex < count; axisIndex++) {
      final ValueAxis yAxis = this.getRangeAxis(axisIndex);
      if (yAxis != null) {
        yAxis.configure();
      }
    }
    if (this.getParent() != null) {
      this.getParent().datasetChanged(event);
    } else {
      final PlotChangeEvent e = new PlotChangeEvent(this);
      e.setType(ChartChangeEventType.DATASET_UPDATED);
      this.notifyListeners(e);
    }

  }

  /**
   * Returns a list of the datasets that are mapped to the axis with the
   * specified index.
   *
   * @param axisIndex
   *            the axis index.
   *
   * @return The list (possibly empty, but never <code>null</code>).
   *
   * @since 1.0.3
   */
  private List datasetsMappedToDomainAxis(final int axisIndex) {
    final List result = new ArrayList();
    for (int datasetIndex = 0; datasetIndex < this.datasets.size(); datasetIndex++) {
      final Object dataset = this.datasets.get(datasetIndex);
      if (dataset != null) {
        final Integer m = (Integer) this.datasetToDomainAxisMap
            .get(datasetIndex);
        if (m == null) { // a dataset with no mapping is assigned to
          // axis 0
          if (axisIndex == 0) {
            result.add(dataset);
          }
        } else {
          if (m.intValue() == axisIndex) {
            result.add(dataset);
          }
        }
      }
    }
    return result;
  }

  /**
   * A utility method that returns a list of datasets that are mapped to a
   * given range axis.
   *
   * @param index
   *            the axis index.
   *
   * @return A list of datasets.
   */
  private List datasetsMappedToRangeAxis(final int index) {
    final List result = new ArrayList();
    for (int i = 0; i < this.datasets.size(); i++) {
      final Object dataset = this.datasets.get(i);
      if (dataset != null) {
        final Integer m = (Integer) this.datasetToRangeAxisMap.get(i);
        if (m == null) { // a dataset with no mapping is assigned to
          // axis 0
          if (index == 0) {
            result.add(dataset);
          }
        } else {
          if (m.intValue() == index) {
            result.add(dataset);
          }
        }
      }
    }
    return result;
  }

  /**
   * Draws the plot on a Java 2D graphics device (such as the screen or a
   * printer).
   * <P>
   * At your option, you may supply an instance of {@link PlotRenderingInfo}.
   * If you do, it will be populated with information about the drawing,
   * including various plot dimensions and tooltip info.
   *
   * @param g2
   *            the graphics device.
   * @param area
   *            the area within which the plot (including axes) should be
   *            drawn.
   * @param anchor
   *            the anchor point (<code>null</code> permitted).
   * @param parentState
   *            the state from the parent plot, if there is one.
   * @param state
   *            collects info as the chart is drawn (possibly
   *            <code>null</code>).
   */
  public void draw(final GC g2, final Rectangle area, final Point anchor,
      final PlotState parentState, PlotRenderingInfo state) {

    try {
      // if the plot area is too small, just return...
      final boolean b1 = (area.width <= MINIMUM_WIDTH_TO_DRAW);
      final boolean b2 = (area.height <= MINIMUM_HEIGHT_TO_DRAW);
      if (b1 || b2) {
        return;
      }

      // record the plot area...
      if (state == null) {
        // if the incoming state is null, no information will be passed
        // back to the caller - but we create a temporary state to
        // record
        // the plot area, since that is used later by the axes
        state = new PlotRenderingInfo(null);
      }
      state.setPlotArea(area);

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

      // calculate the data area...
      final AxisSpace space = this.calculateAxisSpace(g2, area);
      final Rectangle dataArea = space.shrink(area, null);
      this.axisOffset.trim(dataArea);

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

      // if there is a renderer, it draws the background, otherwise use
      // the
      // default background...
      if (this.getRenderer() != null) {
        this.getRenderer().drawBackground(g2, this, dataArea);
      } else {
        this.drawBackground(g2, dataArea);
      }

      final Map axisStateMap = this.drawAxes(g2, area, dataArea, state);

      this.drawDomainGridlines(g2, dataArea);

      AxisState rangeAxisState = (AxisState) axisStateMap.get(this
          .getRangeAxis());
      if (rangeAxisState == null) {
        if (parentState != null) {
          rangeAxisState = (AxisState) parentState
              .getSharedAxisStates().get(this.getRangeAxis());
        }
      }
      if (rangeAxisState != null) {
        this
            .drawRangeGridlines(g2, dataArea, rangeAxisState
                .getTicks());
      }

      // draw the markers...
      for (int i = 0; i < this.renderers.size(); i++) {
        this.drawDomainMarkers(g2, dataArea, i, Layer.BACKGROUND);
      }
      for (int i = 0; i < this.renderers.size(); i++) {
        this.drawRangeMarkers(g2, dataArea, i, Layer.BACKGROUND);
      }

      // now render data items...
      boolean foundData = false;
      final Rectangle savedClip = g2.getClipping();
      g2.setClipping(dataArea);
      // set up the alpha-transparency...
      final int oldAlpha = g2.getAlpha();
      g2.setAlpha(this.getForegroundAlpha());

      final DatasetRenderingOrder order = this.getDatasetRenderingOrder();
      if (order == DatasetRenderingOrder.FORWARD) {
        for (int i = 0; i < this.datasets.size(); i++) {
          foundData = this.render(g2, dataArea, i, state)
              || foundData;
        }
      } else { // DatasetRenderingOrder.REVERSE
        for (int i = this.datasets.size() - 1; i >= 0; i--) {
          foundData = this.render(g2, dataArea, i, state)
              || foundData;
        }
      }
      g2.setClipping(savedClip);

      if (!foundData) {
        // TODO : drawNoDataMessage
        // drawNoDataMessage(g2, dataArea);
      }

      // draw vertical crosshair if required...
      if (this.isRangeCrosshairVisible()) {
        this.drawRangeLine(g2, dataArea, this.getRangeCrosshairValue(),
            this.getRangeCrosshairStroke(), this
                .getRangeCrosshairPaint());
      }

      // draw the foreground markers...
      for (int i = 0; i < this.renderers.size(); i++) {
        this.drawDomainMarkers(g2, dataArea, i, Layer.FOREGROUND);
      }
      for (int i = 0; i < this.renderers.size(); i++) {
        this.drawRangeMarkers(g2, dataArea, i, Layer.FOREGROUND);
      }

      // draw the annotations (if any)...
      this.drawAnnotations(g2, dataArea);

      // draw an outline around the plot area...
      if (this.getRenderer() != null) {
        this.getRenderer().drawOutline(g2, this, dataArea);
      } else {
        this.drawOutline(g2, dataArea);
      }

      g2.setAlpha(oldAlpha);
    } catch (final Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Draws the annotations...
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the data area.
   */
  protected void drawAnnotations(final GC g2, final Rectangle dataArea) {

    if (this.getAnnotations() != null) {
      final Iterator iterator = this.getAnnotations().iterator();
      while (iterator.hasNext()) {
        final CategoryAnnotation annotation = (CategoryAnnotation) iterator
            .next();
        annotation.draw(g2, this, dataArea, this.getDomainAxis(), this
            .getRangeAxis());
      }
    }

  }

  /**
   * A utility method for drawing the plot's axes.
   *
   * @param g2
   *            the graphics device.
   * @param plotArea
   *            the plot area.
   * @param dataArea
   *            the data area.
   * @param plotState
   *            collects information about the plot (<code>null</code>
   *            permitted).
   *
   * @return A map containing the axis states.
   */
  protected Map drawAxes(final GC g2, final Rectangle plotArea,
      final Rectangle dataArea, final PlotRenderingInfo plotState) {

    final AxisCollection axisCollection = new AxisCollection();

    // add domain axes to lists...
    for (int index = 0; index < this.domainAxes.size(); index++) {
      final CategoryAxis xAxis = (CategoryAxis) this.domainAxes
          .get(index);
      if (xAxis != null) {
        axisCollection.add(xAxis, this.getDomainAxisEdge(index));
      }
    }

    // add range axes to lists...
    for (int index = 0; index < this.rangeAxes.size(); index++) {
      final ValueAxis yAxis = (ValueAxis) this.rangeAxes.get(index);
      if (yAxis != null) {
        axisCollection.add(yAxis, this.getRangeAxisEdge(index));
      }
    }

    final Map axisStateMap = new HashMap();

    // draw the top axes
    double cursor = RectangleUtil.getMinY(dataArea)
        - this.axisOffset.calculateTopOutset(dataArea.height);
    Iterator iterator = axisCollection.getAxesAtTop().iterator();
    while (iterator.hasNext()) {
      final Axis axis = (Axis) iterator.next();
      if (axis != null) {
        final AxisState axisState = axis.draw(g2, cursor, plotArea,
            dataArea, RectangleEdge.TOP, plotState);
        cursor = axisState.getCursor();
        axisStateMap.put(axis, axisState);
      }
    }

    // draw the bottom axes
    cursor = RectangleUtil.getMaxY(dataArea)
        + this.axisOffset.calculateBottomOutset(dataArea.height);
    iterator = axisCollection.getAxesAtBottom().iterator();
    while (iterator.hasNext()) {
      final Axis axis = (Axis) iterator.next();
      if (axis != null) {
        final AxisState axisState = axis.draw(g2, cursor, plotArea,
            dataArea, RectangleEdge.BOTTOM, plotState);
        cursor = axisState.getCursor();
        axisStateMap.put(axis, axisState);
      }
    }

    // draw the left axes
    cursor = RectangleUtil.getMinX(dataArea)
        - this.axisOffset.calculateLeftOutset(dataArea.width);
    iterator = axisCollection.getAxesAtLeft().iterator();
    while (iterator.hasNext()) {
      final Axis axis = (Axis) iterator.next();
      if (axis != null) {
        final AxisState axisState = axis.draw(g2, cursor, plotArea,
            dataArea, RectangleEdge.LEFT, plotState);
        cursor = axisState.getCursor();
        axisStateMap.put(axis, axisState);
      }
    }

    // draw the right axes
    cursor = RectangleUtil.getMaxX(dataArea)
        + this.axisOffset.calculateRightOutset(dataArea.width);
    iterator = axisCollection.getAxesAtRight().iterator();
    while (iterator.hasNext()) {
      final Axis axis = (Axis) iterator.next();
      if (axis != null) {
        final AxisState axisState = axis.draw(g2, cursor, plotArea,
            dataArea, RectangleEdge.RIGHT, plotState);
        cursor = axisState.getCursor();
        axisStateMap.put(axis, axisState);
      }
    }

    return axisStateMap;

  }

  /**
   * Draws the gridlines for the plot.
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the area inside the axes.
   */
  protected void drawDomainGridlines(final GC g2, final Rectangle dataArea) {

    // draw the domain grid lines, if any...
    if (this.isDomainGridlinesVisible()) {
      final CategoryAnchor anchor = this.getDomainGridlinePosition();
      final RectangleEdge domainAxisEdge = this.getDomainAxisEdge();
      final Stroke gridStroke = this.getDomainGridlineStroke();
      final Color gridPaint = this.getDomainGridlinePaint();
      if ((gridStroke != null) && (gridPaint != null)) {
        // iterate over the categories
        final CategoryDataset data = this.getDataset();
        if (data != null) {
          final CategoryAxis axis = this.getDomainAxis();
          if (axis != null) {
            final int columnCount = data.getColumnCount();
            for (int c = 0; c < columnCount; c++) {
              final double xx = axis.getCategoryJava2DCoordinate(
                  anchor, c, columnCount, dataArea,
                  domainAxisEdge);
              final CategoryItemRenderer renderer1 = this
                  .getRenderer();
              if (renderer1 != null) {
                renderer1.drawDomainGridline(g2, this,
                    dataArea, xx);
              }
            }
          }
        }
      }
    }
  }

  /**
   * Draws the domain markers (if any) for an axis and layer. This method is
   * typically called from within the draw() method.
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the data area.
   * @param index
   *            the renderer index.
   * @param layer
   *            the layer (foreground or background).
   */
  protected void drawDomainMarkers(final GC g2, final Rectangle dataArea,
      final int index, final Layer layer) {

    final CategoryItemRenderer r = this.getRenderer(index);
    if (r == null) {
      return;
    }

    final Collection markers = this.getDomainMarkers(index, layer);
    final CategoryAxis axis = this.getDomainAxisForDataset(index);
    if ((markers != null) && (axis != null)) {
      final Iterator iterator = markers.iterator();
      while (iterator.hasNext()) {
        final CategoryMarker marker = (CategoryMarker) iterator.next();
        r.drawDomainMarker(g2, this, axis, marker, dataArea);
      }
    }

  }

  /**
   * Draws the gridlines for the plot.
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the area inside the axes.
   * @param ticks
   *            the ticks.
   */
  protected void drawRangeGridlines(final GC g2, final Rectangle dataArea,
      final List ticks) {
    // draw the range grid lines, if any...
    if (this.isRangeGridlinesVisible()) {
      final Stroke gridStroke = this.getRangeGridlineStroke();
      final Color gridPaint = this.getRangeGridlinePaint();
      if ((gridStroke != null) && (gridPaint != null)) {
        final ValueAxis axis = this.getRangeAxis();
        if (axis != null) {
          final Iterator iterator = ticks.iterator();
          while (iterator.hasNext()) {
            final ValueTick tick = (ValueTick) iterator.next();
            final CategoryItemRenderer renderer1 = this
                .getRenderer();
            if (renderer1 != null) {
              renderer1.drawRangeGridline(g2, this, this
                  .getRangeAxis(), dataArea, tick.getValue());
            }
          }
        }
      }
    }
  }

  /**
   * Utility method for drawing a line perpendicular to the range axis (used
   * for crosshairs).
   *
   * @param gc
   *            the graphics device.
   * @param dataArea
   *            the area defined by the axes.
   * @param value
   *            the data value.
   * @param stroke
   *            the line stroke.
   * @param paint
   *            the line paint.
   */
  protected void drawRangeLine(final GC gc, final Rectangle dataArea,
      final double value, final Stroke stroke, final Color paint) {

    final double java2D = this.getRangeAxis().valueToJava2D(value,
        dataArea, this.getRangeAxisEdge());
    Line line = null;
    if (this.orientation == PlotOrientation.HORIZONTAL) {
      line = Line.Double(java2D, RectangleUtil.getMinY(dataArea), java2D,
          RectangleUtil.getMaxY(dataArea));
    } else if (this.orientation == PlotOrientation.VERTICAL) {
      line = Line.Double(RectangleUtil.getMinX(dataArea), java2D,
          RectangleUtil.getMaxX(dataArea), java2D);
    }
    stroke.set(gc);
    gc.setForeground(paint);
    GCUtilities.draw(gc, line);
  }

  /**
   * Draws the range markers (if any) for an axis and layer. This method is
   * typically called from within the draw() method.
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the data area.
   * @param index
   *            the renderer index.
   * @param layer
   *            the layer (foreground or background).
   */
  protected void drawRangeMarkers(final GC g2, final Rectangle dataArea,
      final int index, final Layer layer) {

    final CategoryItemRenderer r = this.getRenderer(index);
    if (r == null) {
      return;
    }

    final Collection markers = this.getRangeMarkers(index, layer);
    final ValueAxis axis = this.getRangeAxisForDataset(index);
    if ((markers != null) && (axis != null)) {
      final Iterator iterator = markers.iterator();
      while (iterator.hasNext()) {
        final Marker marker = (Marker) iterator.next();
        r.drawRangeMarker(g2, this, axis, marker, dataArea);
      }
    }

  }

  /**
   * Tests the plot for equality with an arbitrary object.
   *
   * @param obj
   *            the object to test against (<code>null</code> permitted).
   *
   * @return A boolean.
   */
  public boolean equals(final Object obj) {

    if (obj == this) {
      return true;
    }

    if (!(obj instanceof CategoryPlot)) {
      return false;
    }
    if (!super.equals(obj)) {
      return false;
    }

    final CategoryPlot that = (CategoryPlot) obj;

    if (this.orientation != that.orientation) {
      return false;
    }
    if (!ObjectUtilities.equal(this.axisOffset, that.axisOffset)) {
      return false;
    }
    if (!this.domainAxes.equals(that.domainAxes)) {
      return false;
    }
    if (!this.domainAxisLocations.equals(that.domainAxisLocations)) {
      return false;
    }
    if (this.drawSharedDomainAxis != that.drawSharedDomainAxis) {
      return false;
    }
    if (!this.rangeAxes.equals(that.rangeAxes)) {
      return false;
    }
    if (!this.rangeAxisLocations.equals(that.rangeAxisLocations)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.datasetToDomainAxisMap,
        that.datasetToDomainAxisMap)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.datasetToRangeAxisMap,
        that.datasetToRangeAxisMap)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.renderers, that.renderers)) {
      return false;
    }
    if (this.renderingOrder != that.renderingOrder) {
      return false;
    }
    if (this.columnRenderingOrder != that.columnRenderingOrder) {
      return false;
    }
    if (this.rowRenderingOrder != that.rowRenderingOrder) {
      return false;
    }
    if (this.domainGridlinesVisible != that.domainGridlinesVisible) {
      return false;
    }
    if (this.domainGridlinePosition != that.domainGridlinePosition) {
      return false;
    }
    if (!ObjectUtilities.equal(this.domainGridlineStroke,
        that.domainGridlineStroke)) {
      return false;
    }

    if (!this.domainGridlinePaint.equals(that.domainGridlinePaint)) {
      return false;
    }
    if (this.rangeGridlinesVisible != that.rangeGridlinesVisible) {
      return false;
    }
    if (!ObjectUtilities.equal(this.rangeGridlineStroke,
        that.rangeGridlineStroke)) {
      return false;
    }
    if (!this.rangeGridlinePaint.equals(that.rangeGridlinePaint)) {
      return false;
    }
    if (this.anchorValue != that.anchorValue) {
      return false;
    }
    if (this.rangeCrosshairVisible != that.rangeCrosshairVisible) {
      return false;
    }
    if (this.rangeCrosshairValue != that.rangeCrosshairValue) {
      return false;
    }
    if (!ObjectUtilities.equal(this.rangeCrosshairStroke,
        that.rangeCrosshairStroke)) {
      return false;
    }

    if (!this.rangeCrosshairPaint.equals(that.rangeCrosshairPaint)) {
      return false;
    }
    if (this.rangeCrosshairLockedOnData != that.rangeCrosshairLockedOnData) {
      return false;
    }
    if (!ObjectUtilities.equal(this.foregroundRangeMarkers,
        that.foregroundRangeMarkers)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.backgroundRangeMarkers,
        that.backgroundRangeMarkers)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.annotations, that.annotations)) {
      return false;
    }
    if (this.weight != that.weight) {
      return false;
    }
    if (!ObjectUtilities.equal(this.fixedDomainAxisSpace,
        that.fixedDomainAxisSpace)) {
      return false;
    }
    if (!ObjectUtilities.equal(this.fixedRangeAxisSpace,
        that.fixedRangeAxisSpace)) {
      return false;
    }

    return true;

  }

  /**
   * Returns the anchor value.
   *
   * @return The anchor value.
   */
  public double getAnchorValue() {
    return this.anchorValue;
  }

  /**
   * Returns the list of annotations.
   *
   * @return The list of annotations.
   */
  public List getAnnotations() {
    return this.annotations;
  }

  /**
   * Returns the axis offset.
   *
   * @return The axis offset (never <code>null</code>).
   */
  public RectangleInsets getAxisOffset() {
    return this.axisOffset;
  }

  /**
   * Returns a list of the categories in the plot's primary dataset.
   *
   * @return A list of the categories in the plot's primary dataset.
   *
   * @see #getCategoriesForAxis(CategoryAxis)
   */
  public List getCategories() {
    List result = null;
    if (this.getDataset() != null) {
      result = Collections.unmodifiableList(this.getDataset()
          .getColumnKeys());
    }
    return result;
  }

  /**
   * Returns a list of the categories that should be displayed for the
   * specified axis.
   *
   * @param axis
   *            the axis (<code>null</code> not permitted)
   *
   * @return The categories.
   *
   * @since 1.0.3
   */
  public List getCategoriesForAxis(final CategoryAxis axis) {
    final List result = new ArrayList();
    final int axisIndex = this.domainAxes.indexOf(axis);
    final List datasets = this.datasetsMappedToDomainAxis(axisIndex);
    final Iterator iterator = datasets.iterator();
    while (iterator.hasNext()) {
      final CategoryDataset dataset = (CategoryDataset) iterator.next();
      // add the unique categories from this dataset
      for (int i = 0; i < dataset.getColumnCount(); i++) {
        final Comparable category = dataset.getColumnKey(i);
        if (!result.contains(category)) {
          result.add(category);
        }
      }
    }
    return result;
  }

  /**
   * Returns the order in which the columns are rendered.
   *
   * @return The order.
   */
  public SortOrder getColumnRenderingOrder() {
    return this.columnRenderingOrder;
  }

  /**
   * Returns the range of data values that will be plotted against the range
   * axis. If the dataset is <code>null</code>, this method returns
   * <code>null</code>.
   *
   * @param axis
   *            the axis.
   *
   * @return The data range.
   */
  public Range getDataRange(final ValueAxis axis) {

    Range result = null;
    final List mappedDatasets = new ArrayList();

    final int rangeIndex = this.rangeAxes.indexOf(axis);
    if (rangeIndex >= 0) {
      mappedDatasets.addAll(this.datasetsMappedToRangeAxis(rangeIndex));
    } else if (axis == this.getRangeAxis()) {
      mappedDatasets.addAll(this.datasetsMappedToRangeAxis(0));
    }

    // iterate through the datasets that map to the axis and get the union
    // of the ranges.
    final Iterator iterator = mappedDatasets.iterator();
    while (iterator.hasNext()) {
      final CategoryDataset d = (CategoryDataset) iterator.next();
      final CategoryItemRenderer r = this.getRendererForDataset(d);
      if (r != null) {
        result = Range.combine(result, r.findRangeBounds(d));
      }
    }
    return result;

  }

  /**
   * Returns the primary dataset for the plot.
   *
   * @return The primary dataset (possibly <code>null</code>).
   */
  public CategoryDataset getDataset() {
    return this.getDataset(0);
  }

  /**
   * Returns the dataset at the given index.
   *
   * @param index
   *            the dataset index.
   *
   * @return The dataset (possibly <code>null</code>).
   */
  public CategoryDataset getDataset(final int index) {
    CategoryDataset result = null;
    if (this.datasets.size() > index) {
      result = (CategoryDataset) this.datasets.get(index);
    }
    return result;
  }

  /**
   * Returns the number of datasets.
   *
   * @return The number of datasets.
   *
   * @since 1.0.2
   */
  public int getDatasetCount() {
    return this.datasets.size();
  }

  /**
   * Returns the dataset rendering order.
   *
   * @return The order (never <code>null</code>).
   */
  public DatasetRenderingOrder getDatasetRenderingOrder() {
    return this.renderingOrder;
  }

  /**
   * Returns the domain axis for the plot. If the domain axis for this plot is
   * <code>null</code>, then the method will return the parent plot's domain
   * axis (if there is a parent plot).
   *
   * @return The domain axis (<code>null</code> permitted).
   */
  public CategoryAxis getDomainAxis() {
    return this.getDomainAxis(0);
  }

  /**
   * Returns a domain axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The axis (<code>null</code> possible).
   */
  public CategoryAxis getDomainAxis(final int index) {
    CategoryAxis result = null;
    if (index < this.domainAxes.size()) {
      result = (CategoryAxis) this.domainAxes.get(index);
    }
    if (result == null) {
      final Plot parent = this.getParent();
      if (parent instanceof CategoryPlot) {
        final CategoryPlot cp = (CategoryPlot) parent;
        result = cp.getDomainAxis(index);
      }
    }
    return result;
  }

  /**
   * Returns the number of domain axes.
   *
   * @return The axis count.
   */
  public int getDomainAxisCount() {
    return this.domainAxes.size();
  }

  /**
   * Returns the domain axis edge. This is derived from the axis location and
   * the plot orientation.
   *
   * @return The edge (never <code>null</code>).
   */
  public RectangleEdge getDomainAxisEdge() {
    return this.getDomainAxisEdge(0);
  }

  /**
   * Returns the edge for a domain axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The edge (never <code>null</code>).
   */
  public RectangleEdge getDomainAxisEdge(final int index) {
    RectangleEdge result = null;
    final AxisLocation location = this.getDomainAxisLocation(index);
    if (location != null) {
      result = Plot.resolveDomainAxisLocation(location, this.orientation);
    } else {
      result = RectangleEdge.opposite(this.getDomainAxisEdge(0));
    }
    return result;
  }

  /**
   * Returns the domain axis for a dataset. You can change the axis for a
   * dataset using the {@link #mapDatasetToDomainAxis(int, int)} method.
   *
   * @param index
   *            the dataset index.
   *
   * @return The domain axis.
   */
  public CategoryAxis getDomainAxisForDataset(final int index) {
    CategoryAxis result = this.getDomainAxis();
    final Integer axisIndex = (Integer) this.datasetToDomainAxisMap
        .get(index);
    if (axisIndex != null) {
      result = this.getDomainAxis(axisIndex.intValue());
    }
    return result;
  }

  /**
   * Returns the index of the specified axis, or <code>-1</code> if the axis
   * is not assigned to the plot.
   *
   * @param axis
   *            the axis.
   *
   * @return The axis index.
   *
   * @since 1.0.3
   */
  public int getDomainAxisIndex(final CategoryAxis axis) {
    return this.domainAxes.indexOf(axis);
  }

  /**
   * Returns the domain axis location.
   *
   * @return The location (never <code>null</code>).
   */
  public AxisLocation getDomainAxisLocation() {
    return this.getDomainAxisLocation(0);
  }

  /**
   * Returns the location for a domain axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The location.
   */
  public AxisLocation getDomainAxisLocation(final int index) {
    AxisLocation result = null;
    if (index < this.domainAxisLocations.size()) {
      result = (AxisLocation) this.domainAxisLocations.get(index);
    }
    if (result == null) {
      result = AxisLocation.getOpposite(this.getDomainAxisLocation(0));
    }
    return result;

  }

  /**
   * Returns the paint used to draw grid-lines against the domain axis.
   *
   * @return The paint (never <code>null</code>).
   */
  public Color getDomainGridlinePaint() {
    if (this.domainGridlinePaint == null) {
      this.domainGridlinePaint = this.getDrawingAssets().getColor(
          COLOR_GRIDLINE_PAINT);
    }
    return this.domainGridlinePaint;
  }

  /**
   * Returns the position used for the domain gridlines.
   *
   * @return The gridline position (never <code>null</code>).
   */
  public CategoryAnchor getDomainGridlinePosition() {
    return this.domainGridlinePosition;
  }

  /**
   * Returns the stroke used to draw grid-lines against the domain axis.
   *
   * @return The stroke (never <code>null</code>).
   */
  public Stroke getDomainGridlineStroke() {
    return this.domainGridlineStroke;
  }

  /**
   * Returns a collection of domain markers for a particular renderer and
   * layer.
   *
   * @param index
   *            the renderer index.
   * @param layer
   *            the layer.
   *
   * @return A collection of markers (possibly <code>null</code>).
   */
  public Collection getDomainMarkers(final int index, final Layer layer) {
    Collection result = null;
    final Integer key = new Integer(index);
    if (layer == Layer.FOREGROUND) {
      result = (Collection) this.foregroundDomainMarkers.get(key);
    } else if (layer == Layer.BACKGROUND) {
      result = (Collection) this.backgroundDomainMarkers.get(key);
    }
    if (result != null) {
      result = Collections.unmodifiableCollection(result);
    }
    return result;
  }

  /**
   * Returns the list of domain markers (read only) for the specified layer.
   *
   * @param layer
   *            the layer (foreground or background).
   *
   * @return The list of domain markers.
   */
  public Collection getDomainMarkers(final Layer layer) {
    return this.getDomainMarkers(0, layer);
  }

  /**
   * Returns the flag that controls whether or not the shared domain axis is
   * drawn for each subplot.
   *
   * @return A boolean.
   */
  public boolean getDrawSharedDomainAxis() {
    return this.drawSharedDomainAxis;
  }

  /**
   * Returns the fixed domain axis space.
   *
   * @return The fixed domain axis space (possibly <code>null</code>).
   */
  public AxisSpace getFixedDomainAxisSpace() {
    return this.fixedDomainAxisSpace;
  }

  /**
   * Returns the fixed legend items, if any.
   *
   * @return The legend items (possibly <code>null</code>).
   */
  public LegendItemCollection getFixedLegendItems() {
    return this.fixedLegendItems;
  }

  /**
   * Returns the fixed range axis space.
   *
   * @return The fixed range axis space (possibly <code>null</code>).
   */
  public AxisSpace getFixedRangeAxisSpace() {
    return this.fixedRangeAxisSpace;
  }

  /**
   * Returns the index of the specified renderer, or <code>-1</code> if the
   * renderer is not assigned to this plot.
   *
   * @param renderer
   *            the renderer (<code>null</code> permitted).
   *
   * @return The renderer index.
   */
  public int getIndexOf(final CategoryItemRenderer renderer) {
    return this.renderers.indexOf(renderer);
  }

  /**
   * Returns the legend items for the plot. By default, this method creates a
   * legend item for each series in each of the datasets. You can change this
   * behaviour by overriding this method.
   *
   * @return The legend items.
   */
  public LegendItemCollection getLegendItems() {
    LegendItemCollection result = this.fixedLegendItems;
    if (result == null) {
      result = new LegendItemCollection();
      // get the legend items for the datasets...
      final int count = this.datasets.size();
      for (int datasetIndex = 0; datasetIndex < count; datasetIndex++) {
        final CategoryDataset dataset = this.getDataset(datasetIndex);
        if (dataset != null) {
          final CategoryItemRenderer renderer = this
              .getRenderer(datasetIndex);
          if (renderer != null) {
            final int seriesCount = dataset.getRowCount();
            for (int i = 0; i < seriesCount; i++) {
              final LegendItem item = renderer.getLegendItem(
                  datasetIndex, i);
              if (item != null) {
                result.add(item);
              }
            }
          }
        }
      }
    }
    return result;
  }

  /**
   * Returns the orientation of the plot.
   *
   * @return The orientation of the plot.
   */
  public PlotOrientation getOrientation() {
    return this.orientation;
  }

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

  /**
   * Returns the range axis for the plot. If the range axis for this plot is
   * null, then the method will return the parent plot's range axis (if there
   * is a parent plot).
   *
   * @return The range axis (possibly <code>null</code>).
   */
  public ValueAxis getRangeAxis() {
    return this.getRangeAxis(0);
  }

  /**
   * Returns a range axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The axis (<code>null</code> possible).
   */
  public ValueAxis getRangeAxis(final int index) {
    ValueAxis result = null;
    if (index < this.rangeAxes.size()) {
      result = (ValueAxis) this.rangeAxes.get(index);
    }
    if (result == null) {
      final Plot parent = this.getParent();
      if (parent instanceof CategoryPlot) {
        final CategoryPlot cp = (CategoryPlot) parent;
        result = cp.getRangeAxis(index);
      }
    }
    return result;
  }

  /**
   * Returns the number of range axes.
   *
   * @return The axis count.
   */
  public int getRangeAxisCount() {
    return this.rangeAxes.size();
  }

  /**
   * Returns the edge where the primary range axis is located.
   *
   * @return The edge (never <code>null</code>).
   */
  public RectangleEdge getRangeAxisEdge() {
    return this.getRangeAxisEdge(0);
  }

  /**
   * Returns the edge for a range axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The edge.
   */
  public RectangleEdge getRangeAxisEdge(final int index) {
    final AxisLocation location = this.getRangeAxisLocation(index);
    RectangleEdge result = Plot.resolveRangeAxisLocation(location,
        this.orientation);
    if (result == null) {
      result = RectangleEdge.opposite(this.getRangeAxisEdge(0));
    }
    return result;
  }

  /**
   * Returns the range axis for a dataset. You can change the axis for a
   * dataset using the {@link #mapDatasetToRangeAxis(int, int)} method.
   *
   * @param index
   *            the dataset index.
   *
   * @return The range axis.
   */
  public ValueAxis getRangeAxisForDataset(final int index) {
    ValueAxis result = this.getRangeAxis();
    final Integer axisIndex = (Integer) this.datasetToRangeAxisMap
        .get(index);
    if (axisIndex != null) {
      result = this.getRangeAxis(axisIndex.intValue());
    }
    return result;
  }

  /**
   * Returns the range axis location.
   *
   * @return The location (never <code>null</code>).
   */
  public AxisLocation getRangeAxisLocation() {
    return this.getRangeAxisLocation(0);
  }

  /**
   * Returns the location for a range axis.
   *
   * @param index
   *            the axis index.
   *
   * @return The location.
   */
  public AxisLocation getRangeAxisLocation(final int index) {
    AxisLocation result = null;
    if (index < this.rangeAxisLocations.size()) {
      result = (AxisLocation) this.rangeAxisLocations.get(index);
    }
    if (result == null) {
      result = AxisLocation.getOpposite(this.getRangeAxisLocation(0));
    }
    return result;
  }

  /**
   * Returns the range crosshair color.
   *
   * @return The crosshair color.
   */
  public Color getRangeCrosshairPaint() {
    return this.rangeCrosshairPaint;
  }

  /**
   * Returns the pen-style (<code>Stroke</code>) used to draw the crosshair
   * (if visible).
   *
   * @return The crosshair stroke.
   */
  public Stroke getRangeCrosshairStroke() {
    return this.rangeCrosshairStroke;
  }

  /**
   * Returns the range crosshair value.
   *
   * @return The value.
   */
  public double getRangeCrosshairValue() {
    return this.rangeCrosshairValue;
  }

  /**
   * Returns the paint used to draw the grid-lines against the range axis.
   *
   * @return The paint (never <code>null</code>).
   */
  public Color getRangeGridlinePaint() {
    if (this.rangeGridlinePaint == null) {
      this.rangeGridlinePaint = this.getDrawingAssets().getColor(
          COLOR_GRIDLINE_PAINT);
    }
    return this.rangeGridlinePaint;
  }

  /**
   * Returns the stroke used to draw the grid-lines against the range axis.
   *
   * @return The stroke (never <code>null</code>).
   */
  public Stroke getRangeGridlineStroke() {
    Stroke stroke = this.getStroke(CategoryPlot.STROKE_RANGE_GRIDLINE);
    if (stroke == null) {
      stroke = this.rangeGridlineStroke;
    }
    return stroke;
  }

  /**
   * Returns a collection of range markers for a particular renderer and
   * layer.
   *
   * @param index
   *            the renderer index.
   * @param layer
   *            the layer.
   *
   * @return A collection of markers (possibly <code>null</code>).
   */
  public Collection getRangeMarkers(final int index, final Layer layer) {
    Collection result = null;
    final Integer key = new Integer(index);
    if (layer == Layer.FOREGROUND) {
      result = (Collection) this.foregroundRangeMarkers.get(key);
    } else if (layer == Layer.BACKGROUND) {
      result = (Collection) this.backgroundRangeMarkers.get(key);
    }
    if (result != null) {
      result = Collections.unmodifiableCollection(result);
    }
    return result;
  }

  /**
   * Returns the list of range markers (read only) for the specified layer.
   *
   * @param layer
   *            the layer (foreground or background).
   *
   * @return The list of range markers.
   */
  public Collection getRangeMarkers(final Layer layer) {
    return this.getRangeMarkers(0, layer);
  }

  /**
   * Returns a reference to the renderer for the plot.
   *
   * @return The renderer.
   */
  public CategoryItemRenderer getRenderer() {
    return this.getRenderer(0);
  }

  /**
   * Returns the renderer at the given index.
   *
   * @param index
   *            the renderer index.
   *
   * @return The renderer (possibly <code>null</code>).
   */
  public CategoryItemRenderer getRenderer(final int index) {
    CategoryItemRenderer result = null;
    if (this.renderers.size() > index) {
      result = (CategoryItemRenderer) this.renderers.get(index);
    }
    return result;
  }

  /**
   * Returns the renderer for the specified dataset. If the dataset doesn't
   * belong to the plot, this method will return <code>null</code>.
   *
   * @param dataset
   *            the dataset (<code>null</code> permitted).
   *
   * @return The renderer (possibly <code>null</code>).
   */
  public CategoryItemRenderer getRendererForDataset(
      final CategoryDataset dataset) {
    CategoryItemRenderer result = null;
    for (int i = 0; i < this.datasets.size(); i++) {
      if (this.datasets.get(i) == dataset) {
        result = (CategoryItemRenderer) this.renderers.get(i);
        break;
      }
    }
    return result;
  }

  /**
   * Returns the order in which the rows should be rendered.
   *
   * @return The order (never <code>null</code>).
   */
  public SortOrder getRowRenderingOrder() {
    return this.rowRenderingOrder;
  }

  /**
   * Returns the weight for this plot when it is used as a subplot within a
   * combined plot.
   *
   * @return The weight.
   */
  public int getWeight() {
    return this.weight;
  }

  /**
   * Handles a 'click' on the plot by updating the anchor value.
   *
   * @param x
   *            x-coordinate of the click (in Java2D space).
   * @param y
   *            y-coordinate of the click (in Java2D space).
   * @param info
   *            information about the plot's dimensions.
   *
   */
  public void handleClick(final int x, final int y,
      final PlotRenderingInfo info) {

    final Rectangle dataArea = info.getDataArea();
    if (dataArea.contains(x, y)) {
      // set the anchor value for the range axis...
      double java2D = 0.0;
      if (this.orientation == PlotOrientation.HORIZONTAL) {
        java2D = x;
      } else if (this.orientation == PlotOrientation.VERTICAL) {
        java2D = y;
      }
      final RectangleEdge edge = Plot.resolveRangeAxisLocation(this
          .getRangeAxisLocation(), this.orientation);
      final double value = this.getRangeAxis().java2DToValue(java2D,
          info.getDataArea(), edge);
      this.setAnchorValue(value);
      this.setRangeCrosshairValue(value);
    }

  }

  // Important! This method should be called after constructor
  public void init(final Marker baseline) {
    // TODO : SIMPLIFICATION was done.
    // Marker baseline = new ValueMarker(0.0, new Color(0.8f, 0.8f, 0.8f,
    // 0.5f), new BasicStroke(1.0f), new Color(0.85f, 0.85f, 0.95f,
    // 0.5f), new BasicStroke(1.0f), 0.6f);
    this.addRangeMarker(baseline, Layer.BACKGROUND);
  }

  /**
   * Returns the flag that controls whether the domain grid-lines are visible.
   *
   * @return The <code>true</code> or <code>false</code>.
   */
  public boolean isDomainGridlinesVisible() {
    return this.domainGridlinesVisible;
  }

  /**
   * Returns <code>false</code>.
   *
   * @return A boolean.
   */
  public boolean isDomainZoomable() {
    return false;
  }

  /**
   * Returns a flag indicating whether or not the crosshair should "lock-on"
   * to actual data values.
   *
   * @return The flag.
   */
  public boolean isRangeCrosshairLockedOnData() {
    return this.rangeCrosshairLockedOnData;
  }

  /**
   * Returns a flag indicating whether or not the range crosshair is visible.
   *
   * @return The flag.
   */
  public boolean isRangeCrosshairVisible() {
    return this.rangeCrosshairVisible;
  }

  /**
   * Returns the flag that controls whether the range grid-lines are visible.
   *
   * @return The flag.
   */
  public boolean isRangeGridlinesVisible() {
    return this.rangeGridlinesVisible;
  }

  /**
   * Returns <code>false</code>.
   *
   * @return A boolean.
   */
  public boolean isRangeZoomable() {
    return true;
  }

  /**
   * Maps a dataset to a particular domain axis.
   *
   * @param index
   *            the dataset index (zero-based).
   * @param axisIndex
   *            the axis index (zero-based).
   */
  public void mapDatasetToDomainAxis(final int index, final int axisIndex) {
    this.datasetToDomainAxisMap.set(index, new Integer(axisIndex));
    // fake a dataset change event to update axes...
    this
        .datasetChanged(new DatasetChangeEvent(this, this
            .getDataset(index)));
  }

  /**
   * Maps a dataset to a particular range axis.
   *
   * @param index
   *            the dataset index (zero-based).
   * @param axisIndex
   *            the axis index (zero-based).
   */
  public void mapDatasetToRangeAxis(final int index, final int axisIndex) {
    this.datasetToRangeAxisMap.set(index, new Integer(axisIndex));
    // fake a dataset change event to update axes...
    this
        .datasetChanged(new DatasetChangeEvent(this, this
            .getDataset(index)));
  }

  /**
   * Removes an annotation from the plot and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   *
   * @param annotation
   *            the annotation (<code>null</code> not permitted).
   *
   * @return A boolean (indicates whether or not the annotation was removed).
   */
  public boolean removeAnnotation(final CategoryAnnotation annotation) {
    if (annotation == null) {
      throw new IllegalArgumentException("Null 'annotation' argument.");
    }
    final boolean removed = this.annotations.remove(annotation);
    if (removed) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
    return removed;
  }

  /**
   * Draws a representation of a dataset within the dataArea region using the
   * appropriate renderer.
   *
   * @param g2
   *            the graphics device.
   * @param dataArea
   *            the region in which the data is to be drawn.
   * @param index
   *            the dataset and renderer index.
   * @param info
   *            an optional object for collection dimension information.
   *
   * @return A boolean that indicates whether or not real data was found.
   */
  public boolean render(final GC g2, final Rectangle dataArea,
      final int index, final PlotRenderingInfo info) {

    boolean foundData = false;
    final CategoryDataset currentDataset = this.getDataset(index);
    final CategoryItemRenderer renderer = this.getRenderer(index);
    final CategoryAxis domainAxis = this.getDomainAxisForDataset(index);
    final ValueAxis rangeAxis = this.getRangeAxisForDataset(index);
    final boolean hasData = !DatasetUtilities.isEmptyOrNull(currentDataset);
    if (hasData && (renderer != null)) {

      foundData = true;
      final CategoryItemRendererState state = renderer.initialise(g2,
          dataArea, this, index, info);
      final int columnCount = currentDataset.getColumnCount();
      final int rowCount = currentDataset.getRowCount();
      final int passCount = renderer.getPassCount();
      for (int pass = 0; pass < passCount; pass++) {
        if (this.columnRenderingOrder == SortOrder.ASCENDING) {
          for (int column = 0; column < columnCount; column++) {
            if (this.rowRenderingOrder == SortOrder.ASCENDING) {
              for (int row = 0; row < rowCount; row++) {
                renderer.drawItem(g2, state, dataArea, this,
                    domainAxis, rangeAxis, currentDataset,
                    row, column, pass);
              }
            } else {
              for (int row = rowCount - 1; row >= 0; row--) {
                renderer.drawItem(g2, state, dataArea, this,
                    domainAxis, rangeAxis, currentDataset,
                    row, column, pass);
              }
            }
          }
        } else {
          for (int column = columnCount - 1; column >= 0; column--) {
            if (this.rowRenderingOrder == SortOrder.ASCENDING) {
              for (int row = 0; row < rowCount; row++) {
                renderer.drawItem(g2, state, dataArea, this,
                    domainAxis, rangeAxis, currentDataset,
                    row, column, pass);
              }
            } else {
              for (int row = rowCount - 1; row >= 0; row--) {
                renderer.drawItem(g2, state, dataArea, this,
                    domainAxis, rangeAxis, currentDataset,
                    row, column, pass);
              }
            }
          }
        }
      }
    }
    return foundData;

  }

  /**
   * Receives notification of a renderer change event.
   *
   * @param event
   *            the event.
   */
  public void rendererChanged(final RendererChangeEvent event) {
    final Plot parent = this.getParent();
    if (parent != null) {
      if (parent instanceof RendererChangeListener) {
        final RendererChangeListener rcl = (RendererChangeListener) parent;
        rcl.rendererChanged(event);
      } else {
        // this should never happen with the existing code, but throw
        // an exception in case future changes make it possible...
        throw new RuntimeException(
            "The renderer has changed and I don't know what to do!");
      }
    } else {
      this.configureRangeAxes();
      final PlotChangeEvent e = new PlotChangeEvent(this);
      this.notifyListeners(e);
    }
  }

  /**
   * Sets the anchor value.
   *
   * @param value
   *            the anchor value.
   */
  public void setAnchorValue(final double value) {
    this.setAnchorValue(value, true);
  }

  /**
   * Sets the anchor value.
   *
   * @param value
   *            the value.
   * @param notify
   *            notify listeners?
   */
  public void setAnchorValue(final double value, final boolean notify) {
    this.anchorValue = value;
    if (notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the axis offsets (gap between the data area and the axes).
   *
   * @param offset
   *            the offset (<code>null</code> not permitted).
   */
  public void setAxisOffset(final RectangleInsets offset) {
    if (offset == null) {
      throw new IllegalArgumentException("Null 'offset' argument.");
    }
    this.axisOffset = offset;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the order in which the columns should be rendered.
   *
   * @param order
   *            the order.
   */
  public void setColumnRenderingOrder(final SortOrder order) {
    this.columnRenderingOrder = order;
  }

  /**
   * Sets the dataset for the plot, replacing the existing dataset, if there
   * is one. This method also calls the
   * {@link #datasetChanged(DatasetChangeEvent)} method, which adjusts the
   * axis ranges if necessary and sends a {@link PlotChangeEvent} to all
   * registered listeners.
   *
   * @param dataset
   *            the dataset (<code>null</code> permitted).
   */
  public void setDataset(final CategoryDataset dataset) {
    this.setDataset(0, dataset);
  }

  /**
   * Sets a dataset for the plot.
   *
   * @param index
   *            the dataset index.
   * @param dataset
   *            the dataset (<code>null</code> permitted).
   */
  public void setDataset(final int index, final CategoryDataset dataset) {

    final CategoryDataset existing = (CategoryDataset) this.datasets
        .get(index);
    if (existing != null) {
      existing.removeChangeListener(this);
    }
    this.datasets.set(index, dataset);
    if (dataset != null) {
      dataset.addChangeListener(this);
    }

    // send a dataset change event to self...
    final DatasetChangeEvent event = new DatasetChangeEvent(this, dataset);
    this.datasetChanged(event);

  }

  /**
   * Sets the rendering order and sends a {@link PlotChangeEvent} to all
   * registered listeners. By default, the plot renders the primary dataset
   * last (so that the primary dataset overlays the secondary datasets). You
   * can reverse this if you want to.
   *
   * @param order
   *            the rendering order (<code>null</code> not permitted).
   */
  public void setDatasetRenderingOrder(final DatasetRenderingOrder order) {
    if (order == null) {
      throw new IllegalArgumentException("Null 'order' argument.");
    }
    this.renderingOrder = order;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the domain axes for this plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param axes
   *            the axes.
   */
  public void setDomainAxes(final CategoryAxis[] axes) {
    for (int i = 0; i < axes.length; i++) {
      this.setDomainAxis(i, axes[i], false);
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the domain axis for the plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param axis
   *            the axis (<code>null</code> permitted).
   */
  public void setDomainAxis(final CategoryAxis axis) {
    this.setDomainAxis(0, axis);
  }

  /**
   * Sets a domain axis and sends a {@link PlotChangeEvent} to all registered
   * listeners.
   *
   * @param index
   *            the axis index.
   * @param axis
   *            the axis.
   */
  public void setDomainAxis(final int index, final CategoryAxis axis) {
    this.setDomainAxis(index, axis, true);
  }

  /**
   * Sets a domain axis and, if requested, sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param index
   *            the axis index.
   * @param axis
   *            the axis.
   * @param notify
   *            notify listeners?
   */
  public void setDomainAxis(final int index, final CategoryAxis axis,
      final boolean notify) {
    final CategoryAxis existing = (CategoryAxis) this.domainAxes.get(index);
    if (existing != null) {
      existing.removeChangeListener(this);
    }
    if (axis != null) {
      axis.setPlot(this);
    }
    this.domainAxes.set(index, axis);
    if (axis != null) {
      axis.configure();
      axis.addChangeListener(this);
    }
    if (notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the location of the domain axis and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   *
   * @param location
   *            the axis location (<code>null</code> not permitted).
   */
  public void setDomainAxisLocation(final AxisLocation location) {
    // defer argument checking...
    this.setDomainAxisLocation(location, true);
  }

  /**
   * Sets the location of the domain axis.
   *
   * @param location
   *            the axis location (<code>null</code> not permitted).
   * @param notify
   *            a flag that controls whether listeners are notified.
   */
  public void setDomainAxisLocation(final AxisLocation location,
      final boolean notify) {
    if (location == null) {
      throw new IllegalArgumentException("Null 'location' argument.");
    }
    this.setDomainAxisLocation(0, location);
  }

  /**
   * Sets the location for a domain axis and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   *
   * @param index
   *            the axis index.
   * @param location
   *            the location.
   */
  public void setDomainAxisLocation(final int index,
      final AxisLocation location) {
    // TODO: handle argument checking for primary axis location which
    // should not be null
    this.domainAxisLocations.set(index, location);
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the paint used to draw the grid-lines (if any) against the domain
   * axis and sends a {@link PlotChangeEvent} to all registered listeners.
   *
   * @param paint
   *            the paint (<code>null</code> not permitted).
   */
  public void setDomainGridlinePaint(final Color paint) {
    if (paint == null) {
      throw new IllegalArgumentException("Null 'paint' argument.");
    }
    this.domainGridlinePaint = paint;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the position used for the domain gridlines and sends a
   * {@link PlotChangeEvent} to all registered listeners.
   *
   * @param position
   *            the position (<code>null</code> not permitted).
   */
  public void setDomainGridlinePosition(final CategoryAnchor position) {
    if (position == null) {
      throw new IllegalArgumentException("Null 'position' argument.");
    }
    this.domainGridlinePosition = position;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the stroke used to draw grid-lines against the domain axis and sends
   * a {@link PlotChangeEvent} to all registered listeners.
   *
   * @param stroke
   *            the stroke (<code>null</code> not permitted).
   */
  public void setDomainGridlineStroke(final Stroke stroke) {
    if (stroke == null) {
      throw new IllegalArgumentException("Null 'stroke' not permitted.");
    }
    this.domainGridlineStroke = stroke;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the flag that controls whether or not grid-lines are drawn against
   * the domain axis.
   * <p>
   * If the flag value changes, a {@link PlotChangeEvent} is sent to all
   * registered listeners.
   *
   * @param visible
   *            the new value of the flag.
   */
  public void setDomainGridlinesVisible(final boolean visible) {
    if (this.domainGridlinesVisible != visible) {
      this.domainGridlinesVisible = visible;
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the flag that controls whether the shared domain axis is drawn when
   * this plot is being used as a subplot.
   *
   * @param draw
   *            a boolean.
   */
  public void setDrawSharedDomainAxis(final boolean draw) {
    this.drawSharedDomainAxis = draw;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the fixed domain axis space.
   *
   * @param space
   *            the space (<code>null</code> permitted).
   */
  public void setFixedDomainAxisSpace(final AxisSpace space) {
    this.fixedDomainAxisSpace = space;
  }

  /**
   * Sets the fixed legend items for the plot. Leave this set to
   * <code>null</code> if you prefer the legend items to be created
   * automatically.
   *
   * @param items
   *            the legend items (<code>null</code> permitted).
   */
  public void setFixedLegendItems(final LegendItemCollection items) {
    this.fixedLegendItems = items;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the fixed range axis space.
   *
   * @param space
   *            the space (<code>null</code> permitted).
   */
  public void setFixedRangeAxisSpace(final AxisSpace space) {
    this.fixedRangeAxisSpace = space;
  }

  /**
   * Sets the orientation for the plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param orientation
   *            the orientation (<code>null</code> not permitted).
   */
  public void setOrientation(final PlotOrientation orientation) {
    if (orientation == null) {
      throw new IllegalArgumentException("Null 'orientation' argument.");
    }
    this.orientation = orientation;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the range axes for this plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param axes
   *            the axes.
   */
  public void setRangeAxes(final ValueAxis[] axes) {
    for (int i = 0; i < axes.length; i++) {
      this.setRangeAxis(i, axes[i], false);
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets a range axis and sends a {@link PlotChangeEvent} to all registered
   * listeners.
   *
   * @param index
   *            the axis index.
   * @param axis
   *            the axis.
   */
  public void setRangeAxis(final int index, final ValueAxis axis) {
    this.setRangeAxis(index, axis, true);
  }

  /**
   * Sets a range axis and, if requested, sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param index
   *            the axis index.
   * @param axis
   *            the axis.
   * @param notify
   *            notify listeners?
   */
  public void setRangeAxis(final int index, final ValueAxis axis,
      final boolean notify) {
    final ValueAxis existing = (ValueAxis) this.rangeAxes.get(index);
    if (existing != null) {
      existing.removeChangeListener(this);
    }
    if (axis != null) {
      axis.setPlot(this);
    }
    this.rangeAxes.set(index, axis);
    if (axis != null) {
      axis.configure();
      axis.addChangeListener(this);
    }
    if (notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the range axis for the plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param axis
   *            the axis (<code>null</code> permitted).
   */
  public void setRangeAxis(final ValueAxis axis) {
    this.setRangeAxis(0, axis);
  }

  /**
   * Sets the location of the range axis and sends a {@link PlotChangeEvent}
   * to all registered listeners.
   *
   * @param location
   *            the location (<code>null</code> not permitted).
   */
  public void setRangeAxisLocation(final AxisLocation location) {
    // defer argument checking...
    this.setRangeAxisLocation(location, true);
  }

  /**
   * Sets the location of the range axis and, if requested, sends a
   * {@link PlotChangeEvent} to all registered listeners.
   *
   * @param location
   *            the location (<code>null</code> not permitted).
   * @param notify
   *            notify listeners?
   */
  public void setRangeAxisLocation(final AxisLocation location,
      final boolean notify) {
    this.setRangeAxisLocation(0, location, notify);
  }

  /**
   * Sets the location for a range axis and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param index
   *            the axis index.
   * @param location
   *            the location.
   */
  public void setRangeAxisLocation(final int index,
      final AxisLocation location) {
    this.setRangeAxisLocation(index, location, true);
  }

  /**
   * Sets the location for a range axis and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param index
   *            the axis index.
   * @param location
   *            the location.
   * @param notify
   *            notify listeners?
   */
  public void setRangeAxisLocation(final int index,
      final AxisLocation location, final boolean notify) {
    // TODO: don't allow null for index = 0
    this.rangeAxisLocations.set(index, location);
    if (notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the flag indicating whether or not the range crosshair should
   * "lock-on" to actual data values.
   *
   * @param flag
   *            the flag.
   */
  public void setRangeCrosshairLockedOnData(final boolean flag) {

    if (this.rangeCrosshairLockedOnData != flag) {
      this.rangeCrosshairLockedOnData = flag;
      this.notifyListeners(new PlotChangeEvent(this));
    }

  }

  /**
   * Sets the Paint used to color the crosshairs (if visible) and notifies
   * registered listeners that the axis has been modified.
   *
   * @param paint
   *            the new crosshair paint.
   */
  public void setRangeCrosshairPaint(final Color paint) {
    this.rangeCrosshairPaint = paint;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the pen-style (<code>Stroke</code>) used to draw the crosshairs (if
   * visible). A {@link PlotChangeEvent} is sent to all registered listeners.
   *
   * @param stroke
   *            the new crosshair stroke.
   */
  public void setRangeCrosshairStroke(final Stroke stroke) {
    this.rangeCrosshairStroke = stroke;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the domain crosshair value.
   * <P>
   * Registered listeners are notified that the plot has been modified, but
   * only if the crosshair is visible.
   *
   * @param value
   *            the new value.
   */
  public void setRangeCrosshairValue(final double value) {
    this.setRangeCrosshairValue(value, true);
  }

  /**
   * Sets the range crosshair value.
   * <P>
   * Registered listeners are notified that the axis has been modified, but
   * only if the crosshair is visible.
   *
   * @param value
   *            the new value.
   * @param notify
   *            a flag that controls whether or not listeners are notified.
   */
  public void setRangeCrosshairValue(final double value, final boolean notify) {
    this.rangeCrosshairValue = value;
    if (this.isRangeCrosshairVisible() && notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the flag indicating whether or not the range crosshair is visible.
   *
   * @param flag
   *            the new value of the flag.
   */
  public void setRangeCrosshairVisible(final boolean flag) {

    if (this.rangeCrosshairVisible != flag) {
      this.rangeCrosshairVisible = flag;
      this.notifyListeners(new PlotChangeEvent(this));
    }

  }

  /**
   * Sets the paint used to draw the grid lines against the range axis and
   * sends a {@link PlotChangeEvent} to all registered listeners.
   *
   * @param paint
   *            the paint (<code>null</code> not permitted).
   */
  public void setRangeGridlinePaint(final Color paint) {
    if (paint == null) {
      throw new IllegalArgumentException("Null 'paint' argument.");
    }
    this.rangeGridlinePaint = paint;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the stroke used to draw the grid-lines against the range axis and
   * sends a {@link PlotChangeEvent} to all registered listeners.
   *
   * @param stroke
   *            the stroke (<code>null</code> not permitted).
   */
  public void setRangeGridlineStroke(final Stroke stroke) {
    if (stroke == null) {
      throw new IllegalArgumentException("Null 'stroke' argument.");
    }
    this.rangeGridlineStroke = stroke;
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the flag that controls whether or not grid-lines are drawn against
   * the range axis. If the flag changes value, a {@link PlotChangeEvent} is
   * sent to all registered listeners.
   *
   * @param visible
   *            the new value of the flag.
   */
  public void setRangeGridlinesVisible(final boolean visible) {
    if (this.rangeGridlinesVisible != visible) {
      this.rangeGridlinesVisible = visible;
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the renderer at index 0 (sometimes referred to as the "primary"
   * renderer) and sends a {@link PlotChangeEvent} to all registered
   * listeners.
   *
   * @param renderer
   *            the renderer (<code>null</code> permitted.
   */
  public void setRenderer(final CategoryItemRenderer renderer) {
    this.setRenderer(0, renderer, true);
  }

  /**
   * Sets the renderer at index 0 (sometimes referred to as the "primary"
   * renderer) and, if requested, sends a {@link PlotChangeEvent} to all
   * registered listeners.
   * <p>
   * You can set the renderer to <code>null</code>, but this is not
   * recommended because:
   * <ul>
   * <li>no data will be displayed;</li>
   * <li>the plot background will not be painted;</li>
   * </ul>
   *
   * @param renderer
   *            the renderer (<code>null</code> permitted).
   * @param notify
   *            notify listeners?
   */
  public void setRenderer(final CategoryItemRenderer renderer,
      final boolean notify) {
    this.setRenderer(0, renderer, notify);
  }

  /**
   * Sets the renderer at the specified index and sends a
   * {@link PlotChangeEvent} to all registered listeners.
   *
   * @param index
   *            the index.
   * @param renderer
   *            the renderer (<code>null</code> permitted).
   */
  public void setRenderer(final int index, final CategoryItemRenderer renderer) {
    this.setRenderer(index, renderer, true);
  }

  /**
   * Sets a renderer. A {@link PlotChangeEvent} is sent to all registered
   * listeners.
   *
   * @param index
   *            the index.
   * @param renderer
   *            the renderer (<code>null</code> permitted).
   * @param notify
   *            notify listeners?
   */
  public void setRenderer(final int index,
      final CategoryItemRenderer renderer, final boolean notify) {

    // stop listening to the existing renderer...
    final CategoryItemRenderer existing = (CategoryItemRenderer) this.renderers
        .get(index);
    if (existing != null) {
      existing.removeChangeListener(this);
    }

    // register the new renderer...
    this.renderers.set(index, renderer);
    if (renderer != null) {
      renderer.setPlot(this);
      renderer.addChangeListener(this);
    }

    this.configureDomainAxes();
    this.configureRangeAxes();

    if (notify) {
      this.notifyListeners(new PlotChangeEvent(this));
    }
  }

  /**
   * Sets the renderers for this plot and sends a {@link PlotChangeEvent} to
   * all registered listeners.
   *
   * @param renderers
   *            the renderers.
   */
  public void setRenderers(final CategoryItemRenderer[] renderers) {
    for (int i = 0; i < renderers.length; i++) {
      this.setRenderer(i, renderers[i], false);
    }
    this.notifyListeners(new PlotChangeEvent(this));
  }

  /**
   * Sets the order in which the rows should be rendered.
   *
   * @param order
   *            the order (<code>null</code> not allowed).
   */
  public void setRowRenderingOrder(final SortOrder order) {
    if (order == null) {
      throw new IllegalArgumentException("Null 'order' argument.");
    }
    this.rowRenderingOrder = order;
  }

  /**
   * Sets the weight for the plot.
   *
   * @param weight
   *            the weight.
   */
  public void setWeight(final int weight) {
    this.weight = weight;
  }

  /**
   * Zooms (in or out) on the plot's value axis.
   * <p>
   * If the value 0.0 is passed in as the zoom percent, the auto-range
   * calculation for the axis is restored (which sets the range to include the
   * minimum and maximum data values, thus displaying all the data).
   *
   * @param percent
   *            the zoom amount.
   */
  public void zoom(final double percent) {

    if (percent > 0.0) {
      final double range = this.getRangeAxis().getRange().getLength();
      final double scaledRange = range * percent;
      this.getRangeAxis().setRange(this.anchorValue - scaledRange / 2.0,
          this.anchorValue + scaledRange / 2.0);
    } else {
      this.getRangeAxis().setAutoRange(true);
    }

  }

  /**
   * This method does nothing, because <code>CategoryPlot</code> doesn't
   * support zooming on the domain.
   *
   * @param lowerPercent
   *            the lower bound.
   * @param upperPercent
   *            the upper bound.
   * @param state
   *            the plot state.
   * @param source
   *            the source point (in Java2D space) for the zoom.
   */
  public void zoomDomainAxes(final double lowerPercent,
      final double upperPercent, final PlotRenderingInfo state,
      final Point source) {
    // can't zoom domain axis
  }

  /**
   * This method does nothing, because <code>CategoryPlot</code> doesn't
   * support zooming on the domain.
   *
   * @param factor
   *            the zoom factor.
   * @param state
   *            the plot state.
   * @param source
   *            the source point (in Java2D space) for the zoom.
   */
  public void zoomDomainAxes(final double factor,
      final PlotRenderingInfo state, final Point source) {
    // can't zoom domain axis
  }

  /**
   * Zooms in on the range axes.
   *
   * @param lowerPercent
   *            the lower bound.
   * @param upperPercent
   *            the upper bound.
   * @param state
   *            the plot state.
   * @param source
   *            the source point (in Java2D space) for the zoom.
   */
  public void zoomRangeAxes(final double lowerPercent,
      final double upperPercent, final PlotRenderingInfo state,
      final Point source) {
    for (int i = 0; i < this.rangeAxes.size(); i++) {
      final ValueAxis rangeAxis = (ValueAxis) this.rangeAxes.get(i);
      if (rangeAxis != null) {
        rangeAxis.zoomRange(lowerPercent, upperPercent);
      }
    }
  }

  /**
   * 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 space) for the zoom.
   */
  public void zoomRangeAxes(final double factor,
      final PlotRenderingInfo state, final Point source) {
    for (int i = 0; i < this.rangeAxes.size(); i++) {
      final ValueAxis rangeAxis = (ValueAxis) this.rangeAxes.get(i);
      if (rangeAxis != null) {
        rangeAxis.resizeRange(factor);
      }
    }
  }
}
TOP

Related Classes of com.positive.charts.plot.CategoryPlot

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.