Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java @ 1769:bbe12d77710d
Prepare rendering of added other (static) WKms and Differences in LongitudinalSection Diagrams.
flys-artifacts/trunk@3090 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 26 Oct 2011 12:54:22 +0000 |
parents | 5966a20fc72c |
children | 8428de5846e8 |
rev | line source |
---|---|
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
1 /* |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
2 * Copyright (c) 2011 by Intevation GmbH |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
3 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
7 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 package de.intevation.flys.exports; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
10 import com.lowagie.text.Document; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
11 import com.lowagie.text.DocumentException; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
12 import com.lowagie.text.PageSize; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 import com.lowagie.text.Rectangle; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
15 import com.lowagie.text.pdf.PdfContentByte; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
16 import com.lowagie.text.pdf.PdfTemplate; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
17 import com.lowagie.text.pdf.PdfWriter; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
18 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 import java.awt.Graphics2D; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 import java.awt.Transparency; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 import java.awt.geom.Rectangle2D.Double; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
24 import java.awt.geom.Rectangle2D; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 import java.io.IOException; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
27 import java.io.OutputStream; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
28 import java.io.OutputStreamWriter; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
29 import java.io.UnsupportedEncodingException; |
1036
e6aff80b59ff
Added proof-of-concept collision detection for text annotations.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
714
diff
changeset
|
30 import org.jfree.chart.ChartRenderingInfo; |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
31 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
32 import javax.imageio.ImageIO; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
33 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
34 import org.apache.batik.svggen.SVGGraphics2D; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
35 import org.apache.batik.svggen.SVGGraphics2DIOException; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
37 import org.apache.log4j.Logger; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
38 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
39 import org.jfree.chart.JFreeChart; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
40 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
41 import de.intevation.artifacts.CallContext; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
42 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
43 import de.intevation.artifacts.common.utils.XMLUtils; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
44 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
46 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
47 * This class is a helper class which supports some methods to export charts |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
48 * into specific formats. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
49 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
50 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
51 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 public class ChartExportHelper { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
53 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
54 public static final String FORMAT_PNG = "png"; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
55 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
56 public static final String FORMAT_PDF = "pdf"; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
57 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
58 public static final String FORMAT_SVG = "svg"; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
59 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
60 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
61 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
62 * Constant field to define A4 as default page size. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
63 */ |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
64 public static final String DEFAULT_PAGE_SIZE = "A4"; |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
65 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
66 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
67 * Constant field to define UTF-8 as default encoding. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
68 */ |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
69 public static final String DEFAULT_ENCODING = "UTF-8"; |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
70 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
71 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
72 * Logger used for logging with log4j. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
73 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
74 private static Logger log = Logger.getLogger(ChartExportHelper.class); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
75 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
76 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
77 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
78 * A method to export a <code>JFreeChart</code> as image to an |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
79 * <code>OutputStream</code> with a given format, width and height. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
80 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
81 * @param out OutputStream |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
82 * @param chart JFreeChart object to be exported. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
83 * @param format Format (e.g. png, gif, jpg) |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
84 * @param width Width, the image used to be |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
85 * @param height Height, the image used to be |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
86 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
87 * @throws IOException if writing image to OutputStream failed. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
88 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
89 public static void exportImage( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
90 OutputStream out, |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
91 JFreeChart chart, |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
92 CallContext cc |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
93 ) |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
94 throws IOException |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
95 { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
96 log.info("export chart as png"); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
97 |
1036
e6aff80b59ff
Added proof-of-concept collision detection for text annotations.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
714
diff
changeset
|
98 ChartRenderingInfo info = new ChartRenderingInfo(); |
e6aff80b59ff
Added proof-of-concept collision detection for text annotations.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
714
diff
changeset
|
99 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
100 String format = (String) cc.getContextValue("chart.image.format"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
101 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
102 int[] size = getSize(cc); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
103 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
104 ImageIO.write( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
105 chart.createBufferedImage( |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
106 size[0], size[1], Transparency.BITMASK, info |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
107 ), |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
108 format, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
109 out |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
110 ); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
111 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
112 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
113 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
114 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
115 * A method to export a <code>JFreeChart</code> as SVG to an |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
116 * <code>OutputStream</code>. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
117 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
118 * @param out OutputStream |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
119 * @param chart JFreeChart to be exported |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
120 * @param context The CallContext object that contains extra chart |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
121 * parameters. |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
122 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
123 public static void exportSVG( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
124 OutputStream out, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
125 JFreeChart chart, |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
126 CallContext context |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
127 ) { |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
128 String encoding = (String) context.getContextValue("chart.encoding"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
129 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
130 log.info("export chart as svg"); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
131 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
132 if (encoding == null) |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
133 encoding = DEFAULT_ENCODING; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
134 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
135 org.w3c.dom.Document document = XMLUtils.newDocument(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
136 SVGGraphics2D graphics = new SVGGraphics2D(document); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
137 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
138 int[] size = getSize(context); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
139 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
140 chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,size[0],size[1])); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
141 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
142 try { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
143 graphics.stream(new OutputStreamWriter(out, encoding)); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
144 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
145 catch (SVGGraphics2DIOException svge) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
146 log.error("Error while writing svg export to output stream.", svge); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
147 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
148 catch (UnsupportedEncodingException uee) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
149 log.error("Unsupported encoding: " + encoding, uee); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
150 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
151 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
152 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
153 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
154 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
155 * A method to export a <code>JFreeChart</code> as PDF to an |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
156 * <code>OutputStream</code>. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
157 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
158 * @param out OutputStream |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
159 * @param chart JFreeChart |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
160 * @param pageFormat String to specify a page format, {@link |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
161 * #DEFAULT_PAGE_SIZE} is used if no pageFormat is given |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
162 * @param landscape If this is true, the pdf is delivered in landscape |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
163 * format |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
164 * @param marginLeft Space to left border |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
165 * @param marginRight Space to right border |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
166 * @param marginTop Space to upper border |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
167 * @param marginBottom Space to lower border |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
168 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
169 public static void exportPDF( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
170 OutputStream out, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
171 JFreeChart chart, |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
172 CallContext cc |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
173 ) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
174 log.info("export chart as pdf."); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
175 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
176 String pageFormat = (String) cc.getContextValue("chart.page.format"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
177 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
178 if (pageFormat == null) |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
179 pageFormat = DEFAULT_PAGE_SIZE; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
180 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
181 // max size of the chart |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
182 Rectangle page = PageSize.getRectangle(pageFormat); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
183 float pageWidth = page.getWidth(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
184 float pageHeight = page.getHeight(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
185 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
186 // the chart width |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
187 int[] size = getSize(cc); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
188 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
189 boolean landscape = size[0] > size[1]; |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
190 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
191 float width = 0; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
192 float height = 0; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
193 if (landscape) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
194 width = pageHeight; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
195 height = pageWidth; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
196 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
197 else { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
198 width = pageWidth; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
199 height = pageHeight; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
200 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
201 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
202 float marginLeft = (Float) cc.getContextValue( |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
203 "chart.marginLeft"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
204 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
205 float marginRight = (Float) cc.getContextValue( |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
206 "chart.marginRight"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
207 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
208 float marginTop = (Float) cc.getContextValue( |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
209 "chart.marginTop"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
210 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
211 float marginBottom = (Float) cc.getContextValue( |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
212 "chart.marginBottom"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
213 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
214 float spaceX = width - marginLeft - marginRight; |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
215 if (size[0] > spaceX) { |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
216 log.warn("Width of the chart is too big for pdf -> resize it now."); |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
217 double ratio = ((double)spaceX) / size[0]; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
218 size[0] *= ratio; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
219 size[1] *= ratio; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
220 log.debug("Resized chart to " + size[0] + "x" + size[1]); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
221 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
222 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
223 float spaceY = height - marginTop - marginBottom; |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
224 if (size[1] > spaceY) { |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
225 log.warn("Height of the chart is too big for pdf -> resize it now."); |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
226 double ratio = ((double)spaceY) / size[1]; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
227 size[0] *= ratio; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
228 size[1] *= ratio; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
229 log.debug("Resized chart to " + size[0] + "x" + size[1]); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
230 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
231 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
232 Document document = null; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
233 if (landscape) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
234 document = new Document(page.rotate()); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
235 log.debug("Create landscape pdf."); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
236 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
237 else |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
238 document = new Document(page); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
239 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
240 try { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
241 PdfWriter writer = PdfWriter.getInstance(document, out); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
242 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
243 document.addSubject(chart.getTitle().getText()); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
244 document.addCreationDate(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
245 document.open(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
246 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
247 PdfContentByte content = writer.getDirectContent(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
248 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
249 PdfTemplate template = content.createTemplate(width, height); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
250 Graphics2D graphics = template.createGraphics(width, height); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
251 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
252 double[] origin = getCenteredAnchor( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
253 marginLeft, marginRight, marginBottom, marginTop, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
254 width, height, |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
255 size[0], size[1]); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
256 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
257 Rectangle2D area = new Rectangle2D.Double( |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
258 origin[0], origin[1], size[0], size[1]); |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
259 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
260 chart.draw(graphics, area); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
261 graphics.dispose(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
262 content.addTemplate(template, 0f, 0f); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
263 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
264 catch (DocumentException de) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
265 log.error("Error while exporting chart to pdf.", de); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
266 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
267 finally { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
268 document.close(); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
269 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
270 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
271 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
272 |
1735
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
273 public static int[] getSize(CallContext cc) { |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
274 int[] size = new int[2]; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
275 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
276 size[0] = (Integer) cc.getContextValue("chart.width"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
277 size[1] = (Integer) cc.getContextValue("chart.height"); |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
278 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
279 return size; |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
280 } |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
281 |
5966a20fc72c
Enabled support for PDF and SVG chart exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1036
diff
changeset
|
282 |
152
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
283 /** |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
284 * This method returns the anchor of the chart so that the chart is centered |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
285 * according to the given parameters. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
286 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
287 * @param mLeft Left margin |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
288 * @param mRight Right margin |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
289 * @param mBottom Bottom margin |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
290 * @param mTop Top margin |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
291 * @param width The complete width of the drawing area. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
292 * @param height The complete height of the drawing area. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
293 * @param chartWidth The width of the chart. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
294 * @param chartHeight The height of the chart. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
295 * |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
296 * @return an array that contains the anchor for a chart with the given |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
297 * parameters. The first value is the x point, the second value is the y |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
298 * point. |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
299 */ |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
300 public static double[] getCenteredAnchor( |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
301 double mLeft, double mRight, double mBottom, double mTop, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
302 double width, double height, |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
303 double chartWidth, double chartHeight |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
304 ) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
305 if (log.isDebugEnabled()) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
306 log.debug("Calculate centered origin..."); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
307 log.debug("-> PDF width : " + width); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
308 log.debug("-> PDF height : " + height); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
309 log.debug("-> Chart width : " + chartWidth); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
310 log.debug("-> Chart height : " + chartHeight); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
311 log.debug("-> margin left : " + mLeft); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
312 log.debug("-> margin right : " + mRight); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
313 log.debug("-> margin bottom: " + mBottom); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
314 log.debug("-> margin top : " + mTop); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
315 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
316 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
317 double[] origin = new double[2]; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
318 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
319 double centerX = width / 2; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
320 double centerY = height / 2; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
321 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
322 origin[0] = centerX - chartWidth / 2; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
323 origin[1] = centerY - chartHeight / 2; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
324 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
325 origin[0] = origin[0] >= mLeft ? origin[0] : mLeft; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
326 origin[1] = origin[1] >= mTop ? origin[1] : mTop; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
327 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
328 if (log.isDebugEnabled()) { |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
329 log.debug("==> centered left origin: " + origin[0]); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
330 log.debug("==> centered top origin: " + origin[1]); |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
331 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
332 |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
333 return origin; |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
334 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
335 } |
956445dd601d
Added a helper class to export charts.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
336 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |