sascha@1823: /* ===========================================================
sascha@1823:  * JFreeChart : a free chart library for the Java(tm) platform
sascha@1823:  * ===========================================================
sascha@1823:  *
sascha@1823:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
sascha@1823:  *
sascha@1823:  * Project Info:  http://www.jfree.org/jfreechart/index.html
sascha@1823:  *
sascha@1823:  * This library is free software; you can redistribute it and/or modify it
sascha@1823:  * under the terms of the GNU Lesser General Public License as published by
sascha@1823:  * the Free Software Foundation; either version 2.1 of the License, or
sascha@1823:  * (at your option) any later version.
sascha@1823:  *
sascha@1823:  * This library is distributed in the hope that it will be useful, but
sascha@1823:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
sascha@1823:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
sascha@1823:  * License for more details.
sascha@1823:  *
sascha@1823:  * You should have received a copy of the GNU Lesser General Public
sascha@1823:  * License along with this library; if not, write to the Free Software
sascha@1823:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
sascha@1823:  * USA.
sascha@1823:  *
sascha@1823:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
sascha@1823:  * in the United States and other countries.]
sascha@1823:  *
sascha@1823:  * -------------------------
sascha@1823:  * StableXYDifferenceRenderer.java
sascha@1823:  * -------------------------
sascha@1823:  * (C) Copyright 2003-2008, by Object Refinery Limited and Contributors.
sascha@1823:  *
sascha@1823:  * Original Author:  David Gilbert (for Object Refinery Limited);
sascha@1823:  * Contributor(s):   Richard West, Advanced Micro Devices, Inc. (major rewrite
sascha@1823:  *                   of difference drawing algorithm);
sascha@1823:  *
sascha@1823:  * Changes:
sascha@1823:  * --------
sascha@1823:  * 30-Apr-2003 : Version 1 (DG);
sascha@1823:  * 30-Jul-2003 : Modified entity constructor (CZ);
sascha@1823:  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
sascha@1823:  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
sascha@1823:  * 09-Feb-2004 : Updated to support horizontal plot orientation (DG);
sascha@1823:  * 10-Feb-2004 : Added default constructor, setter methods and updated
sascha@1823:  *               Javadocs (DG);
sascha@1823:  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
sascha@1823:  * 30-Mar-2004 : Fixed bug in getNegativePaint() method (DG);
sascha@1823:  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
sascha@1823:  *               getYValue() (DG);
sascha@1823:  * 25-Aug-2004 : Fixed a bug preventing the use of crosshairs (DG);
sascha@1823:  * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
sascha@1823:  * 19-Jan-2005 : Now accesses only primitive values from dataset (DG);
sascha@1823:  * 22-Feb-2005 : Override getLegendItem(int, int) to return "line" items (DG);
sascha@1823:  * 13-Apr-2005 : Fixed shape positioning bug (id = 1182062) (DG);
sascha@1823:  * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
sascha@1823:  * 04-May-2005 : Override equals() method, renamed get/setPlotShapes() -->
sascha@1823:  *               get/setShapesVisible (DG);
sascha@1823:  * 09-Jun-2005 : Updated equals() to handle GradientPaint (DG);
sascha@1823:  * 16-Jun-2005 : Fix bug (1221021) affecting stroke used for each series (DG);
sascha@1823:  * ------------- JFREECHART 1.0.x ---------------------------------------------
sascha@1823:  * 24-Jan-2007 : Added flag to allow rounding of x-coordinates, and fixed
sascha@1823:  *               bug in clone() (DG);
sascha@1823:  * 05-Feb-2007 : Added an extra call to updateCrosshairValues() in
sascha@1823:  *               drawItemPass1(), to fix bug 1564967 (DG);
sascha@1823:  * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
sascha@1823:  * 08-Mar-2007 : Fixed entity generation (DG);
sascha@1823:  * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
sascha@1823:  * 23-Apr-2007 : Rewrite of difference drawing algorithm to allow use of
sascha@1823:  *               series with disjoint x-values (RW);
sascha@1823:  * 04-May-2007 : Set processVisibleItemsOnly flag to false (DG);
sascha@1823:  * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
sascha@1823:  * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
sascha@1823:  * 05-Nov-2007 : Draw item labels if visible (RW);
sascha@1823:  * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG);
sascha@1823:  *
sascha@1823:  */
sascha@1824: package de.intevation.flys.jfree;
sascha@1823: 
sascha@1823: import java.awt.Color;
sascha@1823: import java.awt.Graphics2D;
sascha@1823: import java.awt.Paint;
sascha@1823: import java.awt.Shape;
sascha@1823: import java.awt.Stroke;
sascha@1823: import java.awt.geom.GeneralPath;
sascha@1823: import java.awt.geom.Line2D;
sascha@1823: import java.awt.geom.Rectangle2D;
sascha@1823: import java.io.IOException;
sascha@1823: import java.io.ObjectInputStream;
sascha@1823: import java.io.ObjectOutputStream;
sascha@1823: import java.util.Collections;
sascha@1823: import java.util.LinkedList;
sascha@1824: import java.util.Iterator;
sascha@1823: 
sascha@1823: import org.jfree.chart.LegendItem;
sascha@1823: import org.jfree.chart.axis.ValueAxis;
sascha@1823: import org.jfree.chart.entity.EntityCollection;
sascha@1823: import org.jfree.chart.entity.XYItemEntity;
sascha@1823: import org.jfree.chart.event.RendererChangeEvent;
sascha@1823: import org.jfree.chart.labels.XYToolTipGenerator;
sascha@1823: import org.jfree.chart.plot.CrosshairState;
sascha@1823: import org.jfree.chart.plot.PlotOrientation;
sascha@1823: import org.jfree.chart.plot.PlotRenderingInfo;
sascha@1823: import org.jfree.chart.plot.XYPlot;
sascha@1823: import org.jfree.chart.urls.XYURLGenerator;
sascha@1823: import org.jfree.data.xy.XYDataset;
sascha@1823: import org.jfree.io.SerialUtilities;
sascha@1823: import org.jfree.ui.RectangleEdge;
sascha@1823: import org.jfree.util.PaintUtilities;
sascha@1823: import org.jfree.util.PublicCloneable;
sascha@1823: import org.jfree.util.ShapeUtilities;
sascha@1823: 
sascha@1824: import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
sascha@1824: import org.jfree.chart.renderer.xy.XYItemRenderer;
sascha@1824: import org.jfree.chart.renderer.xy.XYItemRendererState;
sascha@1824: 
sascha@1823: /**
sascha@1823:  * A renderer for an {@link XYPlot} that highlights the differences between two
sascha@1823:  * series.  The example shown here is generated by the
sascha@1823:  * <code>DifferenceChartDemo1.java</code> program included in the JFreeChart
sascha@1823:  * demo collection:
sascha@1823:  * <br><br>
sascha@1823:  * <img src="../../../../../images/StableXYDifferenceRendererSample.png"
sascha@1823:  * alt="StableXYDifferenceRendererSample.png" />
sascha@1823:  */
sascha@1823: public class StableXYDifferenceRenderer extends AbstractXYItemRenderer
sascha@1823:         implements XYItemRenderer, PublicCloneable {
sascha@1823: 
sascha@1823:     /** For serialization. */
sascha@1823:     private static final long serialVersionUID = -8447915602375584857L;
sascha@1823: 
sascha@1823:     /** The paint used to highlight positive differences (y(0) > y(1)). */
sascha@1823:     private transient Paint positivePaint;
sascha@1823: 
sascha@1823:     /** The paint used to highlight negative differences (y(0) < y(1)). */
sascha@1823:     private transient Paint negativePaint;
sascha@1823: 
sascha@1823:     /** Display shapes at each point? */
sascha@1823:     private boolean shapesVisible;
sascha@1823: 
sascha@1823:     /** The shape to display in the legend item. */
sascha@1823:     private transient Shape legendLine;
sascha@1823: 
sascha@1824:     private XYDatasetToZeroMapper mapper;
sascha@1824: 
sascha@1823:     /**
sascha@1823:      * This flag controls whether or not the x-coordinates (in Java2D space)
sascha@1823:      * are rounded to integers.  When set to true, this can avoid the vertical
sascha@1823:      * striping that anti-aliasing can generate.  However, the rounding may not
sascha@1823:      * be appropriate for output in high resolution formats (for example,
sascha@1823:      * vector graphics formats such as SVG and PDF).
sascha@1823:      *
sascha@1823:      * @since 1.0.4
sascha@1823:      */
sascha@1823:     private boolean roundXCoordinates;
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Creates a new renderer with default attributes.
sascha@1823:      */
sascha@1823:     public StableXYDifferenceRenderer() {
sascha@1824:         this(Color.green, Color.red, false, null);
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Creates a new renderer.
sascha@1823:      *
sascha@1823:      * @param positivePaint  the highlight color for positive differences
sascha@1823:      *                       (<code>null</code> not permitted).
sascha@1823:      * @param negativePaint  the highlight color for negative differences
sascha@1823:      *                       (<code>null</code> not permitted).
sascha@1823:      * @param shapes  draw shapes?
sascha@1823:      */
sascha@1823:     public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
sascha@1824:                                 boolean shapes,
sascha@1824:                                 XYDatasetToZeroMapper mapper) {
sascha@1823:         if (positivePaint == null) {
sascha@1823:             throw new IllegalArgumentException(
sascha@1823:                     "Null 'positivePaint' argument.");
sascha@1823:         }
sascha@1823:         if (negativePaint == null) {
sascha@1823:             throw new IllegalArgumentException(
sascha@1823:                     "Null 'negativePaint' argument.");
sascha@1823:         }
sascha@1823:         this.positivePaint = positivePaint;
sascha@1823:         this.negativePaint = negativePaint;
sascha@1823:         this.shapesVisible = shapes;
sascha@1823:         this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);
sascha@1823:         this.roundXCoordinates = false;
sascha@1824:         this.mapper = mapper;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns the paint used to highlight positive differences.
sascha@1823:      *
sascha@1823:      * @return The paint (never <code>null</code>).
sascha@1823:      *
sascha@1823:      * @see #setPositivePaint(Paint)
sascha@1823:      */
sascha@1823:     public Paint getPositivePaint() {
sascha@1823:         return this.positivePaint;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Sets the paint used to highlight positive differences and sends a
sascha@1823:      * {@link RendererChangeEvent} to all registered listeners.
sascha@1823:      *
sascha@1823:      * @param paint  the paint (<code>null</code> not permitted).
sascha@1823:      *
sascha@1823:      * @see #getPositivePaint()
sascha@1823:      */
sascha@1823:     public void setPositivePaint(Paint paint) {
sascha@1823:         if (paint == null) {
sascha@1823:             throw new IllegalArgumentException("Null 'paint' argument.");
sascha@1823:         }
sascha@1823:         this.positivePaint = paint;
sascha@1823:         fireChangeEvent();
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns the paint used to highlight negative differences.
sascha@1823:      *
sascha@1823:      * @return The paint (never <code>null</code>).
sascha@1823:      *
sascha@1823:      * @see #setNegativePaint(Paint)
sascha@1823:      */
sascha@1823:     public Paint getNegativePaint() {
sascha@1823:         return this.negativePaint;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Sets the paint used to highlight negative differences.
sascha@1823:      *
sascha@1823:      * @param paint  the paint (<code>null</code> not permitted).
sascha@1823:      *
sascha@1823:      * @see #getNegativePaint()
sascha@1823:      */
sascha@1823:     public void setNegativePaint(Paint paint) {
sascha@1823:         if (paint == null) {
sascha@1823:             throw new IllegalArgumentException("Null 'paint' argument.");
sascha@1823:         }
sascha@1823:         this.negativePaint = paint;
sascha@1823:         notifyListeners(new RendererChangeEvent(this));
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns a flag that controls whether or not shapes are drawn for each
sascha@1823:      * data value.
sascha@1823:      *
sascha@1823:      * @return A boolean.
sascha@1823:      *
sascha@1823:      * @see #setShapesVisible(boolean)
sascha@1823:      */
sascha@1823:     public boolean getShapesVisible() {
sascha@1823:         return this.shapesVisible;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Sets a flag that controls whether or not shapes are drawn for each
sascha@1823:      * data value, and sends a {@link RendererChangeEvent} to all registered
sascha@1823:      * listeners.
sascha@1823:      *
sascha@1823:      * @param flag  the flag.
sascha@1823:      *
sascha@1823:      * @see #getShapesVisible()
sascha@1823:      */
sascha@1823:     public void setShapesVisible(boolean flag) {
sascha@1823:         this.shapesVisible = flag;
sascha@1823:         fireChangeEvent();
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns the shape used to represent a line in the legend.
sascha@1823:      *
sascha@1823:      * @return The legend line (never <code>null</code>).
sascha@1823:      *
sascha@1823:      * @see #setLegendLine(Shape)
sascha@1823:      */
sascha@1823:     public Shape getLegendLine() {
sascha@1823:         return this.legendLine;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Sets the shape used as a line in each legend item and sends a
sascha@1823:      * {@link RendererChangeEvent} to all registered listeners.
sascha@1823:      *
sascha@1823:      * @param line  the line (<code>null</code> not permitted).
sascha@1823:      *
sascha@1823:      * @see #getLegendLine()
sascha@1823:      */
sascha@1823:     public void setLegendLine(Shape line) {
sascha@1823:         if (line == null) {
sascha@1823:             throw new IllegalArgumentException("Null 'line' argument.");
sascha@1823:         }
sascha@1823:         this.legendLine = line;
sascha@1823:         fireChangeEvent();
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns the flag that controls whether or not the x-coordinates (in
sascha@1823:      * Java2D space) are rounded to integer values.
sascha@1823:      *
sascha@1823:      * @return The flag.
sascha@1823:      *
sascha@1823:      * @since 1.0.4
sascha@1823:      *
sascha@1823:      * @see #setRoundXCoordinates(boolean)
sascha@1823:      */
sascha@1823:     public boolean getRoundXCoordinates() {
sascha@1823:         return this.roundXCoordinates;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Sets the flag that controls whether or not the x-coordinates (in
sascha@1823:      * Java2D space) are rounded to integer values, and sends a
sascha@1823:      * {@link RendererChangeEvent} to all registered listeners.
sascha@1823:      *
sascha@1823:      * @param round  the new flag value.
sascha@1823:      *
sascha@1823:      * @since 1.0.4
sascha@1823:      *
sascha@1823:      * @see #getRoundXCoordinates()
sascha@1823:      */
sascha@1823:     public void setRoundXCoordinates(boolean round) {
sascha@1823:         this.roundXCoordinates = round;
sascha@1823:         fireChangeEvent();
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Initialises the renderer and returns a state object that should be
sascha@1823:      * passed to subsequent calls to the drawItem() method.  This method will
sascha@1823:      * be called before the first item is rendered, giving the renderer an
sascha@1823:      * opportunity to initialise any state information it wants to maintain.
sascha@1823:      * The renderer can do nothing if it chooses.
sascha@1823:      *
sascha@1823:      * @param g2  the graphics device.
sascha@1823:      * @param dataArea  the area inside the axes.
sascha@1823:      * @param plot  the plot.
sascha@1823:      * @param data  the data.
sascha@1823:      * @param info  an optional info collection object to return data back to
sascha@1823:      *              the caller.
sascha@1823:      *
sascha@1823:      * @return A state object.
sascha@1823:      */
sascha@1823:     public XYItemRendererState initialise(Graphics2D g2,
sascha@1823:                                           Rectangle2D dataArea,
sascha@1823:                                           XYPlot plot,
sascha@1823:                                           XYDataset data,
sascha@1823:                                           PlotRenderingInfo info) {
sascha@1823: 
sascha@1823:         XYItemRendererState state = super.initialise(g2, dataArea, plot, data,
sascha@1823:                 info);
sascha@1823:         state.setProcessVisibleItemsOnly(false);
sascha@1823:         return state;
sascha@1823: 
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns <code>2</code>, the number of passes required by the renderer.
sascha@1823:      * The {@link XYPlot} will run through the dataset this number of times.
sascha@1823:      *
sascha@1823:      * @return The number of passes required by the renderer.
sascha@1823:      */
sascha@1823:     public int getPassCount() {
sascha@1823:         return 2;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Draws the visual representation of a single data item.
sascha@1823:      *
sascha@1823:      * @param g2  the graphics device.
sascha@1823:      * @param state  the renderer state.
sascha@1823:      * @param dataArea  the area within which the data is being drawn.
sascha@1823:      * @param info  collects information about the drawing.
sascha@1823:      * @param plot  the plot (can be used to obtain standard color
sascha@1823:      *              information etc).
sascha@1823:      * @param domainAxis  the domain (horizontal) axis.
sascha@1823:      * @param rangeAxis  the range (vertical) axis.
sascha@1823:      * @param dataset  the dataset.
sascha@1823:      * @param series  the series index (zero-based).
sascha@1823:      * @param item  the item index (zero-based).
sascha@1823:      * @param crosshairState  crosshair information for the plot
sascha@1823:      *                        (<code>null</code> permitted).
sascha@1823:      * @param pass  the pass index.
sascha@1823:      */
sascha@1823:     public void drawItem(Graphics2D g2,
sascha@1823:                          XYItemRendererState state,
sascha@1823:                          Rectangle2D dataArea,
sascha@1823:                          PlotRenderingInfo info,
sascha@1823:                          XYPlot plot,
sascha@1823:                          ValueAxis domainAxis,
sascha@1823:                          ValueAxis rangeAxis,
sascha@1823:                          XYDataset dataset,
sascha@1823:                          int series,
sascha@1823:                          int item,
sascha@1823:                          CrosshairState crosshairState,
sascha@1823:                          int pass) {
sascha@1823: 
sascha@1824:         if (mapper == null) {
sascha@1824:             switch (pass) {
sascha@1824:                 case 0:
sascha@1824:                     drawItemPass0(g2, dataArea, info, 
sascha@1824:                         plot, domainAxis, rangeAxis,
sascha@1824:                         dataset, series, item, crosshairState);
sascha@1824:                     break;
sascha@1824:                 case 1:
sascha@1824:                     drawItemPass1(g2, dataArea, info,
sascha@1824:                         plot, domainAxis, rangeAxis,
sascha@1824:                         dataset, series, item, crosshairState);
sascha@1824:             }
sascha@1823:         }
sascha@1824:         else {
sascha@1824:             for (Iterator<XYDataset> iter = mapper.iterator(dataset);
sascha@1824:                 iter.hasNext();
sascha@1824:             ) {
sascha@1824:                 switch (pass) {
sascha@1824:                     case 0:
sascha@1824:                         drawItemPass0(g2, dataArea, info, 
sascha@1824:                             plot, domainAxis, rangeAxis,
sascha@1824:                             dataset, series, item, crosshairState);
sascha@1824:                         break;
sascha@1824:                     case 1:
sascha@1824:                         drawItemPass1(g2, dataArea, info,
sascha@1824:                             plot, domainAxis, rangeAxis,
sascha@1824:                             dataset, series, item, crosshairState);
sascha@1824:                 }
sascha@1824:             } // for all segments
sascha@1823:         }
sascha@1823: 
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Draws the visual representation of a single data item, first pass.
sascha@1823:      *
sascha@1823:      * @param x_graphics  the graphics device.
sascha@1823:      * @param x_dataArea  the area within which the data is being drawn.
sascha@1823:      * @param x_info  collects information about the drawing.
sascha@1823:      * @param x_plot  the plot (can be used to obtain standard color
sascha@1823:      *                information etc).
sascha@1823:      * @param x_domainAxis  the domain (horizontal) axis.
sascha@1823:      * @param x_rangeAxis  the range (vertical) axis.
sascha@1823:      * @param x_dataset  the dataset.
sascha@1823:      * @param x_series  the series index (zero-based).
sascha@1823:      * @param x_item  the item index (zero-based).
sascha@1823:      * @param x_crosshairState  crosshair information for the plot
sascha@1823:      *                          (<code>null</code> permitted).
sascha@1823:      */
sascha@1823:     protected void drawItemPass0(Graphics2D x_graphics,
sascha@1823:                                  Rectangle2D x_dataArea,
sascha@1823:                                  PlotRenderingInfo x_info,
sascha@1823:                                  XYPlot x_plot,
sascha@1823:                                  ValueAxis x_domainAxis,
sascha@1823:                                  ValueAxis x_rangeAxis,
sascha@1823:                                  XYDataset x_dataset,
sascha@1823:                                  int x_series,
sascha@1823:                                  int x_item,
sascha@1823:                                  CrosshairState x_crosshairState) {
sascha@1823: 
sascha@1823:         if (!((0 == x_series) && (0 == x_item))) {
sascha@1823:             return;
sascha@1823:         }
sascha@1823: 
sascha@1823:         boolean b_impliedZeroSubtrahend = (1 == x_dataset.getSeriesCount());
sascha@1823: 
sascha@1823:         // check if either series is a degenerate case (i.e. less than 2 points)
sascha@1823:         if (isEitherSeriesDegenerate(x_dataset, b_impliedZeroSubtrahend)) {
sascha@1823:             return;
sascha@1823:         }
sascha@1823: 
sascha@1823:         // check if series are disjoint (i.e. domain-spans do not overlap)
sascha@1823:         if (!b_impliedZeroSubtrahend && areSeriesDisjoint(x_dataset)) {
sascha@1823:             return;
sascha@1823:         }
sascha@1823: 
sascha@1823:         // polygon definitions
sascha@1823:         LinkedList l_minuendXs    = new LinkedList();
sascha@1823:         LinkedList l_minuendYs    = new LinkedList();
sascha@1823:         LinkedList l_subtrahendXs = new LinkedList();
sascha@1823:         LinkedList l_subtrahendYs = new LinkedList();
sascha@1823:         LinkedList l_polygonXs    = new LinkedList();
sascha@1823:         LinkedList l_polygonYs    = new LinkedList();
sascha@1823: 
sascha@1823:         // state
sascha@1823:         int l_minuendItem      = 0;
sascha@1823:         int l_minuendItemCount = x_dataset.getItemCount(0);
sascha@1823:         Double l_minuendCurX   = null;
sascha@1823:         Double l_minuendNextX  = null;
sascha@1823:         Double l_minuendCurY   = null;
sascha@1823:         Double l_minuendNextY  = null;
sascha@1823:         double l_minuendMaxY   = Double.NEGATIVE_INFINITY;
sascha@1823:         double l_minuendMinY   = Double.POSITIVE_INFINITY;
sascha@1823: 
sascha@1823:         int l_subtrahendItem      = 0;
sascha@1823:         int l_subtrahendItemCount = 0; // actual value set below
sascha@1823:         Double l_subtrahendCurX   = null;
sascha@1823:         Double l_subtrahendNextX  = null;
sascha@1823:         Double l_subtrahendCurY   = null;
sascha@1823:         Double l_subtrahendNextY  = null;
sascha@1823:         double l_subtrahendMaxY   = Double.NEGATIVE_INFINITY;
sascha@1823:         double l_subtrahendMinY   = Double.POSITIVE_INFINITY;
sascha@1823: 
sascha@1823:         // if a subtrahend is not specified, assume it is zero
sascha@1823:         if (b_impliedZeroSubtrahend) {
sascha@1823:             l_subtrahendItem      = 0;
sascha@1823:             l_subtrahendItemCount = 2;
sascha@1823:             l_subtrahendCurX      = new Double(x_dataset.getXValue(0, 0));
sascha@1823:             l_subtrahendNextX     = new Double(x_dataset.getXValue(0,
sascha@1823:                     (l_minuendItemCount - 1)));
sascha@1823:             l_subtrahendCurY      = new Double(0.0);
sascha@1823:             l_subtrahendNextY     = new Double(0.0);
sascha@1823:             l_subtrahendMaxY      = 0.0;
sascha@1823:             l_subtrahendMinY      = 0.0;
sascha@1823: 
sascha@1823:             l_subtrahendXs.add(l_subtrahendCurX);
sascha@1823:             l_subtrahendYs.add(l_subtrahendCurY);
sascha@1823:         }
sascha@1823:         else {
sascha@1823:             l_subtrahendItemCount = x_dataset.getItemCount(1);
sascha@1823:         }
sascha@1823: 
sascha@1823:         boolean b_minuendDone           = false;
sascha@1823:         boolean b_minuendAdvanced       = true;
sascha@1823:         boolean b_minuendAtIntersect    = false;
sascha@1823:         boolean b_minuendFastForward    = false;
sascha@1823:         boolean b_subtrahendDone        = false;
sascha@1823:         boolean b_subtrahendAdvanced    = true;
sascha@1823:         boolean b_subtrahendAtIntersect = false;
sascha@1823:         boolean b_subtrahendFastForward = false;
sascha@1823:         boolean b_colinear              = false;
sascha@1823: 
sascha@1823:         boolean b_positive;
sascha@1823: 
sascha@1823:         // coordinate pairs
sascha@1823:         double l_x1 = 0.0, l_y1 = 0.0; // current minuend point
sascha@1823:         double l_x2 = 0.0, l_y2 = 0.0; // next minuend point
sascha@1823:         double l_x3 = 0.0, l_y3 = 0.0; // current subtrahend point
sascha@1823:         double l_x4 = 0.0, l_y4 = 0.0; // next subtrahend point
sascha@1823: 
sascha@1823:         // fast-forward through leading tails
sascha@1823:         boolean b_fastForwardDone = false;
sascha@1823:         while (!b_fastForwardDone) {
sascha@1823:             // get the x and y coordinates
sascha@1823:             l_x1 = x_dataset.getXValue(0, l_minuendItem);
sascha@1823:             l_y1 = x_dataset.getYValue(0, l_minuendItem);
sascha@1823:             l_x2 = x_dataset.getXValue(0, l_minuendItem + 1);
sascha@1823:             l_y2 = x_dataset.getYValue(0, l_minuendItem + 1);
sascha@1823: 
sascha@1823:             l_minuendCurX  = new Double(l_x1);
sascha@1823:             l_minuendCurY  = new Double(l_y1);
sascha@1823:             l_minuendNextX = new Double(l_x2);
sascha@1823:             l_minuendNextY = new Double(l_y2);
sascha@1823: 
sascha@1823:             if (b_impliedZeroSubtrahend) {
sascha@1823:                 l_x3 = l_subtrahendCurX.doubleValue();
sascha@1823:                 l_y3 = l_subtrahendCurY.doubleValue();
sascha@1823:                 l_x4 = l_subtrahendNextX.doubleValue();
sascha@1823:                 l_y4 = l_subtrahendNextY.doubleValue();
sascha@1823:             }
sascha@1823:             else {
sascha@1823:                 l_x3 = x_dataset.getXValue(1, l_subtrahendItem);
sascha@1823:                 l_y3 = x_dataset.getYValue(1, l_subtrahendItem);
sascha@1823:                 l_x4 = x_dataset.getXValue(1, l_subtrahendItem + 1);
sascha@1823:                 l_y4 = x_dataset.getYValue(1, l_subtrahendItem + 1);
sascha@1823: 
sascha@1823:                 l_subtrahendCurX  = new Double(l_x3);
sascha@1823:                 l_subtrahendCurY  = new Double(l_y3);
sascha@1823:                 l_subtrahendNextX = new Double(l_x4);
sascha@1823:                 l_subtrahendNextY = new Double(l_y4);
sascha@1823:             }
sascha@1823: 
sascha@1823:             if (l_x2 <= l_x3) {
sascha@1823:                 // minuend needs to be fast forwarded
sascha@1823:                 l_minuendItem++;
sascha@1823:                 b_minuendFastForward = true;
sascha@1823:                 continue;
sascha@1823:             }
sascha@1823: 
sascha@1823:             if (l_x4 <= l_x1) {
sascha@1823:                 // subtrahend needs to be fast forwarded
sascha@1823:                 l_subtrahendItem++;
sascha@1823:                 b_subtrahendFastForward = true;
sascha@1823:                 continue;
sascha@1823:             }
sascha@1823: 
sascha@1823:             // check if initial polygon needs to be clipped
sascha@1823:             if ((l_x3 < l_x1) && (l_x1 < l_x4)) {
sascha@1823:                 // project onto subtrahend
sascha@1823:                 double l_slope   = (l_y4 - l_y3) / (l_x4 - l_x3);
sascha@1823:                 l_subtrahendCurX = l_minuendCurX;
sascha@1823:                 l_subtrahendCurY = new Double((l_slope * l_x1)
sascha@1823:                         + (l_y3 - (l_slope * l_x3)));
sascha@1823: 
sascha@1823:                 l_subtrahendXs.add(l_subtrahendCurX);
sascha@1823:                 l_subtrahendYs.add(l_subtrahendCurY);
sascha@1823:             }
sascha@1823: 
sascha@1823:             if ((l_x1 < l_x3) && (l_x3 < l_x2)) {
sascha@1823:                 // project onto minuend
sascha@1823:                 double l_slope = (l_y2 - l_y1) / (l_x2 - l_x1);
sascha@1823:                 l_minuendCurX  = l_subtrahendCurX;
sascha@1823:                 l_minuendCurY  = new Double((l_slope * l_x3)
sascha@1823:                         + (l_y1 - (l_slope * l_x1)));
sascha@1823: 
sascha@1823:                 l_minuendXs.add(l_minuendCurX);
sascha@1823:                 l_minuendYs.add(l_minuendCurY);
sascha@1823:             }
sascha@1823: 
sascha@1823:             l_minuendMaxY    = l_minuendCurY.doubleValue();
sascha@1823:             l_minuendMinY    = l_minuendCurY.doubleValue();
sascha@1823:             l_subtrahendMaxY = l_subtrahendCurY.doubleValue();
sascha@1823:             l_subtrahendMinY = l_subtrahendCurY.doubleValue();
sascha@1823: 
sascha@1823:             b_fastForwardDone = true;
sascha@1823:         }
sascha@1823: 
sascha@1823:         // start of algorithm
sascha@1823:         while (!b_minuendDone && !b_subtrahendDone) {
sascha@1823:             if (!b_minuendDone && !b_minuendFastForward && b_minuendAdvanced) {
sascha@1823:                 l_x1 = x_dataset.getXValue(0, l_minuendItem);
sascha@1823:                 l_y1 = x_dataset.getYValue(0, l_minuendItem);
sascha@1823:                 l_minuendCurX = new Double(l_x1);
sascha@1823:                 l_minuendCurY = new Double(l_y1);
sascha@1823: 
sascha@1823:                 if (!b_minuendAtIntersect) {
sascha@1823:                     l_minuendXs.add(l_minuendCurX);
sascha@1823:                     l_minuendYs.add(l_minuendCurY);
sascha@1823:                 }
sascha@1823: 
sascha@1823:                 l_minuendMaxY = Math.max(l_minuendMaxY, l_y1);
sascha@1823:                 l_minuendMinY = Math.min(l_minuendMinY, l_y1);
sascha@1823: 
sascha@1823:                 l_x2 = x_dataset.getXValue(0, l_minuendItem + 1);
sascha@1823:                 l_y2 = x_dataset.getYValue(0, l_minuendItem + 1);
sascha@1823:                 l_minuendNextX = new Double(l_x2);
sascha@1823:                 l_minuendNextY = new Double(l_y2);
sascha@1823:             }
sascha@1823: 
sascha@1823:             // never updated the subtrahend if it is implied to be zero
sascha@1823:             if (!b_impliedZeroSubtrahend && !b_subtrahendDone
sascha@1823:                     && !b_subtrahendFastForward && b_subtrahendAdvanced) {
sascha@1823:                 l_x3 = x_dataset.getXValue(1, l_subtrahendItem);
sascha@1823:                 l_y3 = x_dataset.getYValue(1, l_subtrahendItem);
sascha@1823:                 l_subtrahendCurX = new Double(l_x3);
sascha@1823:                 l_subtrahendCurY = new Double(l_y3);
sascha@1823: 
sascha@1823:                 if (!b_subtrahendAtIntersect) {
sascha@1823:                     l_subtrahendXs.add(l_subtrahendCurX);
sascha@1823:                     l_subtrahendYs.add(l_subtrahendCurY);
sascha@1823:                 }
sascha@1823: 
sascha@1823:                 l_subtrahendMaxY = Math.max(l_subtrahendMaxY, l_y3);
sascha@1823:                 l_subtrahendMinY = Math.min(l_subtrahendMinY, l_y3);
sascha@1823: 
sascha@1823:                 l_x4 = x_dataset.getXValue(1, l_subtrahendItem + 1);
sascha@1823:                 l_y4 = x_dataset.getYValue(1, l_subtrahendItem + 1);
sascha@1823:                 l_subtrahendNextX = new Double(l_x4);
sascha@1823:                 l_subtrahendNextY = new Double(l_y4);
sascha@1823:             }
sascha@1823: 
sascha@1823:             // deassert b_*FastForward (only matters for 1st time through loop)
sascha@1823:             b_minuendFastForward    = false;
sascha@1823:             b_subtrahendFastForward = false;
sascha@1823: 
sascha@1823:             Double l_intersectX = null;
sascha@1823:             Double l_intersectY = null;
sascha@1823:             boolean b_intersect = false;
sascha@1823: 
sascha@1823:             b_minuendAtIntersect    = false;
sascha@1823:             b_subtrahendAtIntersect = false;
sascha@1823: 
sascha@1823:             // check for intersect
sascha@1823:             if ((l_x2 == l_x4) && (l_y2 == l_y4)) {
sascha@1823:                 // check if line segments are colinear
sascha@1823:                 if ((l_x1 == l_x3) && (l_y1 == l_y3)) {
sascha@1823:                     b_colinear = true;
sascha@1823:                 }
sascha@1823:                 else {
sascha@1823:                     // the intersect is at the next point for both the minuend
sascha@1823:                     // and subtrahend
sascha@1823:                     l_intersectX = new Double(l_x2);
sascha@1823:                     l_intersectY = new Double(l_y2);
sascha@1823: 
sascha@1823:                     b_intersect             = true;
sascha@1823:                     b_minuendAtIntersect    = true;
sascha@1823:                     b_subtrahendAtIntersect = true;
sascha@1823:                  }
sascha@1823:             }
sascha@1823:             else {
sascha@1823:                 // compute common denominator
sascha@1823:                 double l_denominator = ((l_y4 - l_y3) * (l_x2 - l_x1))
sascha@1823:                         - ((l_x4 - l_x3) * (l_y2 - l_y1));
sascha@1823: 
sascha@1823:                 // compute common deltas
sascha@1823:                 double l_deltaY = l_y1 - l_y3;
sascha@1823:                 double l_deltaX = l_x1 - l_x3;
sascha@1823: 
sascha@1823:                 // compute numerators
sascha@1823:                 double l_numeratorA = ((l_x4 - l_x3) * l_deltaY)
sascha@1823:                         - ((l_y4 - l_y3) * l_deltaX);
sascha@1823:                 double l_numeratorB = ((l_x2 - l_x1) * l_deltaY)
sascha@1823:                         - ((l_y2 - l_y1) * l_deltaX);
sascha@1823: 
sascha@1823:                 // check if line segments are colinear
sascha@1823:                 if ((0 == l_numeratorA) && (0 == l_numeratorB)
sascha@1823:                         && (0 == l_denominator)) {
sascha@1823:                     b_colinear = true;
sascha@1823:                 }
sascha@1823:                 else {
sascha@1823:                     // check if previously colinear
sascha@1823:                     if (b_colinear) {
sascha@1823:                         // clear colinear points and flag
sascha@1823:                         l_minuendXs.clear();
sascha@1823:                         l_minuendYs.clear();
sascha@1823:                         l_subtrahendXs.clear();
sascha@1823:                         l_subtrahendYs.clear();
sascha@1823:                         l_polygonXs.clear();
sascha@1823:                         l_polygonYs.clear();
sascha@1823: 
sascha@1823:                         b_colinear = false;
sascha@1823: 
sascha@1823:                         // set new starting point for the polygon
sascha@1823:                         boolean b_useMinuend = ((l_x3 <= l_x1)
sascha@1823:                                 && (l_x1 <= l_x4));
sascha@1823:                         l_polygonXs.add(b_useMinuend ? l_minuendCurX
sascha@1823:                                 : l_subtrahendCurX);
sascha@1823:                         l_polygonYs.add(b_useMinuend ? l_minuendCurY
sascha@1823:                                 : l_subtrahendCurY);
sascha@1823:                     }
sascha@1823: 
sascha@1823:                     // compute slope components
sascha@1823:                     double l_slopeA = l_numeratorA / l_denominator;
sascha@1823:                     double l_slopeB = l_numeratorB / l_denominator;
sascha@1823: 
sascha@1823:                     // check if the line segments intersect
sascha@1823:                     if ((0 < l_slopeA) && (l_slopeA <= 1) && (0 < l_slopeB)
sascha@1823:                             && (l_slopeB <= 1)) {
sascha@1823:                         // compute the point of intersection
sascha@1823:                         double l_xi = l_x1 + (l_slopeA * (l_x2 - l_x1));
sascha@1823:                         double l_yi = l_y1 + (l_slopeA * (l_y2 - l_y1));
sascha@1823: 
sascha@1823:                         l_intersectX            = new Double(l_xi);
sascha@1823:                         l_intersectY            = new Double(l_yi);
sascha@1823:                         b_intersect             = true;
sascha@1823:                         b_minuendAtIntersect    = ((l_xi == l_x2)
sascha@1823:                                 && (l_yi == l_y2));
sascha@1823:                         b_subtrahendAtIntersect = ((l_xi == l_x4)
sascha@1823:                                 && (l_yi == l_y4));
sascha@1823: 
sascha@1823:                         // advance minuend and subtrahend to intesect
sascha@1823:                         l_minuendCurX    = l_intersectX;
sascha@1823:                         l_minuendCurY    = l_intersectY;
sascha@1823:                         l_subtrahendCurX = l_intersectX;
sascha@1823:                         l_subtrahendCurY = l_intersectY;
sascha@1823:                     }
sascha@1823:                 }
sascha@1823:             }
sascha@1823: 
sascha@1823:             if (b_intersect) {
sascha@1823:                 // create the polygon
sascha@1823:                 // add the minuend's points to polygon
sascha@1823:                 l_polygonXs.addAll(l_minuendXs);
sascha@1823:                 l_polygonYs.addAll(l_minuendYs);
sascha@1823: 
sascha@1823:                 // add intersection point to the polygon
sascha@1823:                 l_polygonXs.add(l_intersectX);
sascha@1823:                 l_polygonYs.add(l_intersectY);
sascha@1823: 
sascha@1823:                 // add the subtrahend's points to the polygon in reverse
sascha@1823:                 Collections.reverse(l_subtrahendXs);
sascha@1823:                 Collections.reverse(l_subtrahendYs);
sascha@1823:                 l_polygonXs.addAll(l_subtrahendXs);
sascha@1823:                 l_polygonYs.addAll(l_subtrahendYs);
sascha@1823: 
sascha@1823:                 // create an actual polygon
sascha@1823:                 b_positive = (l_subtrahendMaxY <= l_minuendMaxY)
sascha@1823:                         && (l_subtrahendMinY <= l_minuendMinY);
sascha@1823:                 createPolygon(x_graphics, x_dataArea, x_plot, x_domainAxis,
sascha@1823:                         x_rangeAxis, b_positive, l_polygonXs, l_polygonYs);
sascha@1823: 
sascha@1823:                 // clear the point vectors
sascha@1823:                 l_minuendXs.clear();
sascha@1823:                 l_minuendYs.clear();
sascha@1823:                 l_subtrahendXs.clear();
sascha@1823:                 l_subtrahendYs.clear();
sascha@1823:                 l_polygonXs.clear();
sascha@1823:                 l_polygonYs.clear();
sascha@1823: 
sascha@1823:                 // set the maxY and minY values to intersect y-value
sascha@1823:                 double l_y       = l_intersectY.doubleValue();
sascha@1823:                 l_minuendMaxY    = l_y;
sascha@1823:                 l_subtrahendMaxY = l_y;
sascha@1823:                 l_minuendMinY    = l_y;
sascha@1823:                 l_subtrahendMinY = l_y;
sascha@1823: 
sascha@1823:                 // add interection point to new polygon
sascha@1823:                 l_polygonXs.add(l_intersectX);
sascha@1823:                 l_polygonYs.add(l_intersectY);
sascha@1823:             }
sascha@1823: 
sascha@1823:             // advance the minuend if needed
sascha@1823:             if (l_x2 <= l_x4) {
sascha@1823:                 l_minuendItem++;
sascha@1823:                 b_minuendAdvanced = true;
sascha@1823:             }
sascha@1823:             else {
sascha@1823:                 b_minuendAdvanced = false;
sascha@1823:             }
sascha@1823: 
sascha@1823:             // advance the subtrahend if needed
sascha@1823:             if (l_x4 <= l_x2) {
sascha@1823:                 l_subtrahendItem++;
sascha@1823:                 b_subtrahendAdvanced = true;
sascha@1823:             }
sascha@1823:             else {
sascha@1823:                 b_subtrahendAdvanced = false;
sascha@1823:             }
sascha@1823: 
sascha@1823:             b_minuendDone    = (l_minuendItem == (l_minuendItemCount - 1));
sascha@1823:             b_subtrahendDone = (l_subtrahendItem == (l_subtrahendItemCount
sascha@1823:                     - 1));
sascha@1823:         }
sascha@1823: 
sascha@1823:         // check if the final polygon needs to be clipped
sascha@1823:         if (b_minuendDone && (l_x3 < l_x2) && (l_x2 < l_x4)) {
sascha@1823:             // project onto subtrahend
sascha@1823:             double l_slope    = (l_y4 - l_y3) / (l_x4 - l_x3);
sascha@1823:             l_subtrahendNextX = l_minuendNextX;
sascha@1823:             l_subtrahendNextY = new Double((l_slope * l_x2)
sascha@1823:                     + (l_y3 - (l_slope * l_x3)));
sascha@1823:         }
sascha@1823: 
sascha@1823:         if (b_subtrahendDone && (l_x1 < l_x4) && (l_x4 < l_x2)) {
sascha@1823:             // project onto minuend
sascha@1823:             double l_slope = (l_y2 - l_y1) / (l_x2 - l_x1);
sascha@1823:             l_minuendNextX = l_subtrahendNextX;
sascha@1823:             l_minuendNextY = new Double((l_slope * l_x4)
sascha@1823:                     + (l_y1 - (l_slope * l_x1)));
sascha@1823:         }
sascha@1823: 
sascha@1823:         // consider last point of minuend and subtrahend for determining
sascha@1823:         // positivity
sascha@1823:         l_minuendMaxY    = Math.max(l_minuendMaxY,
sascha@1823:                 l_minuendNextY.doubleValue());
sascha@1823:         l_subtrahendMaxY = Math.max(l_subtrahendMaxY,
sascha@1823:                 l_subtrahendNextY.doubleValue());
sascha@1823:         l_minuendMinY    = Math.min(l_minuendMinY,
sascha@1823:                 l_minuendNextY.doubleValue());
sascha@1823:         l_subtrahendMinY = Math.min(l_subtrahendMinY,
sascha@1823:                 l_subtrahendNextY.doubleValue());
sascha@1823: 
sascha@1823:         // add the last point of the minuned and subtrahend
sascha@1823:         l_minuendXs.add(l_minuendNextX);
sascha@1823:         l_minuendYs.add(l_minuendNextY);
sascha@1823:         l_subtrahendXs.add(l_subtrahendNextX);
sascha@1823:         l_subtrahendYs.add(l_subtrahendNextY);
sascha@1823: 
sascha@1823:         // create the polygon
sascha@1823:         // add the minuend's points to polygon
sascha@1823:         l_polygonXs.addAll(l_minuendXs);
sascha@1823:         l_polygonYs.addAll(l_minuendYs);
sascha@1823: 
sascha@1823:         // add the subtrahend's points to the polygon in reverse
sascha@1823:         Collections.reverse(l_subtrahendXs);
sascha@1823:         Collections.reverse(l_subtrahendYs);
sascha@1823:         l_polygonXs.addAll(l_subtrahendXs);
sascha@1823:         l_polygonYs.addAll(l_subtrahendYs);
sascha@1823: 
sascha@1823:         // create an actual polygon
sascha@1823:         b_positive = (l_subtrahendMaxY <= l_minuendMaxY)
sascha@1823:                 && (l_subtrahendMinY <= l_minuendMinY);
sascha@1823:         createPolygon(x_graphics, x_dataArea, x_plot, x_domainAxis,
sascha@1823:                 x_rangeAxis, b_positive, l_polygonXs, l_polygonYs);
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Draws the visual representation of a single data item, second pass.  In
sascha@1823:      * the second pass, the renderer draws the lines and shapes for the
sascha@1823:      * individual points in the two series.
sascha@1823:      *
sascha@1823:      * @param x_graphics  the graphics device.
sascha@1823:      * @param x_dataArea  the area within which the data is being drawn.
sascha@1823:      * @param x_info  collects information about the drawing.
sascha@1823:      * @param x_plot  the plot (can be used to obtain standard color
sascha@1823:      *         information etc).
sascha@1823:      * @param x_domainAxis  the domain (horizontal) axis.
sascha@1823:      * @param x_rangeAxis  the range (vertical) axis.
sascha@1823:      * @param x_dataset  the dataset.
sascha@1823:      * @param x_series  the series index (zero-based).
sascha@1823:      * @param x_item  the item index (zero-based).
sascha@1823:      * @param x_crosshairState  crosshair information for the plot
sascha@1823:      *                          (<code>null</code> permitted).
sascha@1823:      */
sascha@1823:     protected void drawItemPass1(Graphics2D x_graphics,
sascha@1823:                                  Rectangle2D x_dataArea,
sascha@1823:                                  PlotRenderingInfo x_info,
sascha@1823:                                  XYPlot x_plot,
sascha@1823:                                  ValueAxis x_domainAxis,
sascha@1823:                                  ValueAxis x_rangeAxis,
sascha@1823:                                  XYDataset x_dataset,
sascha@1823:                                  int x_series,
sascha@1823:                                  int x_item,
sascha@1823:                                  CrosshairState x_crosshairState) {
sascha@1823: 
sascha@1823:         Shape l_entityArea = null;
sascha@1823:         EntityCollection l_entities = null;
sascha@1823:         if (null != x_info) {
sascha@1823:             l_entities = x_info.getOwner().getEntityCollection();
sascha@1823:         }
sascha@1823: 
sascha@1823:         Paint l_seriesPaint   = getItemPaint(x_series, x_item);
sascha@1823:         Stroke l_seriesStroke = getItemStroke(x_series, x_item);
sascha@1823:         x_graphics.setPaint(l_seriesPaint);
sascha@1823:         x_graphics.setStroke(l_seriesStroke);
sascha@1823: 
sascha@1823:         PlotOrientation l_orientation      = x_plot.getOrientation();
sascha@1823:         RectangleEdge l_domainAxisLocation = x_plot.getDomainAxisEdge();
sascha@1823:         RectangleEdge l_rangeAxisLocation  = x_plot.getRangeAxisEdge();
sascha@1823: 
sascha@1823:         double l_x0 = x_dataset.getXValue(x_series, x_item);
sascha@1823:         double l_y0 = x_dataset.getYValue(x_series, x_item);
sascha@1823:         double l_x1 = x_domainAxis.valueToJava2D(l_x0, x_dataArea,
sascha@1823:                 l_domainAxisLocation);
sascha@1823:         double l_y1 = x_rangeAxis.valueToJava2D(l_y0, x_dataArea,
sascha@1823:                 l_rangeAxisLocation);
sascha@1823: 
sascha@1823:         if (getShapesVisible()) {
sascha@1823:             Shape l_shape = getItemShape(x_series, x_item);
sascha@1823:             if (l_orientation == PlotOrientation.HORIZONTAL) {
sascha@1823:                 l_shape = ShapeUtilities.createTranslatedShape(l_shape,
sascha@1823:                         l_y1, l_x1);
sascha@1823:             }
sascha@1823:             else {
sascha@1823:                 l_shape = ShapeUtilities.createTranslatedShape(l_shape,
sascha@1823:                         l_x1, l_y1);
sascha@1823:             }
sascha@1823:             if (l_shape.intersects(x_dataArea)) {
sascha@1823:                 x_graphics.setPaint(getItemPaint(x_series, x_item));
sascha@1823:                 x_graphics.fill(l_shape);
sascha@1823:             }
sascha@1823:             l_entityArea = l_shape;
sascha@1823:         }
sascha@1823: 
sascha@1823:         // add an entity for the item...
sascha@1823:         if (null != l_entities) {
sascha@1823:             if (null == l_entityArea) {
sascha@1823:                 l_entityArea = new Rectangle2D.Double((l_x1 - 2), (l_y1 - 2),
sascha@1823:                         4, 4);
sascha@1823:             }
sascha@1823:             String l_tip = null;
sascha@1823:             XYToolTipGenerator l_tipGenerator = getToolTipGenerator(x_series,
sascha@1823:                     x_item);
sascha@1823:             if (null != l_tipGenerator) {
sascha@1823:                 l_tip = l_tipGenerator.generateToolTip(x_dataset, x_series,
sascha@1823:                         x_item);
sascha@1823:             }
sascha@1823:             String l_url = null;
sascha@1823:             XYURLGenerator l_urlGenerator = getURLGenerator();
sascha@1823:             if (null != l_urlGenerator) {
sascha@1823:                 l_url = l_urlGenerator.generateURL(x_dataset, x_series,
sascha@1823:                         x_item);
sascha@1823:             }
sascha@1823:             XYItemEntity l_entity = new XYItemEntity(l_entityArea, x_dataset,
sascha@1823:                     x_series, x_item, l_tip, l_url);
sascha@1823:             l_entities.add(l_entity);
sascha@1823:         }
sascha@1823: 
sascha@1823:         // draw the item label if there is one...
sascha@1823:         if (isItemLabelVisible(x_series, x_item)) {
sascha@1823:             drawItemLabel(x_graphics, l_orientation, x_dataset, x_series,
sascha@1823:                           x_item, l_x1, l_y1, (l_y1 < 0.0));
sascha@1823:         }
sascha@1823: 
sascha@1823:         int l_domainAxisIndex = x_plot.getDomainAxisIndex(x_domainAxis);
sascha@1823:         int l_rangeAxisIndex  = x_plot.getRangeAxisIndex(x_rangeAxis);
sascha@1823:         updateCrosshairValues(x_crosshairState, l_x0, l_y0, l_domainAxisIndex,
sascha@1823:                               l_rangeAxisIndex, l_x1, l_y1, l_orientation);
sascha@1823: 
sascha@1823:         if (0 == x_item) {
sascha@1823:             return;
sascha@1823:         }
sascha@1823: 
sascha@1823:         double l_x2 = x_domainAxis.valueToJava2D(x_dataset.getXValue(x_series,
sascha@1823:                 (x_item - 1)), x_dataArea, l_domainAxisLocation);
sascha@1823:         double l_y2 = x_rangeAxis.valueToJava2D(x_dataset.getYValue(x_series,
sascha@1823:                 (x_item - 1)), x_dataArea, l_rangeAxisLocation);
sascha@1823: 
sascha@1823:         Line2D l_line = null;
sascha@1823:         if (PlotOrientation.HORIZONTAL == l_orientation) {
sascha@1823:             l_line = new Line2D.Double(l_y1, l_x1, l_y2, l_x2);
sascha@1823:         }
sascha@1823:         else if (PlotOrientation.VERTICAL == l_orientation) {
sascha@1823:             l_line = new Line2D.Double(l_x1, l_y1, l_x2, l_y2);
sascha@1823:         }
sascha@1823: 
sascha@1823:         if ((null != l_line) && l_line.intersects(x_dataArea)) {
sascha@1823:             x_graphics.setPaint(getItemPaint(x_series, x_item));
sascha@1823:             x_graphics.setStroke(getItemStroke(x_series, x_item));
sascha@1823:             x_graphics.draw(l_line);
sascha@1823:         }
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Determines if a dataset is degenerate.  A degenerate dataset is a
sascha@1823:      * dataset where either series has less than two (2) points.
sascha@1823:      *
sascha@1823:      * @param x_dataset  the dataset.
sascha@1823:      * @param x_impliedZeroSubtrahend  if false, do not check the subtrahend
sascha@1823:      *
sascha@1823:      * @return true if the dataset is degenerate.
sascha@1823:      */
sascha@1823:     private boolean isEitherSeriesDegenerate(XYDataset x_dataset,
sascha@1823:             boolean x_impliedZeroSubtrahend) {
sascha@1823: 
sascha@1823:         if (x_impliedZeroSubtrahend) {
sascha@1823:             return (x_dataset.getItemCount(0) < 2);
sascha@1823:         }
sascha@1823: 
sascha@1823:         return ((x_dataset.getItemCount(0) < 2)
sascha@1823:                 || (x_dataset.getItemCount(1) < 2));
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Determines if the two (2) series are disjoint.
sascha@1823:      * Disjoint series do not overlap in the domain space.
sascha@1823:      *
sascha@1823:      * @param x_dataset  the dataset.
sascha@1823:      *
sascha@1823:      * @return true if the dataset is degenerate.
sascha@1823:      */
sascha@1823:     private boolean areSeriesDisjoint(XYDataset x_dataset) {
sascha@1823: 
sascha@1823:         int l_minuendItemCount = x_dataset.getItemCount(0);
sascha@1823:         double l_minuendFirst  = x_dataset.getXValue(0, 0);
sascha@1823:         double l_minuendLast   = x_dataset.getXValue(0, l_minuendItemCount - 1);
sascha@1823: 
sascha@1823:         int l_subtrahendItemCount = x_dataset.getItemCount(1);
sascha@1823:         double l_subtrahendFirst  = x_dataset.getXValue(1, 0);
sascha@1823:         double l_subtrahendLast   = x_dataset.getXValue(1,
sascha@1823:                 l_subtrahendItemCount - 1);
sascha@1823: 
sascha@1823:         return ((l_minuendLast < l_subtrahendFirst)
sascha@1823:                 || (l_subtrahendLast < l_minuendFirst));
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Draws the visual representation of a polygon
sascha@1823:      *
sascha@1823:      * @param x_graphics  the graphics device.
sascha@1823:      * @param x_dataArea  the area within which the data is being drawn.
sascha@1823:      * @param x_plot  the plot (can be used to obtain standard color
sascha@1823:      *                information etc).
sascha@1823:      * @param x_domainAxis  the domain (horizontal) axis.
sascha@1823:      * @param x_rangeAxis  the range (vertical) axis.
sascha@1823:      * @param x_positive  indicates if the polygon is positive (true) or
sascha@1823:      *                    negative (false).
sascha@1823:      * @param x_xValues  a linked list of the x values (expects values to be
sascha@1823:      *                   of type Double).
sascha@1823:      * @param x_yValues  a linked list of the y values (expects values to be
sascha@1823:      *                   of type Double).
sascha@1823:      */
sascha@1823:     private void createPolygon (Graphics2D x_graphics,
sascha@1823:                                 Rectangle2D x_dataArea,
sascha@1823:                                 XYPlot x_plot,
sascha@1823:                                 ValueAxis x_domainAxis,
sascha@1823:                                 ValueAxis x_rangeAxis,
sascha@1823:                                 boolean x_positive,
sascha@1823:                                 LinkedList x_xValues,
sascha@1823:                                 LinkedList x_yValues) {
sascha@1823: 
sascha@1823:         PlotOrientation l_orientation      = x_plot.getOrientation();
sascha@1823:         RectangleEdge l_domainAxisLocation = x_plot.getDomainAxisEdge();
sascha@1823:         RectangleEdge l_rangeAxisLocation  = x_plot.getRangeAxisEdge();
sascha@1823: 
sascha@1823:         Object[] l_xValues = x_xValues.toArray();
sascha@1823:         Object[] l_yValues = x_yValues.toArray();
sascha@1823: 
sascha@1823:         GeneralPath l_path = new GeneralPath();
sascha@1823: 
sascha@1823:         if (PlotOrientation.VERTICAL == l_orientation) {
sascha@1823:             double l_x = x_domainAxis.valueToJava2D((
sascha@1823:                     (Double) l_xValues[0]).doubleValue(), x_dataArea,
sascha@1823:                     l_domainAxisLocation);
sascha@1823:             if (this.roundXCoordinates) {
sascha@1823:                 l_x = Math.rint(l_x);
sascha@1823:             }
sascha@1823: 
sascha@1823:             double l_y = x_rangeAxis.valueToJava2D((
sascha@1823:                     (Double) l_yValues[0]).doubleValue(), x_dataArea,
sascha@1823:                     l_rangeAxisLocation);
sascha@1823: 
sascha@1823:             l_path.moveTo((float) l_x, (float) l_y);
sascha@1823:             for (int i = 1; i < l_xValues.length; i++) {
sascha@1823:                 l_x = x_domainAxis.valueToJava2D((
sascha@1823:                         (Double) l_xValues[i]).doubleValue(), x_dataArea,
sascha@1823:                         l_domainAxisLocation);
sascha@1823:                 if (this.roundXCoordinates) {
sascha@1823:                     l_x = Math.rint(l_x);
sascha@1823:                 }
sascha@1823: 
sascha@1823:                 l_y = x_rangeAxis.valueToJava2D((
sascha@1823:                         (Double) l_yValues[i]).doubleValue(), x_dataArea,
sascha@1823:                         l_rangeAxisLocation);
sascha@1823:                 l_path.lineTo((float) l_x, (float) l_y);
sascha@1823:             }
sascha@1823:             l_path.closePath();
sascha@1823:         }
sascha@1823:         else {
sascha@1823:             double l_x = x_domainAxis.valueToJava2D((
sascha@1823:                     (Double) l_xValues[0]).doubleValue(), x_dataArea,
sascha@1823:                     l_domainAxisLocation);
sascha@1823:             if (this.roundXCoordinates) {
sascha@1823:                 l_x = Math.rint(l_x);
sascha@1823:             }
sascha@1823: 
sascha@1823:             double l_y = x_rangeAxis.valueToJava2D((
sascha@1823:                     (Double) l_yValues[0]).doubleValue(), x_dataArea,
sascha@1823:                     l_rangeAxisLocation);
sascha@1823: 
sascha@1823:             l_path.moveTo((float) l_y, (float) l_x);
sascha@1823:             for (int i = 1; i < l_xValues.length; i++) {
sascha@1823:                 l_x = x_domainAxis.valueToJava2D((
sascha@1823:                         (Double) l_xValues[i]).doubleValue(), x_dataArea,
sascha@1823:                         l_domainAxisLocation);
sascha@1823:                 if (this.roundXCoordinates) {
sascha@1823:                     l_x = Math.rint(l_x);
sascha@1823:                 }
sascha@1823: 
sascha@1823:                 l_y = x_rangeAxis.valueToJava2D((
sascha@1823:                         (Double) l_yValues[i]).doubleValue(), x_dataArea,
sascha@1823:                         l_rangeAxisLocation);
sascha@1823:                 l_path.lineTo((float) l_y, (float) l_x);
sascha@1823:             }
sascha@1823:             l_path.closePath();
sascha@1823:         }
sascha@1823: 
sascha@1823:         if (l_path.intersects(x_dataArea)) {
sascha@1823:             x_graphics.setPaint(x_positive ? getPositivePaint()
sascha@1823:                     : getNegativePaint());
sascha@1823:             x_graphics.fill(l_path);
sascha@1823:         }
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns a default legend item for the specified series.  Subclasses
sascha@1823:      * should override this method to generate customised items.
sascha@1823:      *
sascha@1823:      * @param datasetIndex  the dataset index (zero-based).
sascha@1823:      * @param series  the series index (zero-based).
sascha@1823:      *
sascha@1823:      * @return A legend item for the series.
sascha@1823:      */
sascha@1823:     public LegendItem getLegendItem(int datasetIndex, int series) {
sascha@1823:         LegendItem result = null;
sascha@1823:         XYPlot p = getPlot();
sascha@1823:         if (p != null) {
sascha@1823:             XYDataset dataset = p.getDataset(datasetIndex);
sascha@1823:             if (dataset != null) {
sascha@1823:                 if (getItemVisible(series, 0)) {
sascha@1823:                     String label = getLegendItemLabelGenerator().generateLabel(
sascha@1823:                             dataset, series);
sascha@1823:                     String description = label;
sascha@1823:                     String toolTipText = null;
sascha@1823:                     if (getLegendItemToolTipGenerator() != null) {
sascha@1823:                         toolTipText
sascha@1823:                             = getLegendItemToolTipGenerator().generateLabel(
sascha@1823:                                     dataset, series);
sascha@1823:                     }
sascha@1823:                     String urlText = null;
sascha@1823:                     if (getLegendItemURLGenerator() != null) {
sascha@1823:                         urlText = getLegendItemURLGenerator().generateLabel(
sascha@1823:                                 dataset, series);
sascha@1823:                     }
sascha@1823:                     Paint paint = lookupSeriesPaint(series);
sascha@1823:                     Stroke stroke = lookupSeriesStroke(series);
sascha@1823:                     Shape line = getLegendLine();
sascha@1823:                     result = new LegendItem(label, description,
sascha@1823:                             toolTipText, urlText, line, stroke, paint);
sascha@1823:                     result.setLabelFont(lookupLegendTextFont(series));
sascha@1823:                     Paint labelPaint = lookupLegendTextPaint(series);
sascha@1823:                     if (labelPaint != null) {
sascha@1823:                         result.setLabelPaint(labelPaint);
sascha@1823:                     }
sascha@1823:                     result.setDataset(dataset);
sascha@1823:                     result.setDatasetIndex(datasetIndex);
sascha@1823:                     result.setSeriesKey(dataset.getSeriesKey(series));
sascha@1823:                     result.setSeriesIndex(series);
sascha@1823:                 }
sascha@1823:             }
sascha@1823: 
sascha@1823:         }
sascha@1823: 
sascha@1823:         return result;
sascha@1823: 
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Tests this renderer for equality with an arbitrary object.
sascha@1823:      *
sascha@1823:      * @param obj  the object (<code>null</code> permitted).
sascha@1823:      *
sascha@1823:      * @return A boolean.
sascha@1823:      */
sascha@1823:     public boolean equals(Object obj) {
sascha@1823:         if (obj == this) {
sascha@1823:             return true;
sascha@1823:         }
sascha@1823:         if (!(obj instanceof StableXYDifferenceRenderer)) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         if (!super.equals(obj)) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         StableXYDifferenceRenderer that = (StableXYDifferenceRenderer) obj;
sascha@1823:         if (!PaintUtilities.equal(this.positivePaint, that.positivePaint)) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         if (!PaintUtilities.equal(this.negativePaint, that.negativePaint)) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         if (this.shapesVisible != that.shapesVisible) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         if (!ShapeUtilities.equal(this.legendLine, that.legendLine)) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         if (this.roundXCoordinates != that.roundXCoordinates) {
sascha@1823:             return false;
sascha@1823:         }
sascha@1823:         return true;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Returns a clone of the renderer.
sascha@1823:      *
sascha@1823:      * @return A clone.
sascha@1823:      *
sascha@1823:      * @throws CloneNotSupportedException  if the renderer cannot be cloned.
sascha@1823:      */
sascha@1823:     public Object clone() throws CloneNotSupportedException {
sascha@1823:         StableXYDifferenceRenderer clone = (StableXYDifferenceRenderer) super.clone();
sascha@1823:         clone.legendLine = ShapeUtilities.clone(this.legendLine);
sascha@1823:         return clone;
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Provides serialization support.
sascha@1823:      *
sascha@1823:      * @param stream  the output stream.
sascha@1823:      *
sascha@1823:      * @throws IOException  if there is an I/O error.
sascha@1823:      */
sascha@1823:     private void writeObject(ObjectOutputStream stream) throws IOException {
sascha@1823:         stream.defaultWriteObject();
sascha@1823:         SerialUtilities.writePaint(this.positivePaint, stream);
sascha@1823:         SerialUtilities.writePaint(this.negativePaint, stream);
sascha@1823:         SerialUtilities.writeShape(this.legendLine, stream);
sascha@1823:     }
sascha@1823: 
sascha@1823:     /**
sascha@1823:      * Provides serialization support.
sascha@1823:      *
sascha@1823:      * @param stream  the input stream.
sascha@1823:      *
sascha@1823:      * @throws IOException  if there is an I/O error.
sascha@1823:      * @throws ClassNotFoundException  if there is a classpath problem.
sascha@1823:      */
sascha@1823:     private void readObject(ObjectInputStream stream)
sascha@1823:         throws IOException, ClassNotFoundException {
sascha@1823:         stream.defaultReadObject();
sascha@1823:         this.positivePaint = SerialUtilities.readPaint(stream);
sascha@1823:         this.negativePaint = SerialUtilities.readPaint(stream);
sascha@1823:         this.legendLine = SerialUtilities.readShape(stream);
sascha@1823:     }
sascha@1823: }