Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java @ 1735:5966a20fc72c
Enabled support for PDF and SVG chart exports.
flys-artifacts/trunk@3023 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 19 Oct 2011 09:43:04 +0000 |
parents | e6aff80b59ff |
children | 8428de5846e8 |
comparison
equal
deleted
inserted
replaced
1734:25d481cad4fb | 1735:5966a20fc72c |
---|---|
49 * | 49 * |
50 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 50 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
51 */ | 51 */ |
52 public class ChartExportHelper { | 52 public class ChartExportHelper { |
53 | 53 |
54 public static final String FORMAT_PNG = "png"; | |
55 | |
56 public static final String FORMAT_PDF = "pdf"; | |
57 | |
58 public static final String FORMAT_SVG = "svg"; | |
59 | |
60 | |
54 /** | 61 /** |
55 * Constant field to define A4 as default page size. | 62 * Constant field to define A4 as default page size. |
56 */ | 63 */ |
57 private static final String DEFAULT_PAGE_SIZE = "A4"; | 64 public static final String DEFAULT_PAGE_SIZE = "A4"; |
58 | 65 |
59 /** | 66 /** |
60 * Constant field to define UTF-8 as default encoding. | 67 * Constant field to define UTF-8 as default encoding. |
61 */ | 68 */ |
62 private static final String DEFAULT_ENCODING = "UTF-8"; | 69 public static final String DEFAULT_ENCODING = "UTF-8"; |
63 | 70 |
64 /** | 71 /** |
65 * Logger used for logging with log4j. | 72 * Logger used for logging with log4j. |
66 */ | 73 */ |
67 private static Logger log = Logger.getLogger(ChartExportHelper.class); | 74 private static Logger log = Logger.getLogger(ChartExportHelper.class); |
79 * | 86 * |
80 * @throws IOException if writing image to OutputStream failed. | 87 * @throws IOException if writing image to OutputStream failed. |
81 */ | 88 */ |
82 public static void exportImage( | 89 public static void exportImage( |
83 OutputStream out, | 90 OutputStream out, |
84 JFreeChart chart, | 91 JFreeChart chart, |
85 String format, | 92 CallContext cc |
86 int width, | |
87 int height | |
88 ) | 93 ) |
89 throws IOException | 94 throws IOException |
90 { | 95 { |
91 log.info("export chart as png"); | 96 log.info("export chart as png"); |
92 | 97 |
93 ChartRenderingInfo info = new ChartRenderingInfo(); | 98 ChartRenderingInfo info = new ChartRenderingInfo(); |
94 | 99 |
100 String format = (String) cc.getContextValue("chart.image.format"); | |
101 | |
102 int[] size = getSize(cc); | |
103 | |
95 ImageIO.write( | 104 ImageIO.write( |
96 chart.createBufferedImage( | 105 chart.createBufferedImage( |
97 width, height, Transparency.BITMASK, info | 106 size[0], size[1], Transparency.BITMASK, info |
98 ), | 107 ), |
99 format, | 108 format, |
100 out | 109 out |
101 ); | 110 ); |
102 } | 111 } |
106 * A method to export a <code>JFreeChart</code> as SVG to an | 115 * A method to export a <code>JFreeChart</code> as SVG to an |
107 * <code>OutputStream</code>. | 116 * <code>OutputStream</code>. |
108 * | 117 * |
109 * @param out OutputStream | 118 * @param out OutputStream |
110 * @param chart JFreeChart to be exported | 119 * @param chart JFreeChart to be exported |
111 * @param encoding Encoding, defaults to {@link #DEFAULT_ENCODING} if null | 120 * @param context The CallContext object that contains extra chart |
112 * @param width Width the svg used to be | 121 * parameters. |
113 * @param height Height the svg used to be | |
114 */ | 122 */ |
115 public static void exportSVG( | 123 public static void exportSVG( |
116 OutputStream out, | 124 OutputStream out, |
117 JFreeChart chart, | 125 JFreeChart chart, |
118 String encoding, | 126 CallContext context |
119 int width, | |
120 int height | |
121 ) { | 127 ) { |
128 String encoding = (String) context.getContextValue("chart.encoding"); | |
129 | |
122 log.info("export chart as svg"); | 130 log.info("export chart as svg"); |
123 | 131 |
124 if (encoding == null) | 132 if (encoding == null) |
125 encoding = DEFAULT_ENCODING; | 133 encoding = DEFAULT_ENCODING; |
126 | 134 |
127 org.w3c.dom.Document document = XMLUtils.newDocument(); | 135 org.w3c.dom.Document document = XMLUtils.newDocument(); |
128 SVGGraphics2D graphics = new SVGGraphics2D(document); | 136 SVGGraphics2D graphics = new SVGGraphics2D(document); |
129 | 137 |
130 chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,width,height)); | 138 int[] size = getSize(context); |
139 | |
140 chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,size[0],size[1])); | |
131 | 141 |
132 try { | 142 try { |
133 graphics.stream(new OutputStreamWriter(out, encoding)); | 143 graphics.stream(new OutputStreamWriter(out, encoding)); |
134 } | 144 } |
135 catch (SVGGraphics2DIOException svge) { | 145 catch (SVGGraphics2DIOException svge) { |
157 * @param marginBottom Space to lower border | 167 * @param marginBottom Space to lower border |
158 */ | 168 */ |
159 public static void exportPDF( | 169 public static void exportPDF( |
160 OutputStream out, | 170 OutputStream out, |
161 JFreeChart chart, | 171 JFreeChart chart, |
162 String pageFormat, | 172 CallContext cc |
163 float marginLeft, | |
164 float marginRight, | |
165 float marginTop, | |
166 float marginBottom, | |
167 CallContext context | |
168 ) { | 173 ) { |
169 log.info("export chart as pdf."); | 174 log.info("export chart as pdf."); |
175 | |
176 String pageFormat = (String) cc.getContextValue("chart.page.format"); | |
170 | 177 |
171 if (pageFormat == null) | 178 if (pageFormat == null) |
172 pageFormat = DEFAULT_PAGE_SIZE; | 179 pageFormat = DEFAULT_PAGE_SIZE; |
173 | 180 |
174 // max size of the chart | 181 // max size of the chart |
175 Rectangle page = PageSize.getRectangle(pageFormat); | 182 Rectangle page = PageSize.getRectangle(pageFormat); |
176 float pageWidth = page.getWidth(); | 183 float pageWidth = page.getWidth(); |
177 float pageHeight = page.getHeight(); | 184 float pageHeight = page.getHeight(); |
178 | 185 |
179 // the chart width | 186 // the chart width |
180 int chartWidth = (Integer) context.getContextValue("chart.width"); | 187 int[] size = getSize(cc); |
181 int chartHeight = (Integer) context.getContextValue("chart.height"); | 188 |
182 | 189 boolean landscape = size[0] > size[1]; |
183 boolean landscape = chartWidth > chartHeight; | |
184 | 190 |
185 float width = 0; | 191 float width = 0; |
186 float height = 0; | 192 float height = 0; |
187 if (landscape) { | 193 if (landscape) { |
188 width = pageHeight; | 194 width = pageHeight; |
191 else { | 197 else { |
192 width = pageWidth; | 198 width = pageWidth; |
193 height = pageHeight; | 199 height = pageHeight; |
194 } | 200 } |
195 | 201 |
202 float marginLeft = (Float) cc.getContextValue( | |
203 "chart.marginLeft"); | |
204 | |
205 float marginRight = (Float) cc.getContextValue( | |
206 "chart.marginRight"); | |
207 | |
208 float marginTop = (Float) cc.getContextValue( | |
209 "chart.marginTop"); | |
210 | |
211 float marginBottom = (Float) cc.getContextValue( | |
212 "chart.marginBottom"); | |
213 | |
196 float spaceX = width - marginLeft - marginRight; | 214 float spaceX = width - marginLeft - marginRight; |
197 if (chartWidth > spaceX) { | 215 if (size[0] > spaceX) { |
198 log.warn("Width of the chart is too big for pdf -> resize it now."); | 216 log.warn("Width of the chart is too big for pdf -> resize it now."); |
199 double ratio = ((double)spaceX) / chartWidth; | 217 double ratio = ((double)spaceX) / size[0]; |
200 chartWidth *= ratio; | 218 size[0] *= ratio; |
201 chartHeight *= ratio; | 219 size[1] *= ratio; |
202 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); | 220 log.debug("Resized chart to " + size[0] + "x" + size[1]); |
203 } | 221 } |
204 | 222 |
205 float spaceY = height - marginTop - marginBottom; | 223 float spaceY = height - marginTop - marginBottom; |
206 if (chartHeight > spaceY) { | 224 if (size[1] > spaceY) { |
207 log.warn("Height of the chart is too big for pdf -> resize it now."); | 225 log.warn("Height of the chart is too big for pdf -> resize it now."); |
208 double ratio = ((double)spaceY) / chartHeight; | 226 double ratio = ((double)spaceY) / size[1]; |
209 chartWidth *= ratio; | 227 size[0] *= ratio; |
210 chartHeight *= ratio; | 228 size[1] *= ratio; |
211 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); | 229 log.debug("Resized chart to " + size[0] + "x" + size[1]); |
212 } | 230 } |
213 | 231 |
214 Document document = null; | 232 Document document = null; |
215 if (landscape) { | 233 if (landscape) { |
216 document = new Document(page.rotate()); | 234 document = new Document(page.rotate()); |
232 Graphics2D graphics = template.createGraphics(width, height); | 250 Graphics2D graphics = template.createGraphics(width, height); |
233 | 251 |
234 double[] origin = getCenteredAnchor( | 252 double[] origin = getCenteredAnchor( |
235 marginLeft, marginRight, marginBottom, marginTop, | 253 marginLeft, marginRight, marginBottom, marginTop, |
236 width, height, | 254 width, height, |
237 chartWidth, chartHeight); | 255 size[0], size[1]); |
238 | 256 |
239 Rectangle2D area = new Rectangle2D.Double( | 257 Rectangle2D area = new Rectangle2D.Double( |
240 origin[0], origin[1], chartWidth, chartHeight); | 258 origin[0], origin[1], size[0], size[1]); |
241 | 259 |
242 chart.draw(graphics, area); | 260 chart.draw(graphics, area); |
243 graphics.dispose(); | 261 graphics.dispose(); |
244 content.addTemplate(template, 0f, 0f); | 262 content.addTemplate(template, 0f, 0f); |
245 } | 263 } |
247 log.error("Error while exporting chart to pdf.", de); | 265 log.error("Error while exporting chart to pdf.", de); |
248 } | 266 } |
249 finally { | 267 finally { |
250 document.close(); | 268 document.close(); |
251 } | 269 } |
270 } | |
271 | |
272 | |
273 public static int[] getSize(CallContext cc) { | |
274 int[] size = new int[2]; | |
275 | |
276 size[0] = (Integer) cc.getContextValue("chart.width"); | |
277 size[1] = (Integer) cc.getContextValue("chart.height"); | |
278 | |
279 return size; | |
252 } | 280 } |
253 | 281 |
254 | 282 |
255 /** | 283 /** |
256 * This method returns the anchor of the chart so that the chart is centered | 284 * This method returns the anchor of the chart so that the chart is centered |