changeset 1824:982956bde69e

Added a dataset to zero mapper to circumvent shortcommings in XYDifferenceRenderer. flys-artifacts/trunk@3153 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 03 Nov 2011 12:28:38 +0000
parents 7a11b37d8594
children 02cd002205a3
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java flys-artifacts/src/main/java/de/intevation/flys/jfree/XYDatasetToZeroMapper.java
diffstat 3 files changed, 185 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Nov 03 10:32:17 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Nov 03 12:28:38 2011 +0000
@@ -1,3 +1,13 @@
+2011-11-03  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/jfree/XYDatasetToZeroMapper.java:
+	  New. Maps series to zero to be compatible with XYDifferenceRenderer.
+	  It returns an iterator over XYDatasets to enable splitting by NaNs,
+	  which still needs to be implemented.
+
+	* src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java:
+	  Uses a XYDatasetToZeroMapper now. Fixed package name.
+
 2011-11-03  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	Added new matching options for theme-mappings to allow more
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Thu Nov 03 10:32:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Thu Nov 03 12:28:38 2011 +0000
@@ -73,8 +73,7 @@
  * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG);
  *
  */
-
-package org.jfree.chart.renderer.xy;
+package de.intevation.flys.jfree;
 
 import java.awt.Color;
 import java.awt.Graphics2D;
@@ -89,6 +88,7 @@
 import java.io.ObjectOutputStream;
 import java.util.Collections;
 import java.util.LinkedList;
+import java.util.Iterator;
 
 import org.jfree.chart.LegendItem;
 import org.jfree.chart.axis.ValueAxis;
@@ -108,6 +108,10 @@
 import org.jfree.util.PublicCloneable;
 import org.jfree.util.ShapeUtilities;
 
+import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
+import org.jfree.chart.renderer.xy.XYItemRendererState;
+
 /**
  * A renderer for an {@link XYPlot} that highlights the differences between two
  * series.  The example shown here is generated by the
@@ -135,6 +139,8 @@
     /** The shape to display in the legend item. */
     private transient Shape legendLine;
 
+    private XYDatasetToZeroMapper mapper;
+
     /**
      * This flag controls whether or not the x-coordinates (in Java2D space)
      * are rounded to integers.  When set to true, this can avoid the vertical
@@ -150,7 +156,7 @@
      * Creates a new renderer with default attributes.
      */
     public StableXYDifferenceRenderer() {
-        this(Color.green, Color.red, false);
+        this(Color.green, Color.red, false, null);
     }
 
     /**
@@ -163,7 +169,8 @@
      * @param shapes  draw shapes?
      */
     public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
-                                boolean shapes) {
+                                boolean shapes,
+                                XYDatasetToZeroMapper mapper) {
         if (positivePaint == null) {
             throw new IllegalArgumentException(
                     "Null 'positivePaint' argument.");
@@ -177,6 +184,7 @@
         this.shapesVisible = shapes;
         this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);
         this.roundXCoordinates = false;
+        this.mapper = mapper;
     }
 
     /**
@@ -385,13 +393,35 @@
                          CrosshairState crosshairState,
                          int pass) {
 
-        if (pass == 0) {
-            drawItemPass0(g2, dataArea, info, plot, domainAxis, rangeAxis,
-                    dataset, series, item, crosshairState);
+        if (mapper == null) {
+            switch (pass) {
+                case 0:
+                    drawItemPass0(g2, dataArea, info, 
+                        plot, domainAxis, rangeAxis,
+                        dataset, series, item, crosshairState);
+                    break;
+                case 1:
+                    drawItemPass1(g2, dataArea, info,
+                        plot, domainAxis, rangeAxis,
+                        dataset, series, item, crosshairState);
+            }
         }
-        else if (pass == 1) {
-            drawItemPass1(g2, dataArea, info, plot, domainAxis, rangeAxis,
-                    dataset, series, item, crosshairState);
+        else {
+            for (Iterator<XYDataset> iter = mapper.iterator(dataset);
+                iter.hasNext();
+            ) {
+                switch (pass) {
+                    case 0:
+                        drawItemPass0(g2, dataArea, info, 
+                            plot, domainAxis, rangeAxis,
+                            dataset, series, item, crosshairState);
+                        break;
+                    case 1:
+                        drawItemPass1(g2, dataArea, info,
+                            plot, domainAxis, rangeAxis,
+                            dataset, series, item, crosshairState);
+                }
+            } // for all segments
         }
 
     }
@@ -1239,4 +1269,3 @@
         this.legendLine = SerialUtilities.readShape(stream);
     }
 }
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/XYDatasetToZeroMapper.java	Thu Nov 03 12:28:38 2011 +0000
@@ -0,0 +1,135 @@
+package de.intevation.flys.jfree;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.jfree.data.xy.XYDataset;
+
+import org.jfree.data.general.DatasetChangeListener;
+import org.jfree.data.general.DatasetGroup;
+
+import org.jfree.data.DomainOrder;
+
+import java.io.Serializable;
+
+public class XYDatasetToZeroMapper
+implements   Serializable
+{
+    protected int seriesA;
+    protected int seriesB;
+
+    public class MappedXYDataset implements XYDataset, Serializable {
+
+        protected XYDataset parent;
+
+        public MappedXYDataset() {
+        }
+
+        public MappedXYDataset(XYDataset parent) {
+            this.parent = parent;
+        }
+
+        protected int remap(int series) {
+            switch (series) {
+                case 0: return seriesA;
+                case 1: return seriesB;
+            }
+            return series;
+        }
+
+        @Override
+        public int getSeriesCount() {
+            return seriesA != seriesB ? 2 : 1;
+        }
+
+        @Override
+        public Comparable getSeriesKey(int series) {
+            return parent.getSeriesKey(remap(series));
+        }
+
+        @Override
+        public int indexOf(Comparable seriesKey) {
+            Comparable cmpA = parent.getSeriesKey(seriesA);
+            if (cmpA == seriesKey
+            || (cmpA != null && cmpA.equals(seriesKey))) {
+                return 0;
+            }
+            Comparable cmpB = parent.getSeriesKey(seriesB);
+            if (cmpB == seriesKey
+            || (cmpB != null && cmpB.equals(seriesKey))) {
+                return 1;
+            }
+            return -1;
+        }
+
+        @Override
+        public void addChangeListener(DatasetChangeListener listener) {
+            parent.addChangeListener(listener);
+        }
+
+        @Override
+        public void removeChangeListener(DatasetChangeListener listener) {
+            parent.removeChangeListener(listener);
+        }
+
+        @Override
+        public DatasetGroup getGroup() {
+            return parent.getGroup();
+        }
+
+        @Override
+        public void setGroup(DatasetGroup group) {
+            parent.setGroup(group);
+        }
+
+        @Override
+        public DomainOrder getDomainOrder() {
+            return parent.getDomainOrder();
+        }
+
+        @Override
+        public int getItemCount(int series) {
+            return parent.getItemCount(remap(series));
+        }
+
+        @Override
+        public Number getX(int series, int item) {
+            return parent.getX(remap(series), item);
+        }
+
+        @Override
+        public double getXValue(int series, int item) {
+            return parent.getXValue(remap(series), item);
+        }
+
+        @Override
+        public Number getY(int series, int item) {
+            return parent.getY(remap(series), item);
+        }
+
+        @Override
+        public double getYValue(int series, int item) {
+            return parent.getYValue(remap(series), item);
+        }
+    } // class MappedXYDataset
+
+    public XYDatasetToZeroMapper() {
+    }
+
+    public XYDatasetToZeroMapper(int seriesA) {
+        this(seriesA, seriesA);
+    }
+
+    public XYDatasetToZeroMapper(int seriesA, int seriesB) {
+        this.seriesA = seriesA;
+        this.seriesB = seriesB;
+    }
+
+    public Iterator<XYDataset> iterator(XYDataset dataset) {
+        List<XYDataset> list = new ArrayList<XYDataset>(1);
+        list.add(new MappedXYDataset(dataset));
+        return list.iterator();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org