changeset 652:8fa4c5c9cd1a

Charts are zoomed to a specified view if the attribute document for the chart creation contains an x and/or y range. flys-artifacts/trunk@2047 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 03 Jun 2011 06:47:51 +0000
parents e6cecb661bff
children 67c7020f4ed3
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 3 files changed, 131 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 02 15:47:08 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jun 03 06:47:51 2011 +0000
@@ -1,3 +1,11 @@
+2011-06-03  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/ChartGenerator.java: Added new
+	  methods to extract the x and y ranges from request document.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java: Added a
+	  method that zooms the chart to the specified x and y ranges.
+
 2011-06-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/conf/conf.xml: Set collection ttl to 6 hours.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Jun 02 15:47:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Jun 03 06:47:51 2011 +0000
@@ -10,6 +10,8 @@
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
+import org.jfree.data.Range;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
@@ -44,6 +46,12 @@
     public static final String XPATH_CHART_SIZE =
         "/art:action/art:attributes/art:size";
 
+    public static final String XPATH_CHART_X_RANGE =
+        "/art:action/art:attributes/art:xrange";
+
+    public static final String XPATH_CHART_Y_RANGE =
+        "/art:action/art:attributes/art:yrange";
+
 
     /** The document of the incoming out() request.*/
     protected Document request;
@@ -135,6 +143,96 @@
     }
 
 
+    protected Range getDomainAxisRange() {
+        Node xrange = (Node) XMLUtils.xpath(
+            request,
+            XPATH_CHART_X_RANGE,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (xrange == null) {
+            return null;
+        }
+
+        String lower = XMLUtils.xpathString(
+            xrange, "@art:from", ArtifactNamespaceContext.INSTANCE);
+
+        String upper = XMLUtils.xpathString(
+            xrange, "@art:to", ArtifactNamespaceContext.INSTANCE);
+
+        logger.debug("FOUND X RANGE: " + lower + " -> " + upper);
+
+        if (lower != null && upper != null) {
+            try {
+                double from = Double.parseDouble(lower);
+                double to   = Double.parseDouble(upper);
+
+                if (from == 0 && to == 0) {
+                    logger.debug("No range specified. Lower and upper X == 0");
+                    return null;
+                }
+
+                if (from > to) {
+                    double tmp = to;
+                    to         = from;
+                    from       = tmp;
+                }
+
+                return new Range(from, to);
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn("Wrong values for domain axis range.");
+            }
+        }
+
+        return null;
+    }
+
+
+    protected Range getValueAxisRange() {
+        Node yrange = (Node) XMLUtils.xpath(
+            request,
+            XPATH_CHART_Y_RANGE,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (yrange == null) {
+            return null;
+        }
+
+        String lower = XMLUtils.xpathString(
+            yrange, "@art:from", ArtifactNamespaceContext.INSTANCE);
+
+        String upper = XMLUtils.xpathString(
+            yrange, "@art:to", ArtifactNamespaceContext.INSTANCE);
+
+        if (lower != null && upper != null) {
+            try {
+                double from = Double.parseDouble(lower);
+                double to   = Double.parseDouble(upper);
+
+                if (from == 0 && to == 0) {
+                    logger.debug("No range specified. Lower and upper Y == 0");
+                    return null;
+                }
+
+                if (from > to) {
+                    double tmp = to;
+                    to         = from;
+                    from       = tmp;
+                }
+
+                return new Range(from, to);
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn("Wrong values for value axis range.");
+            }
+        }
+
+        return null;
+    }
+
+
     /**
      * Returns the default size of a chart export as array.
      *
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jun 02 15:47:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jun 03 06:47:51 2011 +0000
@@ -11,8 +11,10 @@
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.axis.ValueAxis;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
+import org.jfree.data.Range;
 
 import de.intevation.flys.exports.ChartExportHelper;
 
@@ -89,6 +91,8 @@
 
         int[] size = getSize();
 
+        zoom(plot);
+
         ChartExportHelper.exportImage(
             out,
             chart,
@@ -97,6 +101,27 @@
     }
 
 
+    protected void zoom(XYPlot plot) {
+        logger.debug("Zoom to specified ranges.");
+
+        Range xrange = getDomainAxisRange();
+        if (xrange != null) {
+            ValueAxis xaxis = plot.getDomainAxis();
+            xaxis.setRange(xrange);
+
+            logger.debug("Zoom chart to X: " + xrange);
+        }
+
+        Range yrange = getValueAxisRange();
+        if (yrange != null) {
+            ValueAxis yaxis = plot.getRangeAxis();
+            yaxis.setRange(yrange);
+
+            logger.debug("Zoom chart to Y: " + yrange);
+        }
+    }
+
+
     /**
      * Adjusts the axes of a plot.
      *

http://dive4elements.wald.intevation.org