comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 640:e5f1e868ee3e

Implementedr histogram pdf export. gnv-artifacts/trunk@726 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 03 Mar 2010 13:44:15 +0000
parents a94ed2755480
children a0e63136954e
comparison
equal deleted inserted replaced
639:a94ed2755480 640:e5f1e868ee3e
216 } 216 }
217 finally { 217 finally {
218 document.close(); 218 document.close();
219 } 219 }
220 } 220 }
221
222
223 public static void exportHistogramsAsPDF(
224 OutputStream out,
225 Chart[] histograms,
226 String pageFormat,
227 boolean landscape,
228 float marginLeft,
229 float marginRight,
230 float marginTop,
231 float marginBottom
232 ) {
233 log.info("export histogram as pdf.");
234
235 if (pageFormat == null)
236 pageFormat = DEFAULT_PAGE_SIZE;
237
238 Rectangle page = PageSize.getRectangle(pageFormat);
239 int pageWidth =
240 (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
241 int pageHeight =
242 (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
243
244 Document document = null;
245 if (landscape) {
246 document = new Document(page.rotate());
247 log.debug("Create landscape pdf.");
248 }
249 else
250 document = new Document(page);
251
252 try {
253 PdfWriter writer = PdfWriter.getInstance(document, out);
254
255 document.addCreationDate();
256 document.open();
257
258 PdfContentByte content = writer.getDirectContent();
259
260 int width = 0;
261 int height = 0;
262 if (landscape) {
263 width = pageHeight;
264 height = pageWidth;
265 }
266 else {
267 width = pageWidth;
268 height = pageHeight;
269 }
270
271 int size = histograms.length;
272 for (int i = 0; i < size; i++) {
273 if (i > 0) {
274 document.newPage();
275 }
276
277 JFreeChart chart = histograms[i].generateChart();
278 PdfTemplate template = content.createTemplate(width, height);
279 Graphics2D graphics = template.createGraphics(width, height);
280 Rectangle2D area = new Rectangle2D.Double(
281 0.0D, 0.0D,width,height);
282
283 chart.draw(graphics, area);
284 graphics.dispose();
285 content.addTemplate(template, marginLeft, marginBottom);
286 }
287 }
288 catch (DocumentException de) {
289 log.error("Error while exporting chart to pdf.", de);
290 }
291 finally {
292 document.close();
293 }
294 }
221 } 295 }
222 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 296 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org