diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 1684:bdb05dc9b763

Bugfix: #353 Enabled chart's to be drawn with proper axes set even if no data is contained. flys-artifacts/trunk@2902 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 07 Oct 2011 10:51:09 +0000
parents 69929c471646
children 022f62c75878
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Oct 07 09:40:15 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Oct 07 10:51:09 2011 +0000
@@ -9,7 +9,9 @@
 import java.text.NumberFormat;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.w3c.dom.Document;
 
@@ -27,7 +29,6 @@
 import org.jfree.chart.renderer.xy.XYItemRenderer;
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 import org.jfree.data.Range;
-import org.jfree.data.xy.XYDataset;
 import org.jfree.data.xy.XYSeries;
 import org.jfree.data.xy.XYSeriesCollection;
 
@@ -58,6 +59,12 @@
     /** List of annotations to insert in plot. */
     protected List<FLYSAnnotation> annotations;
 
+    /** The max X range that includes all X values of all series for each axis.*/
+    protected Map<Integer, Range> xRanges;
+
+    /** The max Y range that includes all Y values of all series for each axis.*/
+    protected Map<Integer, Range> yRanges;
+
 
     public static final Color DEFAULT_GRID_COLOR      = Color.GRAY;
     public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;
@@ -149,29 +156,89 @@
     }
 
 
-    public void addFirstAxisSeries(XYSeries series) {
+    public void addFirstAxisSeries(XYSeries series, boolean visible) {
         if (first == null) {
             first = new XYSeriesCollection();
         }
 
         if (series != null) {
-            first.addSeries(series);
+            if (visible) {
+                first.addSeries(series);
+            }
+
+            combineYRanges(new Range(series.getMinY(), series.getMaxY()), 0);
+            combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
         }
     }
 
 
-    public void addSecondAxisSeries(XYSeries series) {
+    public void addSecondAxisSeries(XYSeries series, boolean visible) {
         if (second == null) {
             second = new XYSeriesCollection();
         }
 
         if (series != null) {
-            second.addSeries(series);
+            if (visible) {
+                second.addSeries(series);
+            }
+
+            combineYRanges(new Range(series.getMinY(), series.getMaxY()), 1);
+            combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
         }
     }
 
 
-    public void addAnnotations(FLYSAnnotation annotation) {
+    private void combineXRanges(Range range, int index) {
+        Integer key = new Integer(index);
+
+        if (xRanges == null) {
+            xRanges = new HashMap<Integer, Range>();
+            xRanges.put(key, range);
+            return;
+        }
+
+        Range newX = null;
+        Range oldX = xRanges.get(key);
+
+        if (oldX != null) {
+            newX = Range.combine(oldX, range);
+        }
+        else {
+            newX = range;
+        }
+
+        xRanges.put(key, newX);
+    }
+
+
+    private void combineYRanges(Range range, int index) {
+        Integer key = new Integer(index);
+
+        if (yRanges == null) {
+            yRanges = new HashMap<Integer, Range>();
+            yRanges.put(key, range);
+            return;
+        }
+
+        Range newY = null;
+        Range oldY = yRanges.get(key);
+
+        if (oldY != null) {
+            newY = Range.combine(oldY, range);
+        }
+        else {
+            newY = range;
+        }
+
+        yRanges.put(key, newY);
+    }
+
+
+    public void addAnnotations(FLYSAnnotation annotation, boolean visible) {
+        if (!visible) {
+            return;
+        }
+
         if (annotations == null) {
             annotations = new ArrayList<FLYSAnnotation>();
         }
@@ -206,14 +273,14 @@
         Range xrange = getDomainAxisRange();
         Range yrange = getValueAxisRange();
 
-        for (int i = 0, num = plot.getDatasetCount(); i < num; i++) {
-            XYDataset dataset = plot.getDataset(i);
+        logger.debug("XXX: CLIENT X RANGE = " + xrange);
+        logger.debug("XXX: CLIENT Y RANGE = " + yrange);
 
-            if (dataset == null) {
-                continue;
-            }
+        for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
+            Range[] ranges = new Range[] {
+                xRanges.get(0),
+                yRanges.get(new Integer(i)) };
 
-            Range[]   ranges  = getRangesForDataset(dataset);
 
             if (i == 0) {
                 ValueAxis xaxis = plot.getDomainAxis();
@@ -226,6 +293,8 @@
                 continue;
             }
 
+            logger.debug("XXX Zoom y axis for index: " + i);
+            logger.debug("XXX    Y MAX RANGE = " + ranges[1]);
             zoomY(plot, yaxis, ranges[1], yrange);
         }
     }
@@ -274,46 +343,18 @@
 
 
     /**
-     * This method extracts the minimum and maximum values for x and y axes.
+     * This method extracts the minimum and maximum values for x and y axes
+     * which are stored in <i>xRanges</i> and <i>yRanges</i>.
      *
-     * @param dataset The dataset that should be observed.
+     * @param index The index of the y-Axis.
      *
      * @return a Range[] as follows: [x-Range, y-Range].
      */
-    public static Range[] getRangesForDataset(XYDataset dataset) {
-        double[] xr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
-        double[] yr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
-
-        if (dataset != null) {
-            int sCount = dataset.getSeriesCount();
-
-            for (int i = 0; i < sCount; i++) {
-                int iCount = dataset.getItemCount(i);
-    
-                for (int j = 0; j < iCount; j++) {
-                    double x = dataset.getX(i, j).doubleValue();
-                    double y = dataset.getY(i, j).doubleValue();
-    
-                    if (!Double.isNaN(x)) {
-                        xr[0] = xr[0] < x ? xr[0] : x;
-                        xr[1] = xr[1] > x ? xr[1] : x;
-                    }
-    
-                    if (!Double.isNaN(y)) {
-                        yr[0] = yr[0] < y ? yr[0] : y;
-                        yr[1] = yr[1] > y ? yr[1] : y;
-                    }
-                }
-            }
-        }
-
-        // this is only required, if there are no items in the dataset.
-        xr[0] = xr[0] < xr[1] ? xr[0] : xr[1];
-        xr[1] = xr[1] > xr[0] ? xr[1] : xr[0];
-        yr[0] = yr[0] < yr[1] ? yr[0] : yr[1];
-        yr[1] = yr[1] > yr[0] ? yr[1] : yr[0];
-
-        return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])};
+    public Range[] getRangesForDataset(int index) {
+        return new Range[] {
+            xRanges.get(new Integer(0)),
+            yRanges.get(new Integer(index))
+        };
     }
 
 

http://dive4elements.wald.intevation.org