changeset 1735:5966a20fc72c

Enabled support for PDF and SVG chart exports. flys-artifacts/trunk@3023 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 19 Oct 2011 09:43:04 +0000
parents 25d481cad4fb
children 0155cbaba182
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 4 files changed, 133 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Oct 18 15:34:07 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Oct 19 09:43:04 2011 +0000
@@ -1,3 +1,17 @@
+2011-10-19  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/ChartGenerator.java: Added
+	  getFormat() which extracts the format string from XML request document.
+
+	* src/main/java/de/intevation/flys/exports/ChartExportHelper.java: Adapted
+	  method signatures of exportImage(), exportSVG() and exportPDF(). All
+	  methods now take a CallContext object which stores extra chart export
+	  parameters.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Enabled PDF and SVG chart exports based on the "format" string given in
+	  the XML request document.
+
 2011-10-18  Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	Simplify rendereing W(Q)Kms in WDifferencesCurveGenerator.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Tue Oct 18 15:34:07 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Wed Oct 19 09:43:04 2011 +0000
@@ -51,15 +51,22 @@
  */
 public class ChartExportHelper {
 
+    public static final String FORMAT_PNG = "png";
+
+    public static final String FORMAT_PDF = "pdf";
+
+    public static final String FORMAT_SVG = "svg";
+
+
     /**
      * Constant field to define A4 as default page size.
      */
-    private static final String  DEFAULT_PAGE_SIZE = "A4";
+    public static final String  DEFAULT_PAGE_SIZE = "A4";
 
     /**
      * Constant field to define UTF-8 as default encoding.
      */
-    private static final String  DEFAULT_ENCODING  = "UTF-8";
+    public static final String  DEFAULT_ENCODING  = "UTF-8";
 
     /**
      * Logger used for logging with log4j.
@@ -81,10 +88,8 @@
      */
     public static void exportImage(
         OutputStream out,
-        JFreeChart chart,
-        String format,
-        int width,
-        int height
+        JFreeChart   chart,
+        CallContext  cc
     )
     throws IOException
     {
@@ -92,9 +97,13 @@
 
         ChartRenderingInfo info = new ChartRenderingInfo();
 
+        String format = (String) cc.getContextValue("chart.image.format");
+
+        int[] size = getSize(cc);
+
         ImageIO.write(
             chart.createBufferedImage(
-                width, height, Transparency.BITMASK, info
+                size[0], size[1], Transparency.BITMASK, info
             ),
             format,
             out
@@ -108,17 +117,16 @@
      *
      * @param out OutputStream
      * @param chart JFreeChart to be exported
-     * @param encoding Encoding, defaults to {@link #DEFAULT_ENCODING} if null
-     * @param width Width the svg used to be
-     * @param height Height the svg used to be
+     * @param context The CallContext object that contains extra chart
+     * parameters.
      */
     public static void exportSVG(
         OutputStream out,
         JFreeChart   chart,
-        String       encoding,
-        int          width,
-        int          height
+        CallContext  context
     ) {
+        String encoding = (String) context.getContextValue("chart.encoding");
+
         log.info("export chart as svg");
 
         if (encoding == null)
@@ -127,7 +135,9 @@
         org.w3c.dom.Document document = XMLUtils.newDocument();
         SVGGraphics2D        graphics = new SVGGraphics2D(document);
 
-        chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,width,height));
+        int[] size = getSize(context);
+
+        chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,size[0],size[1]));
 
         try {
             graphics.stream(new OutputStreamWriter(out, encoding));
@@ -159,15 +169,12 @@
     public static void exportPDF(
         OutputStream out,
         JFreeChart   chart,
-        String       pageFormat,
-        float        marginLeft,
-        float        marginRight,
-        float        marginTop,
-        float        marginBottom,
-        CallContext  context
+        CallContext  cc
     ) {
         log.info("export chart as pdf.");
 
+        String pageFormat = (String) cc.getContextValue("chart.page.format");
+
         if (pageFormat == null)
             pageFormat = DEFAULT_PAGE_SIZE;
 
@@ -177,10 +184,9 @@
         float pageHeight = page.getHeight();
 
         // the chart width
-        int chartWidth  = (Integer) context.getContextValue("chart.width");
-        int chartHeight = (Integer) context.getContextValue("chart.height");
+        int[] size = getSize(cc);
 
-        boolean landscape = chartWidth > chartHeight;
+        boolean landscape = size[0] > size[1];
 
         float width  = 0;
         float height = 0;
@@ -193,22 +199,34 @@
             height = pageHeight;
         }
 
+        float marginLeft = (Float) cc.getContextValue(
+            "chart.marginLeft");
+
+        float marginRight = (Float) cc.getContextValue(
+            "chart.marginRight");
+
+        float marginTop = (Float) cc.getContextValue(
+            "chart.marginTop");
+
+        float marginBottom = (Float) cc.getContextValue(
+            "chart.marginBottom");
+
         float spaceX = width  - marginLeft - marginRight;
-        if (chartWidth > spaceX) {
+        if (size[0] > spaceX) {
             log.warn("Width of the chart is too big for pdf -> resize it now.");
-            double ratio = ((double)spaceX) / chartWidth;
-            chartWidth  *= ratio;
-            chartHeight *= ratio;
-            log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
+            double ratio = ((double)spaceX) / size[0];
+            size[0]  *= ratio;
+            size[1] *= ratio;
+            log.debug("Resized chart to " + size[0] + "x" + size[1]);
         }
 
         float spaceY = height - marginTop  - marginBottom;
-        if (chartHeight > spaceY) {
+        if (size[1] > spaceY) {
             log.warn("Height of the chart is too big for pdf -> resize it now.");
-            double ratio = ((double)spaceY) / chartHeight;
-            chartWidth  *= ratio;
-            chartHeight *= ratio;
-            log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
+            double ratio = ((double)spaceY) / size[1];
+            size[0]  *= ratio;
+            size[1] *= ratio;
+            log.debug("Resized chart to " + size[0] + "x" + size[1]);
         }
 
         Document document = null;
@@ -234,10 +252,10 @@
             double[] origin = getCenteredAnchor(
                 marginLeft, marginRight, marginBottom, marginTop,
                 width, height,
-                chartWidth, chartHeight);
+                size[0], size[1]);
 
             Rectangle2D area = new Rectangle2D.Double(
-                origin[0], origin[1], chartWidth, chartHeight);
+                origin[0], origin[1], size[0], size[1]);
 
             chart.draw(graphics, area);
             graphics.dispose();
@@ -252,6 +270,16 @@
     }
 
 
+    public static int[] getSize(CallContext cc) {
+        int[] size = new int[2];
+
+        size[0] = (Integer) cc.getContextValue("chart.width");
+        size[1] = (Integer) cc.getContextValue("chart.height");
+
+        return size;
+    }
+
+
     /**
      * This method returns the anchor of the chart so that the chart is centered
      * according to the given parameters.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Tue Oct 18 15:34:07 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Wed Oct 19 09:43:04 2011 +0000
@@ -48,11 +48,17 @@
     /** The default chart height, if no other height is set.*/
     public static final int DEFAULT_CHART_HEIGHT = 400;
 
+    /** The default chart format, if no other height is set.*/
+    public static final String DEFAULT_CHART_FORMAT = "png";
+
     /** The XPath that points to the chart size of the incoming request
      * document.*/
     public static final String XPATH_CHART_SIZE =
         "/art:action/art:attributes/art:size";
 
+    public static final String XPATH_CHART_FORMAT =
+        "/art:action/art:attributes/art:format/@art:value";
+
     public static final String XPATH_CHART_X_RANGE =
         "/art:action/art:attributes/art:xrange";
 
@@ -165,6 +171,19 @@
     }
 
 
+    protected String getFormat() {
+        String format = (String) XMLUtils.xpath(
+            request,
+            XPATH_CHART_FORMAT,
+            XPathConstants.STRING,
+            ArtifactNamespaceContext.INSTANCE);
+
+        return format == null || format.length() == 0
+            ? DEFAULT_CHART_FORMAT
+            : format;
+    }
+
+
     protected Range getDomainAxisRange() {
         Element xrange = (Element)XMLUtils.xpath(
             request,
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Tue Oct 18 15:34:07 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Wed Oct 19 09:43:04 2011 +0000
@@ -98,13 +98,44 @@
 
         JFreeChart chart = generateChart();
 
-        int[] size = getSize();
+        String format = getFormat();
+        int[]  size   = getSize();
 
-        ChartExportHelper.exportImage(
-            out,
-            chart,
-            "png",
-            size[0], size[1]);
+        context.putContextValue("chart.width", size[0]);
+        context.putContextValue("chart.height", size[1]);
+
+        if (format.equals(ChartExportHelper.FORMAT_PNG)) {
+            context.putContextValue("chart.image.format", "png");
+
+            ChartExportHelper.exportImage(
+                out,
+                chart,
+                context);
+        }
+        else if (format.equals(ChartExportHelper.FORMAT_PDF)) {
+            context.putContextValue("chart.marginLeft", 5f);
+            context.putContextValue("chart.marginRight", 5f);
+            context.putContextValue("chart.marginTop", 5f);
+            context.putContextValue("chart.marginBottom", 5f);
+            context.putContextValue(
+                "chart.page.format",
+                ChartExportHelper.DEFAULT_PAGE_SIZE);
+
+            ChartExportHelper.exportPDF(
+                out,
+                chart,
+                context);
+        }
+        else if (format.equals(ChartExportHelper.FORMAT_SVG)) {
+            context.putContextValue(
+                "chart.encoding",
+                ChartExportHelper.DEFAULT_ENCODING);
+
+            ChartExportHelper.exportSVG(
+                out,
+                chart,
+                context);
+        }
     }
 
 

http://dive4elements.wald.intevation.org