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);
felix@2021:  */
felix@2021: /*
felix@2021:  * For further changes within the FLYS project, refer to the ChangeLog.
sascha@1823:  */
sascha@1824: package de.intevation.flys.jfree;
sascha@1823: 
felix@2071: import java.awt.BasicStroke;
sascha@1823: import java.awt.Color;
sascha@1823: import java.awt.Graphics2D;
felix@2666: import java.awt.Font;
sascha@1823: import java.awt.Paint;
felix@2667: import java.awt.geom.Point2D;
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@1883: import java.util.ArrayList;
sascha@1823: import java.util.Collections;
sascha@1823: import java.util.LinkedList;
sascha@1883: import java.util.List;
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@1883: import org.jfree.data.xy.DefaultXYDataset;
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@1883: import gnu.trove.TDoubleArrayList;
sascha@1883: 
sascha@1884: import de.intevation.flys.artifacts.math.Linear;
sascha@1884: 
ingo@3785: import java.text.NumberFormat;
ingo@3785: 
sascha@2107: import org.apache.log4j.Logger;
sascha@2107: 
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@2107:     private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class);
sascha@2107: 
sascha@2108:     public static final int CALCULATE_POSITIVE_AREA = 1;
sascha@2108:     public static final int CALCULATE_NEGATIVE_AREA = 2;
sascha@2108:     public static final int CALCULATE_ALL_AREA =
sascha@2108:                             CALCULATE_POSITIVE_AREA | CALCULATE_NEGATIVE_AREA;
sascha@2108: 
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: 
felix@2071:     /** Display shapes at each point? */
felix@2071:     protected boolean drawOutline;
felix@2071: 
felix@2071:     /** Which stroke to draw outline with? */
felix@2071:     protected Stroke outlineStroke;
felix@2071: 
felix@2071:     /** Which paint to draw outline with? */
felix@2071:     protected Paint outlinePaint;
felix@2071: 
sascha@1823:     /** The shape to display in the legend item. */
felix@2021:     private transient Shape legendShape;
sascha@1823: 
felix@2071:     protected boolean drawOriginalSeries;
felix@2071: 
felix@2666:     /** The color of the label showing the calculated area. */
felix@2666:     protected Color labelColor;
felix@2666: 
felix@2667:     /** The background color of the label showing the calculated area. */
felix@2667:     protected Color labelBGColor;
felix@2667: 
felix@2666:     /** Font to draw label of calculated area with. */
felix@2666:     protected Font labelFont;
felix@2666: 
ingo@3785:     /** Template to create i18ned label for area. */
ingo@3785:     protected String areaLabelTamplate;
ingo@3785: 
ingo@3785:     /** NumberFormat to use for area. */
ingo@3785:     protected NumberFormat areaLabelNumberFormat;
ingo@3785: 
sascha@2108:     protected int areaCalculationMode;
sascha@2108: 
sascha@2108:     protected double positiveArea;
sascha@2108:     protected double negativeArea;
sascha@2108: 
felix@2666:     /** Whether or not to draw a label in the area. */
felix@2666:     protected boolean labelArea = true;
felix@2666: 
sascha@1824: 
felix@2667:     /** Arithmetic centroid of drawn polygons. */
felix@2667:     protected Point2D.Double centroid;
felix@2667: 
felix@2667: 
felix@2667:     /** Number of points that contributed to the centroid. */
felix@2667:     protected int centroidNPoints = 0;
felix@2667: 
felix@2667: 
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@1881:         this(Color.green, Color.red, false /*,  null */);
sascha@1823:     }
sascha@1823: 
sascha@2108:     public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
sascha@2108:                                 boolean shapes) {
felix@2666:         this(positivePaint, negativePaint, shapes, CALCULATE_ALL_AREA);
sascha@2108:     }
sascha@2108: 
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@2108:                                 boolean shapes, int areaCalculationMode) {
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;
felix@2071:         this.legendShape   = new Rectangle2D.Double(-3.0, -3.0, 10.0, 10.0);
sascha@1823:         this.roundXCoordinates = false;
felix@2071:         this.drawOutline   = true;
felix@2071:         this.outlineStroke = new BasicStroke(1);
felix@2071:         this.outlinePaint  = Color.black;
felix@2071:         this.drawOriginalSeries = false;
sascha@2108:         this.areaCalculationMode = areaCalculationMode;
felix@2667:         this.labelBGColor  = null;
felix@2667:         this.centroid      = new Point2D.Double(0,0);
sascha@1823:     }
sascha@1823: 
sascha@2108:     public int getAreaCalculationMode() {
sascha@2108:         return areaCalculationMode;
sascha@2108:     }
sascha@2108: 
sascha@2108:     public void setAreaCalculationMode(int areaCalculationMode) {
sascha@2108:         this.areaCalculationMode = areaCalculationMode;
sascha@2108:     }
sascha@2108: 
ingo@3785: 
ingo@3785:     /** Set template to use to create area label (e.g. 'Area=%dm2'). */
ingo@3785:     public void setAreaLabelTemplate(String areaTemplate) {
ingo@3785:         this.areaLabelTamplate = areaTemplate;
ingo@3785:     }
ingo@3785: 
ingo@3785: 
ingo@3785:     public void setAreaLabelNumberFormat(NumberFormat nf) {
ingo@3785:         this.areaLabelNumberFormat = nf;
ingo@3785:     }
ingo@3785: 
ingo@3785: 
felix@2666:     public boolean isLabelArea() {
felix@2666:         return this.labelArea;
felix@2666:     }
felix@2666: 
felix@2666:     public void setLabelArea(boolean label) {
felix@2666:         this.labelArea = label;
felix@2666:     }
felix@2666: 
felix@2666: 
felix@2666:     /** Set font to paint label with. */
felix@2666:     public void setLabelFont(Font font) {
felix@2666:         this.labelFont = font;
felix@2666:     }
felix@2666: 
felix@2666: 
felix@2666:     /** Get font with which label is painted. */
felix@2666:     public Font getLabelFont() {
felix@2666:         return this.labelFont;
felix@2666:     }
felix@2666: 
sascha@3076: 
felix@2666:     /** Set color with which to paint label. */
felix@2666:     public void setLabelColor(Color color) {
felix@2666:         this.labelColor = color;
felix@2666:     }
felix@2666: 
felix@2666: 
felix@2666:     /** Get color with which label is painted. */
felix@2666:     public Color getLabelColor() {
felix@2666:         return this.labelColor;
felix@2666:     }
sascha@2108: 
sascha@2108: 
felix@2667:     /** Set color with which to paint label bg. */
felix@2667:     public void setLabelBGColor(Color color) {
felix@2667:         this.labelBGColor = color;
felix@2667:     }
felix@2667: 
felix@2667: 
felix@2667:     /** Get color with which label is painted. */
felix@2667:     public Color getLabelBGColor() {
felix@2667:         return this.labelBGColor;
felix@2667:     }
felix@2667: 
felix@2667: 
sascha@2108:     public double getCalculatedArea() {
sascha@2108:         return positiveArea + negativeArea;
sascha@2108:     }
felix@2071: 
felix@2071:     /**
felix@2071:      * Sets color that is used if drawOutline is true.
felix@2071:      */
felix@2071:     public void setOutlinePaint(Paint outlinePaint) {
felix@2071:         this.outlinePaint = outlinePaint;
felix@2071:     }
felix@2071: 
felix@2071: 
felix@2071:     /**
felix@2071:      * Gets color which is used if drawOutline is true.
felix@2071:      */
felix@2071:     public Paint getOutlinePaint() {
felix@2071:         return this.outlinePaint;
felix@2071:     }
felix@2071: 
felix@2071: 
felix@2071:     /**
felix@2071:      * Sets Stroke that is used if drawOutline is true.
felix@2071:      */
felix@2071:     public void setOutlineStroke(Stroke stroke) {
felix@2071:         this.outlineStroke = stroke;
felix@2071:     }
felix@2071: 
felix@2071: 
felix@2071:     /**
felix@2071:      * Returns Stroke that is used if drawOutline is true.
felix@2071:      */
felix@2071:     public Stroke getOutlineStroke() {
felix@2071:         return this.outlineStroke;
felix@2071:     }
felix@2071: 
felix@2071: 
felix@2071:     /**
felix@2071:      * Whether or not to draw the 'Shape' of the area (in contrast to
felix@2071:      * shapes at data items).
felix@2071:      */
felix@2071:     public void setDrawOutline(boolean doDrawOutline) {
felix@2071:         this.drawOutline = doDrawOutline;
felix@2071:     }
felix@2071: 
felix@2071: 
felix@2071:     /**
felix@2071:      * Returns whether or not to draw the shape of the outline.
felix@2071:      */
felix@2071:     public boolean getDrawOutline() {
felix@2071:         return this.drawOutline;
felix@2071:     }
felix@2071: 
felix@2071: 
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() {
felix@2021:         return this.legendShape;
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:         }
felix@2021:         this.legendShape = 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.
felix@3395:      * @param dataArea  the (visible) 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:      * 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: 
felix@3395: 
felix@3395:     /**
felix@3395:      * Adds x/y data to series.
felix@3395:      */
sascha@1884:     private static final void addSeries(
sascha@1884:         DefaultXYDataset ds,
sascha@1884:         Comparable       key,
sascha@1884:         TDoubleArrayList xs,
sascha@1884:         TDoubleArrayList ys
sascha@1884:     ) {
sascha@1884:         ds.addSeries(
sascha@1884:             key,
sascha@1884:             new double [][] {
sascha@1884:                 xs.toNativeArray(),
sascha@1884:                 ys.toNativeArray()
sascha@1884:             });
sascha@1884:     }
sascha@1884: 
sascha@1884:     protected static List<XYDataset> splitByNaNsOneSeries(
sascha@1884:         XYDataset dataset
sascha@1884:     ) {
sascha@1883:         List<XYDataset> datasets = new ArrayList<XYDataset>();
sascha@1883: 
sascha@1884:         int N = dataset.getItemCount(0);
sascha@1884:         TDoubleArrayList xs = new TDoubleArrayList(N);
sascha@1884:         TDoubleArrayList ys = new TDoubleArrayList(N);
sascha@1884:         for (int i = 0; i < N; ++i) {
sascha@1884:             double x = dataset.getXValue(0, i);
sascha@1884:             double y = dataset.getYValue(0, i);
sascha@1884:             if (Double.isNaN(x) || Double.isNaN(y)) {
sascha@1883:                 if (!xs.isEmpty()) {
sascha@1883:                     DefaultXYDataset ds = new DefaultXYDataset();
sascha@1884:                     addSeries(ds, dataset.getSeriesKey(0), xs, ys);
sascha@1883:                     datasets.add(ds);
sascha@1885:                     xs.resetQuick();
sascha@1885:                     ys.resetQuick();
sascha@1883:                 }
sascha@1884:             }
sascha@1884:             else {
sascha@1884:                 xs.add(x);
sascha@1884:                 ys.add(y);
sascha@1884:             }
sascha@1884:         }
sascha@1884:         if (!xs.isEmpty()) {
sascha@1884:             DefaultXYDataset ds = new DefaultXYDataset();
sascha@1884:             addSeries(ds, dataset.getSeriesKey(0), xs, ys);
sascha@1884:             datasets.add(ds);
sascha@1883:         }
sascha@1883: 
sascha@1883:         return datasets;
sascha@1883:     }
sascha@1883: 
sascha@1884:     private static final boolean add(TDoubleArrayList xs, double x) {
sascha@1884:         int N = xs.size();
sascha@1884:         if (N == 0 || xs.getQuick(N-1) < x) {
sascha@1884:             xs.add(x);
sascha@1884:             return true;
sascha@1884:         }
sascha@2107:         log.debug("pushed smaller");
sascha@1884:         return false;
sascha@1884:     }
sascha@1884: 
sascha@1884:     protected static List<XYDataset> splitByNaNsTwoSeries(
sascha@1884:         XYDataset dataset
sascha@1884:     ) {
sascha@2107:         boolean debug = log.isDebugEnabled();
sascha@2107: 
sascha@1884:         List<XYDataset> datasets = new ArrayList<XYDataset>();
sascha@1884: 
sascha@1884:         int N = dataset.getItemCount(0);
sascha@1884:         int M = dataset.getItemCount(1);
sascha@1884: 
sascha@1884:         int i = 0, j = 0;
sascha@1884:         // ignore leading NaNs
sascha@1884:         for (; i < N; ++i) {
sascha@1884:             double x = dataset.getXValue(0, i);
sascha@1884:             double y = dataset.getYValue(0, i);
sascha@1884:             if (!Double.isNaN(x) && !Double.isNaN(y)) {
sascha@1884:                 break;
sascha@1884:             }
sascha@1884:         }
sascha@1884: 
sascha@1884:         for (; j < M; ++j) {
sascha@1884:             double x = dataset.getXValue(1, j);
sascha@1884:             double y = dataset.getYValue(1, j);
sascha@1884:             if (!Double.isNaN(x) && !Double.isNaN(y)) {
sascha@1884:                 break;
sascha@1884:             }
sascha@1884:         }
sascha@1884: 
sascha@1884:         TDoubleArrayList six = new TDoubleArrayList();
sascha@1884:         TDoubleArrayList siy = new TDoubleArrayList();
sascha@1884:         TDoubleArrayList sjx = new TDoubleArrayList();
sascha@1884:         TDoubleArrayList sjy = new TDoubleArrayList();
sascha@1884: 
sascha@1884:         while (i < N && j < M) {
sascha@1884:             int ni = i+1;
sascha@1884:             for (; ni < N && !Double.isNaN(dataset.getXValue(0, ni)); ++ni);
sascha@1884:             for (; ni < N &&  Double.isNaN(dataset.getXValue(0, ni)); ++ni);
sascha@1884: 
sascha@1884:             int nj = j+1;
sascha@1884:             for (; nj < M && !Double.isNaN(dataset.getXValue(1, nj)); ++nj);
sascha@1884:             for (; nj < M &&  Double.isNaN(dataset.getXValue(1, nj)); ++nj);
sascha@1884: 
sascha@1884:             if (ni == N && nj == M) { // no more splits
sascha@2107:                 log.debug("no more splits ....");
sascha@1884:                 for (; i < ni; ++i) {
sascha@1884:                     double x = dataset.getXValue(0, i);
sascha@1884:                     double y = dataset.getYValue(0, i);
sascha@3076:                     if (!Double.isNaN(x)
sascha@1884:                     &&  !Double.isNaN(y)
sascha@1884:                     &&  add(six, x)) {
sascha@1884:                         siy.add(y);
sascha@1884:                     }
sascha@1884:                 }
sascha@1884:                 for (; j < nj; ++j) {
sascha@1884:                     double x = dataset.getXValue(1, j);
sascha@1884:                     double y = dataset.getYValue(1, j);
sascha@1884:                     if (!Double.isNaN(x)
sascha@1884:                     &&  !Double.isNaN(y)
sascha@1884:                     &&  add(sjx, x)) {
sascha@1884:                         sjy.add(y);
sascha@1884:                     }
sascha@1884:                 }
sascha@1884:                 if (!six.isEmpty() && !sjx.isEmpty()) {
sascha@1884:                     DefaultXYDataset ds = new DefaultXYDataset();
sascha@1884:                     addSeries(ds, dataset.getSeriesKey(0), six, siy);
sascha@1884:                     addSeries(ds, dataset.getSeriesKey(1), sjx, sjy);
sascha@1884:                     datasets.add(ds);
sascha@1884:                 }
sascha@1884:                 break;
sascha@1884:             }
sascha@1884: 
sascha@2107:             if (debug) {
sascha@2107:                 log.debug("ni: " + ni + " " + N);
sascha@2107:                 log.debug("nj: " + nj + " " + M);
sascha@2107:             }
sascha@1884: 
sascha@1884:             double xni = ni < N
sascha@1884:                 ? dataset.getXValue(0, ni)
sascha@1884:                 : Double.MAX_VALUE;
sascha@1884: 
sascha@1884:             double xnj = nj < M
sascha@1884:                 ? dataset.getXValue(1, nj)
sascha@1884:                 : Double.MAX_VALUE;
sascha@1884: 
sascha@1884:             double xns = Math.min(xni, xnj);
sascha@1884: 
sascha@1884:             double pushxi = Double.NaN;
sascha@1884:             double pushyi = Double.NaN;
sascha@1884:             double pushxj = Double.NaN;
sascha@1884:             double pushyj = Double.NaN;
sascha@1884: 
sascha@1884:             for (; i < ni; ++i) {
sascha@1884:                 double x = dataset.getXValue(0, i);
sascha@1884:                 double y = dataset.getYValue(0, i);
sascha@1884:                 if (Double.isNaN(x) || Double.isNaN(y)) {
sascha@1884:                     continue;
sascha@1884:                 }
sascha@1884:                 if (x < xns) {
sascha@1884:                     if (add(six, x)) {
sascha@1884:                         siy.add(y);
sascha@1884:                     }
sascha@1884:                     continue;
sascha@1884:                 }
sascha@1884:                 if (x == xns) { // exact match
sascha@1884:                     if (add(six, x)) {
sascha@1884:                         siy.add(y);
sascha@1884:                     }
sascha@1884:                     pushxi = x; pushyi = y;
sascha@1884:                 }
sascha@1884:                 else { // x > xns: intersection
sascha@2107:                     if (debug) {
sascha@2107:                         log.debug("xns: " + xns);
sascha@2107:                         log.debug("x/y: " + x + " / " + y);
sascha@2107:                     }
sascha@1884:                     int SIX = six.size();
sascha@1884:                     if (SIX > 0) { // should always be true
sascha@1884:                         double yns = Linear.linear(
sascha@1884:                             xns,
sascha@3076:                             six.getQuick(SIX-1), x,
sascha@1884:                             siy.getQuick(SIX-1), y);
sascha@2107:                         if (debug) {
sascha@2107:                             log.debug("intersection at: " + yns);
sascha@2107:                         }
sascha@1884:                         if (add(six, xns)) {
sascha@1884:                             siy.add(yns);
sascha@1884:                         }
sascha@1884:                         pushxi = xns;
sascha@1884:                         pushyi = yns;
sascha@1884:                     }
sascha@1884:                 }
sascha@1884:                 break; // Split point reached.
sascha@1884:             }
sascha@1884: 
sascha@1884:             for (; j < nj; ++j) {
sascha@1884:                 double x = dataset.getXValue(1, j);
sascha@1884:                 double y = dataset.getYValue(1, j);
sascha@1884:                 if (Double.isNaN(x) || Double.isNaN(y)) {
sascha@1884:                     continue;
sascha@1884:                 }
sascha@1884:                 if (x < xns) {
sascha@1884:                     if (add(sjx, x)) {
sascha@1884:                         sjy.add(y);
sascha@1884:                     }
sascha@1884:                     continue;
sascha@1884:                 }
sascha@1884:                 if (x == xns) { // exact match
sascha@1884:                     if (add(sjx, x)) {
sascha@1884:                         sjy.add(y);
sascha@1884:                     }
sascha@1884:                     pushxj = x; pushyj = y;
sascha@1884:                 }
sascha@1884:                 else { // x > xns: intersection
sascha@1884:                     int SJX = sjx.size();
sascha@1884:                     if (SJX > 0) { // should always be true
sascha@1884:                         double yns = Linear.linear(
sascha@1884:                             xns,
sascha@1884:                             sjx.getQuick(SJX-1), x,
sascha@1884:                             sjy.getQuick(SJX-1), y);
sascha@2107:                         if (debug) {
sascha@2107:                             log.debug("intersection at: " + yns);
sascha@2107:                         }
sascha@1884:                         if (add(sjx, xns)) {
sascha@1884:                             sjy.add(yns);
sascha@1884:                         }
sascha@1884:                         pushxj = xns; pushyj = yns;
sascha@1884:                     }
sascha@1884:                 }
sascha@1884:                 break; // Split point reached.
sascha@1884:             }
sascha@1884: 
sascha@1884:             if (!six.isEmpty() && !sjx.isEmpty()) {
sascha@1884:                 DefaultXYDataset ds = new DefaultXYDataset();
sascha@1884:                 addSeries(ds, dataset.getSeriesKey(0), six, siy);
sascha@1884:                 addSeries(ds, dataset.getSeriesKey(1), sjx, sjy);
sascha@1884:                 datasets.add(ds);
sascha@1884:             }
sascha@1884: 
sascha@1885:             six.resetQuick(); siy.resetQuick();
sascha@1885:             sjx.resetQuick(); sjy.resetQuick();
sascha@1884: 
sascha@1884:             // Push split points.
sascha@1884:             if (!Double.isNaN(pushxi)) {
sascha@1884:                 six.add(pushxi);
sascha@1884:                 siy.add(pushyi);
sascha@1884:             }
sascha@1884: 
sascha@1884:             if (!Double.isNaN(pushxj)) {
sascha@1884:                 sjx.add(pushxj);
sascha@1884:                 sjy.add(pushyj);
sascha@1884:             }
sascha@1884:         }
sascha@1884: 
sascha@1884:         // Copy the rest.
sascha@1884:         for (; i < N; ++i) {
sascha@1884:             double x = dataset.getXValue(0, i);
sascha@1884:             double y = dataset.getXValue(0, i);
sascha@3076:             if (!Double.isNaN(x)
sascha@1884:             &&  !Double.isNaN(y)
sascha@1884:             &&  add(six, x)) {
sascha@1884:                 siy.add(y);
sascha@1884:             }
sascha@1884:         }
sascha@1884: 
sascha@1884:         for (; j < M; ++j) {
sascha@1884:             double x = dataset.getXValue(1, j);
sascha@1884:             double y = dataset.getXValue(1, j);
sascha@1884:             if (!Double.isNaN(x)
sascha@1884:             &&  !Double.isNaN(y)
sascha@1884:             &&  add(sjx, x)) {
sascha@1884:                 sjy.add(y);
sascha@1884:             }
sascha@1884:         }
sascha@1884: 
sascha@1884:         // Build final dataset.
sascha@1884:         if (!six.isEmpty() && !sjx.isEmpty()) {
sascha@1884:             DefaultXYDataset ds = new DefaultXYDataset();
sascha@1884:             addSeries(ds, dataset.getSeriesKey(0), six, siy);
sascha@1884:             addSeries(ds, dataset.getSeriesKey(1), sjx, sjy);
sascha@1884:             datasets.add(ds);
sascha@1884:         }
sascha@1884: 
sascha@2107:         if (debug) {
sascha@2107:             log.debug("datasets after split: " + datasets.size());
sascha@2107:         }
sascha@1884: 
sascha@1884:         return datasets;
sascha@1884:     }
sascha@1884: 
sascha@1884:     public static List<XYDataset> splitByNaNs(XYDataset dataset)  {
sascha@1884: 
sascha@1884:         switch (dataset.getSeriesCount()) {
sascha@3076:             case 0:
ingo@3785:                 return Collections.<XYDataset>emptyList();
sascha@1884:             case 1:
sascha@1884:                 return splitByNaNsOneSeries(dataset);
sascha@1884:             default: // two or more
sascha@1884:                 return splitByNaNsTwoSeries(dataset);
sascha@1884:         }
sascha@1884:     }
sascha@1884: 
felix@3395: 
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@1881:         switch (pass) {
sascha@1881:             case 0:
sascha@1883:                 for (XYDataset ds: splitByNaNs(dataset)) {
sascha@3076:                     drawItemPass0(g2, dataArea, info,
sascha@1883:                         plot, domainAxis, rangeAxis,
sascha@1884:                         ds, series, item, crosshairState);
sascha@1883:                 }
sascha@1881:                 break;
sascha@1881:             case 1:
sascha@1881:                 drawItemPass1(g2, dataArea, info,
sascha@1881:                     plot, domainAxis, rangeAxis,
sascha@1881:                     dataset, series, item, crosshairState);
sascha@1823:         }
felix@2666: 
felix@2666:         // Find geometric middle, calculate area and paint a string with it here.
ingo@3785:         if (pass == 1 && this.labelArea && areaLabelNumberFormat != null && areaLabelTamplate != null) {
felix@2667:             double center_x = centroid.getX();
felix@2667:             double center_y = centroid.getY();
felix@2667:             center_x = domainAxis.valueToJava2D(center_x, dataArea,
felix@2667:                 plot.getDomainAxisEdge());
felix@2667:             center_y = rangeAxis.valueToJava2D(center_y, dataArea,
felix@2667:                 plot.getRangeAxisEdge());
felix@2667: 
felix@2667:             // Respect text-extend if text should appear really centered.
felix@2667: 
felix@2666:             float area = 0f;
felix@2666:             if (areaCalculationMode == CALCULATE_POSITIVE_AREA
felix@2666:                || areaCalculationMode == CALCULATE_ALL_AREA) {
felix@2666:                 area += Math.abs(positiveArea);
felix@2666:             }
felix@2666:             if (areaCalculationMode == CALCULATE_NEGATIVE_AREA
felix@2666:                || areaCalculationMode == CALCULATE_ALL_AREA) {
felix@2666:                 area += Math.abs(negativeArea);
felix@2666:             }
felix@2666:             if (area != 0f) {
felix@2666:                 Color oldColor = g2.getColor();
felix@2666:                 Font oldFont = g2.getFont();
felix@2666:                 g2.setFont(labelFont);
ingo@3785:                 String labelText = String.format(this.areaLabelTamplate,
ingo@3785:                     areaLabelNumberFormat.format(area));
felix@2667:                 if (labelBGColor != null) {
felix@2667:                     EnhancedLineAndShapeRenderer.drawTextBox(g2, labelText,
felix@2667:                         (float)center_x, (float)center_y, labelBGColor);
felix@2667:                 }
felix@2666:                 g2.setColor(labelColor);
felix@2667:                 g2.drawString(labelText, (float)center_x, (float)center_y);
felix@2666:                 g2.setFont(oldFont);
felix@2666:                 g2.setColor(oldColor);
felix@2666:             }
felix@2666:         }
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: 
felix@2071:         // These are the shapes of the series items.
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);
felix@2071:                 /* TODO We could draw the shapes of single items here.
felix@2071:                 if (drawOutline) {
felix@2071:                     x_graphics.setPaint(this.outlinePaint);
felix@2071:                     x_graphics.setStroke(this.outlineStroke);
felix@2071:                     x_graphics.draw(l_shape);
felix@2071:                 }
felix@2071:                 */
sascha@1823:             }
sascha@1823:             l_entityArea = l_shape;
felix@2071:         } // if (getShapesVisible())
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));
felix@2071:             if (drawOriginalSeries) {
felix@2071:                 x_graphics.setPaint(this.outlinePaint);
felix@2071:                 x_graphics.setStroke(this.outlineStroke);
felix@2071:                 x_graphics.draw(l_line);
felix@2071:             }
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: 
felix@2666: 
felix@2667:     public void updateCentroid(Object [] xValues, Object [] yValues) {
felix@2667:         double x = 0d, y = 0d;
felix@2667: 
felix@2667:         for (int i = 0, N = xValues.length; i < N; ++i) {
felix@2667:             x += ((Double)xValues[i]).doubleValue();
felix@2667:             y += ((Double)yValues[i]).doubleValue();
felix@2667:         }
felix@2667: 
felix@2667:         x /= xValues.length;
felix@2667:         y /= yValues.length;
felix@2667: 
felix@2667:         centroidNPoints++;
felix@2667:         double factorNew = 1d / centroidNPoints;
felix@2667:         double factorOld = 1d - factorNew;
felix@2667: 
felix@2667:         centroid = new Point2D.Double((factorNew * x + factorOld * centroid.x),
felix@2667:             (factorNew * y + factorOld * centroid.y));
felix@2667:     }
felix@2667: 
felix@2667: 
sascha@2108:     public static double calculateArea(Object [] xValues, Object [] yValues) {
sascha@2108:         double area = 0d;
sascha@2108: 
sascha@2108:         for (int i = 0, N = xValues.length; i < N; ++i) {
felix@4704:             int k = (i + 1) % N;
sascha@2108:             double xi = ((Double)xValues[i]).doubleValue();
sascha@2108:             double yi = ((Double)yValues[i]).doubleValue();
felix@4704:             double xk = ((Double)xValues[k]).doubleValue();
felix@4704:             double yk = ((Double)yValues[k]).doubleValue();
sascha@2108: 
felix@4704:             area += xi*yk;
felix@4704:             area -= xk*yi;
felix@2667:             // TODO centroid calculation here?
sascha@2108:         }
sascha@2108: 
sascha@2108:         return 0.5d*area;
sascha@2108:     }
sascha@2108: 
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,
felix@2071:                                 XYPlot      x_plot,
felix@2071:                                 ValueAxis   x_domainAxis,
felix@2071:                                 ValueAxis   x_rangeAxis,
felix@2071:                                 boolean     x_positive,
felix@2071:                                 LinkedList  x_xValues,
felix@2071:                                 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: 
felix@3240:         double area = calculateArea(l_xValues, l_yValues)/2d;
felix@2666:         if (x_positive) positiveArea += area;
felix@2666:         else            negativeArea += area;
felix@2667:         updateCentroid(l_xValues, l_yValues);
sascha@2108: 
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);
felix@2071:             if (drawOutline) {
felix@2071:                 x_graphics.setStroke(this.outlineStroke);
felix@2071:                 x_graphics.setPaint(this.outlinePaint);
felix@2071:                 x_graphics.draw(l_path);
felix@2071:             }
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:                     }
felix@2021:                     // Individualized Paints:
felix@2021:                     //Paint paint = lookupSeriesPaint(series);
felix@2021: 
felix@2021:                     // "Area-Style"- Paint.
felix@2021:                     Paint paint = getPositivePaint();
sascha@1823:                     Stroke stroke = lookupSeriesStroke(series);
sascha@1823:                     Shape line = getLegendLine();
felix@2021:                     // Not-filled Shape:
felix@2021:                     //result = new LegendItem(label, description,
felix@2021:                     //        toolTipText, urlText, line, stroke, paint);
felix@2021: 
felix@2071:                     if (drawOutline) {
felix@2071:                         // TODO Include outline style in legenditem (there is a constructor for that)
felix@2071:                     }
felix@2071: 
felix@2021:                     // Filled Shape ("Area-Style").
sascha@1823:                     result = new LegendItem(label, description,
felix@2021:                             toolTipText, urlText, line, 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:      * 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:         }
felix@2021:         if (!ShapeUtilities.equal(this.legendShape, that.legendShape)) {
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();
felix@2021:         clone.legendShape = ShapeUtilities.clone(this.legendShape);
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);
felix@2021:         SerialUtilities.writeShape(this.legendShape, 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);
felix@2021:         this.legendShape = SerialUtilities.readShape(stream);
sascha@1823:     }
sascha@1823: }
felix@2071: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :