changeset 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 bc1e4878d7e3
children d5f9ba1d055f
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartInfoGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/InfoGeneratorHelper.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 4 files changed, 146 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 09 10:31:22 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jun 09 10:48:13 2011 +0000
@@ -1,3 +1,26 @@
+2011-06-09  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/InfoGeneratorHelper.java:
+	  Append the min/max range and a transformation matrix for each axis.
+
+	* src/main/java/de/intevation/flys/exports/ChartInfoGenerator.java:
+	  Instantiate the InfoGeneratorHelper with a XYChartGenerator instance.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Changed the zoom operation. The zoom values defined in the chart request
+	  document are no longer absolute values for a specific axis. Those values
+	  represent percental values for the start and end point of x and y axes.
+	  E.g. a chart has three axes with the following ranges:
+	    - x axis  :  0 - 10
+	    - y axis 1: 20 - 40
+	    - y axis 2: 40 - 90
+	    - zoom values for x: 0.1 - 0.9 (10% - 90%)
+	    - zoom values for y: 0.2 - 0.8 (20% - 80%)
+	  The produced chart will have the following ranges:
+	    - x axis  :  1 - 9
+		- y axis 1: 24 - 36
+		  y axis 2: 50 - 80
+
 2011-06-09  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionGenerator.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartInfoGenerator.java	Thu Jun 09 10:31:22 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartInfoGenerator.java	Thu Jun 09 10:48:13 2011 +0000
@@ -101,7 +101,8 @@
 
         chart.createBufferedImage(size[0], size[1], Transparency.BITMASK, info);
 
-        Document doc = InfoGeneratorHelper.createInfoDocument(chart, info);
+        InfoGeneratorHelper helper = new InfoGeneratorHelper(generator);
+        Document doc = helper.createInfoDocument(chart, info);
 
         XMLUtils.toStream(doc, out);
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/InfoGeneratorHelper.java	Thu Jun 09 10:31:22 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/InfoGeneratorHelper.java	Thu Jun 09 10:48:13 2011 +0000
@@ -14,6 +14,7 @@
 import org.jfree.chart.axis.ValueAxis;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.data.Range;
+import org.jfree.data.xy.XYDataset;
 
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
@@ -31,7 +32,11 @@
         Logger.getLogger(InfoGeneratorHelper.class);
 
 
-    private InfoGeneratorHelper() {
+    protected XYChartGenerator generator;
+
+
+    public InfoGeneratorHelper(XYChartGenerator generator) {
+        this.generator = generator;
     }
 
 
@@ -43,7 +48,7 @@
      *
      * @return the info document.
      */
-    public static Document createInfoDocument(
+    public Document createInfoDocument(
         JFreeChart         chart,
         ChartRenderingInfo info)
     {
@@ -59,7 +64,7 @@
         Element chartinfo = cr.create("chartinfo");
 
         chartinfo.appendChild(createAxesElements(cr, chart));
-        chartinfo.appendChild(createTransformationElement(cr, chart, info));
+        chartinfo.appendChild(createTransformationElements(cr, chart, info));
 
         doc.appendChild(chartinfo);
 
@@ -76,7 +81,7 @@
      *
      * @return an element with axes information.
      */
-    protected static Element createAxesElements(
+    protected Element createAxesElements(
         ElementCreator     cr,
         JFreeChart         chart)
     {
@@ -89,8 +94,10 @@
         int dAxisCount = plot.getDomainAxisCount();
         for (int i = 0; i < dAxisCount; i++) {
             ValueAxis axis = plot.getDomainAxis(i);
+            XYDataset data = plot.getDataset(i);
+
             if (axis != null) {
-                Element e = createAxisElement(cr, axis, "domain", i);
+                Element e = createAxisElement(cr, axis, data, "domain", i);
                 axes.appendChild(e);
             }
         }
@@ -98,10 +105,15 @@
         int rAxisCount = plot.getRangeAxisCount();
         for (int i = 0; i < rAxisCount; i++) {
             ValueAxis axis = plot.getRangeAxis(i);
-            if (axis != null) {
-                Element e = createAxisElement(cr, axis, "range", i);
-                axes.appendChild(e);
+            XYDataset data = plot.getDataset(i);
+
+            if (axis == null || data == null) {
+                logger.warn("Axis or dataset is empty at pos: " + i);
+                continue;
             }
+
+            Element e = createAxisElement(cr, axis, data, "range", i);
+            axes.appendChild(e);
         }
 
         return axes;
@@ -114,14 +126,16 @@
      *
      * @param cr The ElementCreator
      * @param axis The axis that provides range information.
+     * @param dataset The dataset for min/max determination.
      * @param type The axis type ('domain' or 'range').
      * @param pos The position in the chart.
      *
      * @return An element that contains range information of a given axis.
      */
-    protected static Element createAxisElement(
+    protected Element createAxisElement(
         ElementCreator cr,
         ValueAxis      axis,
+        XYDataset      dataset,
         String         type,
         int            pos)
     {
@@ -132,6 +146,19 @@
         cr.addAttr(e, "from", String.valueOf(range.getLowerBound()), true);
         cr.addAttr(e, "to", String.valueOf(range.getUpperBound()), true);
 
+        Range[] rs = generator.getRangesForDataset(dataset);
+        Range   r  = null;
+
+        if (type.equals("range")) {
+            r = rs[1];
+        }
+        else {
+            r = rs[0];
+        }
+
+        cr.addAttr(e, "min", String.valueOf(r.getLowerBound()), true);
+        cr.addAttr(e, "max", String.valueOf(r.getUpperBound()), true);
+
         return e;
     }
 
@@ -146,12 +173,12 @@
      *
      * @return an element that contains one or more transformation matrix.
      */
-    protected static Element createTransformationElement(
+    protected Element createTransformationElements(
         ElementCreator     cr,
         JFreeChart         chart,
         ChartRenderingInfo info)
     {
-        logger.debug("InfoGeneratorHelper.createTransformationElement");
+        logger.debug("InfoGeneratorHelper.createTransformationElements");
 
         Element tf = cr.create("transformation-matrix");
 
@@ -159,16 +186,60 @@
 
         XYPlot    plot  = (XYPlot) chart.getPlot();
         ValueAxis xAxis = plot.getDomainAxis();
-        ValueAxis yAxis = plot.getRangeAxis();
 
+        if (xAxis == null) {
+            logger.error("There is no x axis in the chart!");
+            return null;
+        }
+
+        for (int i  = 0, num = plot.getRangeAxisCount(); i < num; i++) {
+            ValueAxis yAxis = plot.getRangeAxis(i);
+
+            if (yAxis == null) {
+                logger.warn("No y axis at pos " + i + " existing.");
+                continue;
+            }
+
+            Element matrix = createTransformationElement(
+                cr, xAxis, yAxis, dataArea, i);
+
+            tf.appendChild(matrix);
+        }
+
+        return tf;
+    }
+
+
+    /**
+     * Creates an element that contains values used to transform coordinates
+     * of a coordinate system A into a coordinate system B.
+     *
+     * @param cr The ElementCreator.
+     * @param xAxis The x axis of the target coordinate system.
+     * @param yAxis The y axis of the target coordinate system.
+     * @param dataArea The pixel coordinates of the chart image.
+     * @param pos The dataset position.
+     *
+     * @return an element that contains transformation matrix values.
+     */
+    protected Element createTransformationElement(
+        ElementCreator cr,
+        ValueAxis      xAxis,
+        ValueAxis      yAxis,
+        Rectangle2D    dataArea,
+        int            pos)
+    {
         double[] tm = createTransformationMatrix(dataArea, xAxis, yAxis);
 
-        cr.addAttr(tf, "sx", String.valueOf(tm[0]), true);
-        cr.addAttr(tf, "sy", String.valueOf(tm[1]), true);
-        cr.addAttr(tf, "tx", String.valueOf(tm[2]), true);
-        cr.addAttr(tf, "ty", String.valueOf(tm[3]), true);
+        Element matrix = cr.create("matrix");
 
-        return tf;
+        cr.addAttr(matrix, "pos", String.valueOf(pos), true);
+        cr.addAttr(matrix, "sx", String.valueOf(tm[0]), true);
+        cr.addAttr(matrix, "sy", String.valueOf(tm[1]), true);
+        cr.addAttr(matrix, "tx", String.valueOf(tm[2]), true);
+        cr.addAttr(matrix, "ty", String.valueOf(tm[3]), true);
+
+        return matrix;
     }
 
 
--- 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