diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 673:b22f21b173a7

Changed the zoom process - the values in the chart request document are percentual values that apply to every axis. flys-artifacts/trunk@2095 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 09 Jun 2011 10:48:13 +0000
parents 434146596838
children be4709640aac
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jun 09 10:31:22 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jun 09 10:48:13 2011 +0000
@@ -106,7 +106,7 @@
         addSubtitles(chart);
         adjustPlot(plot);
         adjustAxes(plot);
-        zoom(plot);
+        autoZoom(plot);
 
         return chart;
     }
@@ -121,35 +121,28 @@
      *
      * @param plot The XYPlot.
      */
-    protected void zoom(XYPlot plot) {
+    protected void autoZoom(XYPlot plot) {
         logger.debug("Zoom to specified ranges.");
 
-        Range[] ranges = null;
-
-        boolean x = zoomX(plot);
-        if (!x) {
-            XYDataset dataset = plot.getDataset();
-
-            ranges = getRangesForDataset(dataset);
-
-            logger.debug("No x range specified. Set manually: " + ranges[0]);
+        Range xrange = getDomainAxisRange();
+        Range yrange = getValueAxisRange();
 
-            ValueAxis axis = plot.getDomainAxis();
-            axis.setRange(ranges[0]);
-        }
+        for (int i = 0, num = plot.getDatasetCount(); i < num; i++) {
+            XYDataset dataset = plot.getDataset(i);
+            Range[]   ranges  = getRangesForDataset(dataset);
 
-        boolean y = zoomY(plot);
-        if (!y) {
-            XYDataset dataset = plot.getDataset();
-
-            if (ranges == null) {
-                ranges = getRangesForDataset(dataset);
+            if (i == 0) {
+                ValueAxis xaxis = plot.getDomainAxis();
+                zoom(plot, xaxis, ranges[0], xrange);
             }
 
-            logger.debug("No y range specified. Set manually: " + ranges[1]);
+            ValueAxis yaxis = plot.getRangeAxis(i);
 
-            ValueAxis axis = plot.getRangeAxis();
-            axis.setRange(ranges[1]);
+            if (yaxis == null) {
+                continue;
+            }
+
+            zoom(plot, yaxis, ranges[1], yrange);
         }
     }
 
@@ -158,42 +151,30 @@
      * Zooms the x axis to the range specified in the attribute document.
      *
      * @param plot The XYPlot.
+     * @param axis The axis the shoud be modified.
+     * @param range The whole range specified by a dataset.
+     * @param x A user defined range (null permitted).
      *
      * @return true, if a zoom range was specified, otherwise false.
      */
-    protected boolean zoomX(XYPlot plot) {
-        Range xrange = getDomainAxisRange();
-        if (xrange != null) {
-            ValueAxis xaxis = plot.getDomainAxis();
-            xaxis.setRange(xrange);
+    protected boolean zoom(XYPlot plot, ValueAxis axis, Range range, Range x) {
+        if (x != null) {
+            double min  = range.getLowerBound();
+            double max  = range.getUpperBound();
+            double diff = max > min ? max - min : min - max;
 
-            logger.debug("Zoom chart to X: " + xrange);
+            Range computed = new Range(
+                min + x.getLowerBound() * diff,
+                min + x.getUpperBound() * diff);
+
+            axis.setRange(computed);
+
+            logger.debug("Zoom axis to: " + computed);
 
             return true;
         }
 
-        return false;
-    }
-
-
-    /**
-     * Zooms the y axis to the range specified in the attribute document.
-     *
-     * @param plot The XYPlot.
-     *
-     * @return true, if a zoom range was specified, otherwise false.
-     */
-    protected boolean zoomY(XYPlot plot) {
-        Range yrange = getValueAxisRange();
-        if (yrange != null) {
-            ValueAxis yaxis = plot.getRangeAxis();
-            yaxis.setRange(yrange);
-
-            logger.debug("Zoom chart to Y: " + yrange);
-
-            return true;
-        }
-
+        axis.setRange(range);
         return false;
     }
 
@@ -205,7 +186,7 @@
      *
      * @return a Range[] as follows: [x-Range, y-Range].
      */
-    protected Range[] getRangesForDataset(XYDataset dataset) {
+    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 };
 

http://dive4elements.wald.intevation.org