changeset 321:a4376fd23f99

Implemented svg export of charts. gnv-artifacts/trunk@383 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 30 Nov 2009 14:18:37 +0000 (2009-11-30)
parents 18302372703f
children 57a2bf630854
files gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 2 files changed, 114 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Mon Nov 30 09:51:59 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Mon Nov 30 14:18:37 2009 +0000
@@ -13,9 +13,14 @@
 import java.awt.geom.Rectangle2D;
 import java.awt.geom.Rectangle2D.Double;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import javax.imageio.ImageIO;
 
+import org.apache.batik.dom.svg.SVGDOMImplementation;
+import org.apache.batik.svggen.SVGGraphics2D;
+import org.apache.batik.svggen.SVGGraphics2DIOException;
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.JFreeChart;
@@ -27,6 +32,7 @@
 public class ChartExportHelper {
 
     private static final String  DEFAULT_PAGE_SIZE = "A4";
+    private static final String  DEFAULT_ENCODING  = "UTF-8";
 
     private static Logger log = Logger.getLogger(ChartExportHelper.class);
 
@@ -51,8 +57,33 @@
     }
 
 
-    public static void exportSVG() {
-        log.info("export chart as svg (not implemented yet)");
+    public static void exportSVG(
+        OutputStream out,
+        JFreeChart   chart,
+        String       encoding,
+        int          width,
+        int          height
+    ) {
+        log.info("export chart as svg");
+
+        if (encoding == null)
+            encoding = DEFAULT_ENCODING;
+
+        SVGDOMImplementation svgDom   = new SVGDOMImplementation();
+        org.w3c.dom.Document document = svgDom.createDocument(null,"svg",null);
+        SVGGraphics2D        graphics = new SVGGraphics2D(document);
+
+        chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,width,height));
+
+        try {
+            graphics.stream(new OutputStreamWriter(out, encoding));
+        }
+        catch (SVGGraphics2DIOException svge) {
+            log.error("Error while writing svg export to output stream.", svge);
+        }
+        catch (UnsupportedEncodingException uee) {
+            log.error("Unsupported encoding: " + encoding, uee);
+        }
     }
 
 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Mon Nov 30 09:51:59 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Mon Nov 30 14:18:37 2009 +0000
@@ -6,6 +6,7 @@
 import java.io.IOException;
 import java.io.File;
 import java.io.OutputStream;
+import java.io.FileOutputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -272,6 +273,36 @@
                     locale
                 );
             }
+            else if (outputMode.equalsIgnoreCase("svg")) {
+                log.debug("Output mode == svg");
+                int width  = 600;
+                int height = 400;
+
+                Locale[] serverLocales    =
+                    RessourceFactory.getInstance().getLocales();
+                Locale locale             =
+                    callMeta.getPreferredLocale(serverLocales);
+
+                log.debug(
+                    "Best locale - regarding intersection of server and " +
+                    "browser locales -  is " + locale.toString()
+                );
+
+                createSVG(
+                    outputStream,
+                    getParameters(uuid),
+                    getMeasurements(uuid),
+                    getDates(uuid),
+                    new ChartLabels(
+                        this.getFisName(callMeta.getLanguages()) +
+                        " "+ getSelectedFeatureName(uuid),
+                        this.domainLable),
+                    uuid,
+                    locale,
+                    width,
+                    height
+                );
+            }
             else if (outputMode.equalsIgnoreCase("csv")) {
                 log.debug("CSV-File will be generated.");
                 Collection<Result> chartResult = this.getChartResult(uuid);
@@ -513,6 +544,56 @@
             PDF_FORMAT_LANDSCAPE,
             50F, 50F, 50F, 50F
         );
+
+        try {
+            OutputStream toFile = new FileOutputStream("/vol1/home/iweinzierl/tmp/test.svg");
+            ChartExportHelper.exportSVG(
+                toFile,
+                chart.generateChart(),
+                null,
+                600, 400
+            );
+            toFile.flush();
+            toFile.close();
+        }
+        catch(Exception e) { log.debug("ERROR WHLILE TEST."); }
+    }
+
+
+    protected void createSVG(
+        OutputStream outputStream,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   dates,
+        ChartLabels  chartLables,
+        String       uuid,
+        Locale       locale,
+        int          width,
+        int          height
+    ) {
+        Chart chart = getChart(
+            chartLables,
+            parameters,
+            measurements,
+            dates,
+            getChartResult(uuid),
+            locale,
+            uuid
+        );
+
+        if (chart == null) {
+            log.error("Could not initialize chart.");
+            return;
+        }
+
+        ChartExportHelper.exportSVG(
+            outputStream,
+            chart.generateChart(),
+            null,
+            600, 400
+        );
+
+        log.debug("svg export finished.");
     }
 
 

http://dive4elements.wald.intevation.org