diff gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java @ 364:2413273f1c13

Workarround: Store lower and upper bounds of data while iterating over all data and set the max range of axes with these information. JFreeCharts method NumberAxis.setAutoRange(true) doesn't seem to work properly. gnv-artifacts/trunk@439 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Dec 2009 11:58:44 +0000
parents 4ac3c1c1c060
children d41c155db337
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java	Wed Dec 16 08:05:25 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java	Wed Dec 16 11:58:44 2009 +0000
@@ -20,6 +20,8 @@
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 import org.jfree.chart.title.TextTitle;
+import org.jfree.data.xy.XYDataset;
+import org.jfree.data.Range;
 import org.jfree.data.general.Series;
 
 import de.intevation.gnv.geobackend.base.Result;
@@ -32,6 +34,9 @@
 public abstract class AbstractXYLineChart
 extends               AbstractChart
 {
+    public static final double LOWER_MARGIN = 0.05D;
+    public static final double UPPER_MARGIN = 0.05D;
+
     private static Logger log      = Logger.getLogger(AbstractXYLineChart.class);
 
     protected static Color[] COLOR = {
@@ -43,8 +48,13 @@
 
     protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL;
 
+    /** Map to store datasets for each parameter */
     protected Map datasets;
 
+    /** Map to store max ranges of each parameter (axis.setAutoRange(true)
+     * doesn't seem to work */
+    protected Map ranges;
+
     protected abstract void initData();
     protected abstract void addValue(Result row, Series series);
     protected abstract void addSeries(Series series, String label, int idx);
@@ -100,24 +110,29 @@
         localizeDomainAxis(xAxis, locale);
         localizeRangeAxis(yAxis, locale);
 
+        // litte workarround to adjust the max range of axes.
+        // NumberAxis.setAutoRange(true) doesn't seem to work properly.
+        Range yRange     = (Range) ranges.get(seriesKey);
+        yAxis.setRange(Range.expand(yRange, LOWER_MARGIN, UPPER_MARGIN));
+        log.debug("Max Range of dataset is: " + yRange.toString());
+
         if (seriesKey.contains("richtung")) {
             yAxis.setTickUnit(new NumberTickUnit(30.0));
             yAxis.setUpperBound(360.0);
             yAxis.setLowerBound(0.0);
-            plot.setRangeAxis(idx, yAxis);
         }
         else {
             yAxis.setFixedDimension(10.0);
             yAxis.setAutoRangeIncludesZero(false);
-            plot.setRangeAxis(idx, yAxis);
-            yAxis.configure();
         }
 
+        plot.setRangeAxis(idx, yAxis);
+        yAxis.configure();
+
         if (idx % 2 != 0)
             plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_RIGHT);
         else
             plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_LEFT);
-        plot.mapDatasetToRangeAxis(idx, idx);
     }
 
 
@@ -172,6 +187,42 @@
     }
 
 
+    public Range getMaxRangeOfDataset(XYDataset dataset) {
+        int    seriesCount = dataset.getSeriesCount();
+        double upper       = Double.NEGATIVE_INFINITY;
+        double lower       = Double.POSITIVE_INFINITY;
+
+        for (int i = 0; i < seriesCount; i++) {
+            int itemCount = dataset.getItemCount(i);
+
+            for (int j = 0; j < itemCount; j++) {
+                Number num = dataset.getY(i, j);
+
+                if (num != null) {
+                    double y = num.doubleValue();
+                    lower    = y < lower ? y : lower;
+                    upper    = y > upper ? y : upper;
+                }
+            }
+        }
+
+        return new Range(lower, upper);
+    }
+
+
+    public Range getMaxRangeOfDatasetWithMargin(
+        XYDataset dataset,
+        double    percent
+    ) {
+        Range range   = getMaxRangeOfDataset(dataset);
+        double length = range.getLength();
+        double upper  = range.getUpperBound() + length /100 * percent;
+        double lower  = range.getLowerBound() - length /100 * percent;
+
+        return new Range(lower, upper);
+    }
+
+
     protected String findParameter(String label) {
         Iterator iter = parameters.iterator();
 
@@ -201,5 +252,22 @@
         }
         return "";
     }
+
+
+    protected void storeMaxRange(double value, String parameter) {
+        Range  range     = null;
+
+        range = ranges.containsKey(parameter)
+            ? (Range) ranges.get(parameter)
+            : new Range(value, value);
+
+        double lower = range.getLowerBound();
+        double upper = range.getUpperBound();
+
+        lower = value < lower ? value : lower;
+        upper = value > upper ? value : upper;
+
+        ranges.put(parameter, new Range(lower, upper));
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org