comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java @ 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 8428de5846e8
children 4ac581062c40
comparison
equal deleted inserted replaced
2230:59af81364eb1 2231:5648b5b34ae2
29 import java.io.UnsupportedEncodingException; 29 import java.io.UnsupportedEncodingException;
30 import org.jfree.chart.ChartRenderingInfo; 30 import org.jfree.chart.ChartRenderingInfo;
31 31
32 import javax.imageio.ImageIO; 32 import javax.imageio.ImageIO;
33 33
34 import au.com.bytecode.opencsv.CSVWriter;
35
34 import org.apache.batik.svggen.SVGGraphics2D; 36 import org.apache.batik.svggen.SVGGraphics2D;
35 import org.apache.batik.svggen.SVGGraphics2DIOException; 37 import org.apache.batik.svggen.SVGGraphics2DIOException;
36 38
37 import org.apache.log4j.Logger; 39 import org.apache.log4j.Logger;
38 40
39 import org.jfree.chart.JFreeChart; 41 import org.jfree.chart.JFreeChart;
42 import org.jfree.chart.plot.XYPlot;
43 import org.jfree.data.xy.XYDataset;
40 44
41 import de.intevation.artifacts.CallContext; 45 import de.intevation.artifacts.CallContext;
42 46
43 import de.intevation.artifacts.common.utils.XMLUtils; 47 import de.intevation.artifacts.common.utils.XMLUtils;
44 48
55 59
56 public static final String FORMAT_PDF = "pdf"; 60 public static final String FORMAT_PDF = "pdf";
57 61
58 public static final String FORMAT_SVG = "svg"; 62 public static final String FORMAT_SVG = "svg";
59 63
64 public static final String FORMAT_CSV = "csv";
60 65
61 /** 66 /**
62 * Constant field to define A4 as default page size. 67 * Constant field to define A4 as default page size.
63 */ 68 */
64 public static final String DEFAULT_PAGE_SIZE = "A4"; 69 public static final String DEFAULT_PAGE_SIZE = "A4";
65 70
66 /** 71 /**
67 * Constant field to define UTF-8 as default encoding. 72 * Constant field to define UTF-8 as default encoding.
68 */ 73 */
69 public static final String DEFAULT_ENCODING = "UTF-8"; 74 public static final String DEFAULT_ENCODING = "UTF-8";
75
76 /** The default separator for the CSV export. */
77 public static final char DEFAULT_CSV_SEPARATOR = ',';
78
70 79
71 /** 80 /**
72 * Logger used for logging with log4j. 81 * Logger used for logging with log4j.
73 */ 82 */
74 private static Logger log = Logger.getLogger(ChartExportHelper.class); 83 private static Logger log = Logger.getLogger(ChartExportHelper.class);
268 catch (DocumentException de) { 277 catch (DocumentException de) {
269 log.error("Error while exporting chart to pdf.", de); 278 log.error("Error while exporting chart to pdf.", de);
270 } 279 }
271 finally { 280 finally {
272 document.close(); 281 document.close();
282 }
283 }
284
285
286 /**
287 * A method to export a CSV file to an
288 * <code>OutputStream</code>.
289 *
290 * @param out OutputStream
291 * @param chart JFreeChart containing the data.
292 * @param context The CallContext object that contains extra parameters.
293 */
294 public static void exportCSV(
295 OutputStream out,
296 JFreeChart chart,
297 CallContext context)
298 {
299 log.debug("export chart as CSV");
300 CSVWriter writer = null;
301 try {
302 writer = new CSVWriter(
303 new OutputStreamWriter(
304 out,
305 DEFAULT_ENCODING),
306 DEFAULT_CSV_SEPARATOR);
307 }
308 catch(UnsupportedEncodingException uee) {
309 log.warn("Wrong encoding for CSV export.");
310 return;
311 }
312 XYPlot plot = chart.getXYPlot();
313 int count = plot.getDatasetCount();
314 for (int i = 0; i < count; i++) {
315 XYDataset data = plot.getDataset(i);
316 int scount = data.getSeriesCount();
317 for (int j = 0; j < scount; j++) {
318 Comparable seriesKey = data.getSeriesKey(j);
319 log.debug("series key: " + seriesKey.toString());
320 writeCSVHeader(writer, seriesKey.toString());
321 writeCSVData(writer, data);
322 }
323 }
324 try {
325 writer.close();
326 }
327 catch(IOException ioe) {
328 log.error("Writing CSV export failed!");
329 }
330 }
331
332
333 protected static void writeCSVHeader(CSVWriter writer, String key) {
334 writer.writeNext(new String[] {"#"});
335 writer.writeNext(new String[] {"# " + key});
336 writer.writeNext(new String[] {"#"});
337 writer.writeNext(new String[] {"X", "Y"});
338 }
339
340
341 protected static void writeCSVData(CSVWriter writer, XYDataset data) {
342 int series = data.getSeriesCount();
343 for (int i = 0; i < series; i++) {
344 int items = data.getItemCount(i);
345 for (int j = 0; j < items; j++) {
346 log.debug("write data: " + data.getX(i, j) + ", " + data.getY(i, j));
347 writer.writeNext(new String[] {
348 data.getX(i, j).toString(),
349 data.getY(i, j).toString()});
350 }
273 } 351 }
274 } 352 }
275 353
276 354
277 public static int[] getSize(CallContext cc) { 355 public static int[] getSize(CallContext cc) {

http://dive4elements.wald.intevation.org