changeset 2231:5648b5b34ae2

Issue 466. Added CSV export for activated chart themes excluding annotations. flys-artifacts/trunk@3874 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 02 Feb 2012 10:54:21 +0000
parents 59af81364eb1
children 78ac5058da67
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 3 files changed, 96 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Feb 01 15:41:11 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Feb 02 10:54:21 2012 +0000
@@ -1,3 +1,13 @@
+2012-02-02  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Issue 466: CSV export for chart themes.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Export a CSV file if the requested format is 'csv'.
+
+	* src/main/java/de/intevation/flys/exports/ChartExportHelper.java:
+	  Generate the CSV file containing X-Y-data of all activated themes.
+
 2012-02-01  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Wed Feb 01 15:41:11 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Thu Feb 02 10:54:21 2012 +0000
@@ -31,12 +31,16 @@
 
 import javax.imageio.ImageIO;
 
+import au.com.bytecode.opencsv.CSVWriter;
+
 import org.apache.batik.svggen.SVGGraphics2D;
 import org.apache.batik.svggen.SVGGraphics2DIOException;
 
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.data.xy.XYDataset;
 
 import de.intevation.artifacts.CallContext;
 
@@ -57,6 +61,7 @@
 
     public static final String FORMAT_SVG = "svg";
 
+    public static final String FORMAT_CSV = "csv";
 
     /**
      * Constant field to define A4 as default page size.
@@ -68,6 +73,10 @@
      */
     public static final String  DEFAULT_ENCODING  = "UTF-8";
 
+    /** The default separator for the CSV export. */
+    public static final char DEFAULT_CSV_SEPARATOR = ',';
+
+
     /**
      * Logger used for logging with log4j.
      */
@@ -274,6 +283,75 @@
     }
 
 
+    /**
+     * A method to export a CSV file to an
+     * <code>OutputStream</code>.
+     *
+     * @param out OutputStream
+     * @param chart JFreeChart containing the data.
+     * @param context The CallContext object that contains extra parameters.
+     */
+    public static void exportCSV(
+        OutputStream out,
+        JFreeChart chart,
+        CallContext context)
+    {
+        log.debug("export chart as CSV");
+        CSVWriter writer = null;
+        try {
+            writer = new CSVWriter(
+                new OutputStreamWriter(
+                    out,
+                    DEFAULT_ENCODING),
+                DEFAULT_CSV_SEPARATOR);
+        }
+        catch(UnsupportedEncodingException uee) {
+            log.warn("Wrong encoding for CSV export.");
+            return;
+        }
+        XYPlot plot = chart.getXYPlot();
+        int count = plot.getDatasetCount();
+        for (int i = 0; i < count; i++) {
+            XYDataset data = plot.getDataset(i);
+            int scount = data.getSeriesCount();
+            for (int j = 0; j < scount; j++) {
+                Comparable seriesKey = data.getSeriesKey(j);
+                log.debug("series key: " + seriesKey.toString());
+                writeCSVHeader(writer, seriesKey.toString());
+                writeCSVData(writer, data);
+            }
+        }
+        try {
+            writer.close();
+        }
+        catch(IOException ioe) {
+            log.error("Writing CSV export failed!");
+        }
+    }
+
+
+    protected static void writeCSVHeader(CSVWriter writer, String key) {
+        writer.writeNext(new String[] {"#"});
+        writer.writeNext(new String[] {"# " + key});
+        writer.writeNext(new String[] {"#"});
+        writer.writeNext(new String[] {"X", "Y"});
+    }
+
+
+    protected static void writeCSVData(CSVWriter writer, XYDataset data) {
+        int series = data.getSeriesCount();
+        for (int i = 0; i < series; i++) {
+            int items = data.getItemCount(i);
+            for (int j = 0; j < items; j++) {
+                log.debug("write data: " + data.getX(i, j) + ", " + data.getY(i, j));
+                writer.writeNext(new String[] {
+                    data.getX(i, j).toString(),
+                    data.getY(i, j).toString()});
+            }
+        }
+    }
+
+
     public static int[] getSize(CallContext cc) {
         int[] size = new int[2];
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Wed Feb 01 15:41:11 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Feb 02 10:54:21 2012 +0000
@@ -491,6 +491,14 @@
                 chart,
                 context);
         }
+        else if (format.equals(ChartExportHelper.FORMAT_CSV)) {
+            context.putContextValue("chart.image.format", "csv");
+
+            ChartExportHelper.exportCSV(
+                out,
+                chart,
+                context);
+        }
     }
 
 

http://dive4elements.wald.intevation.org