changeset 639:a94ed2755480

Implemented SVG export for histograms. gnv-artifacts/trunk@724 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Mar 2010 14:48:32 +0000
parents c580666a843f
children e5f1e868ee3e
files gnv-artifacts/ChangeLog gnv-artifacts/doc/conf/products/horizontalprofile/conf_instantaneouspoint.xml gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh.xml gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml gnv-artifacts/doc/conf/products/timeseries/conf_mesh.xml gnv-artifacts/doc/conf/products/timeseries/conf_timeseriespoint.xml gnv-artifacts/doc/conf/products/verticalprofile/conf_instantaneouspoint.xml gnv-artifacts/doc/conf/products/verticalprofile/conf_mesh.xml gnv-artifacts/doc/conf/products/verticalprofile/conf_timeseriespoint.xml gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java
diffstat 12 files changed, 175 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Mar 02 14:48:32 2010 +0000
@@ -1,3 +1,27 @@
+2010-03-02  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/state/OutputStateBase.java: Added an xpath
+	  expression for export modes.
+	
+	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java:
+	  Refactoring of 'out' to be able to add pdf and svg exports to histogram. 
+	  Now, there are just four output modes left (chart, histogram, csv, odv). 
+	  Export modes (pdf, svg, img) are part of these output modes.
+	  Implemented svg export for histogram charts.
+	
+	* src/main/java/de/intevation/gnv/exports/ChartExportHelper.java: Helper
+	  method to export histograms as svg via output stream.
+
+	* doc/conf/products/timeseries/conf_mesh.xml,
+	  doc/conf/products/timeseries/conf_timeseriespoint.xml,
+	  doc/conf/products/horizontalprofile/conf_mesh.xml,
+	  doc/conf/products/horizontalprofile/conf_instantaneouspoint.xml,
+	  doc/conf/products/horizontalprofile/conf_mesh_cross.xml,
+	  doc/conf/products/verticalprofile/conf_mesh.xml,
+	  doc/conf/products/verticalprofile/conf_timeseriespoint.xml,
+	  doc/conf/products/verticalprofile/conf_instantaneouspoint.xml: Added svg
+	  as export mode to histogram output in configuration files.
+
 2010-03-02  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	  Issue180
--- a/gnv-artifacts/doc/conf/products/horizontalprofile/conf_instantaneouspoint.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/horizontalprofile/conf_instantaneouspoint.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -242,8 +242,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not implemented yet
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -243,8 +243,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not implemented yet
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/horizontalprofile/conf_mesh_cross.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -209,8 +209,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not implemented yet
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/timeseries/conf_mesh.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/timeseries/conf_mesh.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -231,8 +231,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not yet implemented
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                 <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/timeseries/conf_timeseriespoint.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/timeseries/conf_timeseriespoint.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -214,8 +214,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not yet implemented
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                 <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/verticalprofile/conf_instantaneouspoint.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/verticalprofile/conf_instantaneouspoint.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -112,8 +112,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not implemented yet
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
              <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
              <outputsMode name="odv" description="ODV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/verticalprofile/conf_mesh.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/verticalprofile/conf_mesh.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -247,8 +247,8 @@
                         <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                         <!-- not implemented yet
                         <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                        -->
                         <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                        -->
                     </exportModes>
                 </outputsMode>
                <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/doc/conf/products/verticalprofile/conf_timeseriespoint.xml	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/doc/conf/products/verticalprofile/conf_timeseriespoint.xml	Tue Mar 02 14:48:32 2010 +0000
@@ -185,8 +185,8 @@
                        <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
                        <!-- not implemented yet
                        <export name="pdf" description="PDF-Export der Daten" mime-type="application/pdf" />
+                       -->
                        <export name="svg" description="SVG-Export der Daten" mime-type="image/svg+xml" />
-                       -->
                     </exportModes>
                 </outputsMode>
                 <outputsMode name="csv" description="CSV-Export der Daten" mime-type="text/plain"/>
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Tue Mar 02 14:48:32 2010 +0000
@@ -92,6 +92,41 @@
     }
 
 
+    public static void exportHistogramsAsSVG(
+        OutputStream out,
+        Chart[]      histograms,
+        String       encoding,
+        int          width,
+        int          height
+    ) {
+        log.info("export histograms as svg");
+
+        if (encoding == null)
+            encoding = DEFAULT_ENCODING;
+
+        org.w3c.dom.Document document = XMLUtils.newDocument();
+        SVGGraphics2D        graphics = new SVGGraphics2D(document);
+
+        int size = histograms.length;
+        for (int i = 0; i < size; i++) {
+            JFreeChart chart = histograms[i].generateChart();
+            chart.draw(graphics, new Rectangle2D.Double(
+                0.0D, i*height,width,height));
+        }
+        graphics.finalize();
+
+        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);
+        }
+    }
+
+
     public static void exportSVG(
         OutputStream out,
         JFreeChart   chart,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java	Tue Mar 02 14:48:32 2010 +0000
@@ -46,6 +46,9 @@
     public static final String XPATH_OUTPUT_MODE =
         "/art:action/art:out/@name";
 
+    public static final String XPATH_EXPORT_MODE =
+        "/art:action/art:out/art:export/@name";
+
     public static final String XPATH_MIME_TYPE   =
         "/art:action/art:out/art:mime-type/@value";
 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Tue Mar 02 08:43:50 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Tue Mar 02 14:48:32 2010 +0000
@@ -218,6 +218,13 @@
         String outputMode = XMLUtils.xpathString(
             format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE);
 
+        String mode       = XMLUtils.xpathString(
+            format, XPATH_EXPORT_MODE, ArtifactNamespaceContext.INSTANCE);
+
+        if (mode == null || mode.equals("")) {
+            mode = "img";
+        }
+
         String mimeType = XMLUtils.xpathString(
             format, XPATH_MIME_TYPE, ArtifactNamespaceContext.INSTANCE);
 
@@ -241,7 +248,6 @@
                 while (it.hasNext()) {
                     InputData ip      = it.next();
                     String optionName = ip.getName().trim();
-                    log.debug("FOUND [" + optionName + "] with value {"+ ip.getValue()+"}");
                     requestParameter.put(optionName, ip.getValue());
 
                     if (optionName.equals("width")) {
@@ -261,65 +267,90 @@
         }
 
         try {
+            Collection<KeyValueDescibeData> parameters   =
+                getParameters(uuid);
+            Collection<KeyValueDescibeData> measurements =
+                getMeasurements(uuid);
+            Collection<KeyValueDescibeData> dates        =
+                getDates(uuid);
+
+            Locale[] serverLocales =
+                RessourceFactory.getInstance().getLocales();
+            Locale locale          =
+                callMeta.getPreferredLocale(serverLocales);
+
+            ChartLabels chartLables = createChartLabels(locale, uuid);
+
+            log.debug(
+                "Best locale - regarding intersection of server and " +
+                "browser locales -  is " + locale.toString()
+            );
+
+            String exportFormat = getExportFormat(mimeType);
+
+            // CHART
             if (outputMode.equalsIgnoreCase("chart")) {
                 log.debug("Chart will be generated.");
 
-                PreferredLocale[] locales = callMeta.getLanguages();
-                Locale[] serverLocales    =
-                    RessourceFactory.getInstance().getLocales();
-                Locale locale             =
-                    callMeta.getPreferredLocale(serverLocales);
-
-                log.debug(
-                    "Best locale - regarding intersection of server and " +
-                    "browser locales -  is " + locale.toString()
-                );
-
-                Collection parameters   = this.getParameters(uuid);
-                Collection measurements = this.getMeasurements(uuid);
-                Collection dates        = this.getDates(uuid);
-
-                ChartLabels chartLables = createChartLabels(locale, uuid);
-
-                String exportFormat = getExportFormat(mimeType);
+                if (mode.equalsIgnoreCase("img")) {
+                    createChart(
+                        outputStream,
+                        parameters,
+                        measurements,
+                        dates,
+                        chartLables,
+                        callContext,
+                        uuid,
+                        exportFormat,
+                        locale,
+                        chartWidth,
+                        chartHeight,
+                        lVisible,
+                        sVisible,
+                        callContext
+                    );
+                }
+                else if (mode.equalsIgnoreCase("pdf")) {
+                    createPDF(
+                        outputStream,
+                        parameters,
+                        measurements,
+                        dates,
+                        chartLables,
+                        uuid,
+                        "A4",
+                        true,
+                        lVisible,
+                        sVisible,
+                        locale,
+                        callContext
+                    );
+                }
+                else if (mode.equalsIgnoreCase("svg")) {
+                    createSVG(
+                        outputStream,
+                        getParameters(uuid),
+                        getMeasurements(uuid),
+                        getDates(uuid),
+                        createChartLabels(locale, uuid),
+                        uuid,
+                        locale,
+                        chartWidth,
+                        chartHeight,
+                        lVisible,
+                        sVisible,
+                        callContext
+                    );
+                }
+            }
+            // HISTOGRAM
+            else if (outputMode.equalsIgnoreCase("histogram")) {
+                Collection results = (Collection) getChartResult(uuid, callContext);
+                requestParameter.put("locale", locale);
 
-                this.createChart(
-                    outputStream,
-                    parameters,
-                    measurements,
-                    dates,
-                    chartLables,
-                    callContext,
-                    uuid,
-                    exportFormat,
-                    locale,
-                    chartWidth,
-                    chartHeight,
-                    lVisible,
-                    sVisible,
-                    callContext
-                );
-            }
-            else if (outputMode.equalsIgnoreCase("histogram")) {
-                log.debug("Create histogram.");
-
-                Collection results      = (Collection)
-                    getChartResult(uuid, callContext);
-
-                String exportFormat = getExportFormat(mimeType);
-                Collection<KeyValueDescibeData> parameters = getParameters(uuid);
-                Collection<KeyValueDescibeData> measurements = getMeasurements(uuid);
-                Collection<KeyValueDescibeData> dates        = getDates(uuid);
                 Object[][] data = HistogramHelper.prepareHistogramData(
                     results, parameters, measurements, dates);
 
-                PreferredLocale[] locales = callMeta.getLanguages();
-                Locale[] serverLocales    =
-                    RessourceFactory.getInstance().getLocales();
-                Locale locale             =
-                    callMeta.getPreferredLocale(serverLocales);
-                requestParameter.put("locale", locale);
-
                 int size           = data.length;
                 Chart[] histograms = new Chart[size];
 
@@ -333,69 +364,27 @@
                         labels, data[i], theme, requestParameter);
                 }
 
-                ChartExportHelper.exportHistograms(
-                    outputStream,
-                    histograms,
-                    exportFormat,
-                    chartWidth,
-                    chartHeight
-                );
-            }
-            else if (outputMode.equalsIgnoreCase("pdf")) {
-                log.debug("Output mode == pdf");
-
-                Locale[] serverLocales    =
-                    RessourceFactory.getInstance().getLocales();
-                Locale locale             =
-                    callMeta.getPreferredLocale(serverLocales);
-
-                log.debug(
-                    "Best locale - regarding intersection of server and " +
-                    "browser locales -  is " + locale.toString()
-                );
-
-                createPDF(
-                    outputStream,
-                    getParameters(uuid),
-                    getMeasurements(uuid),
-                    getDates(uuid),
-                    createChartLabels(locale, uuid),
-                    uuid,
-                    "A4",
-                    true,
-                    lVisible,
-                    sVisible,
-                    locale,
-                    callContext
-                );
-            }
-            else if (outputMode.equalsIgnoreCase("svg")) {
-                log.debug("Output mode == svg");
-
-                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),
-                    createChartLabels(locale, uuid),
-                    uuid,
-                    locale,
-                    chartWidth,
-                    chartHeight,
-                    lVisible,
-                    sVisible,
-                    callContext
-                );
+                if (mode.equalsIgnoreCase("img")) {
+                    ChartExportHelper.exportHistograms(
+                        outputStream,
+                        histograms,
+                        exportFormat,
+                        chartWidth,
+                        chartHeight
+                    );
+                }
+                else if (mode.equalsIgnoreCase("pdf")) {
+                    log.info("not implemented yet.");
+                }
+                else if (mode.equalsIgnoreCase("svg")) {
+                    ChartExportHelper.exportHistogramsAsSVG(
+                        outputStream,
+                        histograms,
+                        null,
+                        chartWidth,
+                        chartHeight
+                    );
+                }
             }
             else if (outputMode.equalsIgnoreCase("csv")) {
                 log.debug("CSV-File will be generated.");
@@ -414,15 +403,6 @@
                 Object     result = getChartResult(uuid, callContext);
 
                 if (result != null && s != null) {
-                    Collection<KeyValueDescibeData> parameters = 
-                        getParameters(uuid);
-
-                    Collection<KeyValueDescibeData> measurements = 
-                        getMeasurements(uuid);
-
-                    Collection<KeyValueDescibeData> dates = 
-                         getDates(uuid);
-
                     statistics = s.calculateStatistics(
                         result,
                         parameters,

http://dive4elements.wald.intevation.org