Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 320:18302372703f
Implemented pdf export of charts.
gnv-artifacts/trunk@381 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 30 Nov 2009 09:51:59 +0000 |
parents | 5f2820e821e0 |
children | a4376fd23f99 |
comparison
equal
deleted
inserted
replaced
319:251f16a083f8 | 320:18302372703f |
---|---|
1 package de.intevation.gnv.exports; | 1 package de.intevation.gnv.exports; |
2 | 2 |
3 import com.lowagie.text.Document; | |
4 import com.lowagie.text.DocumentException; | |
5 import com.lowagie.text.PageSize; | |
6 import com.lowagie.text.Rectangle; | |
7 import com.lowagie.text.pdf.PdfContentByte; | |
8 import com.lowagie.text.pdf.PdfTemplate; | |
9 import com.lowagie.text.pdf.PdfWriter; | |
10 | |
3 import java.awt.Transparency; | 11 import java.awt.Transparency; |
4 import java.awt.image.BufferedImage; | 12 import java.awt.Graphics2D; |
13 import java.awt.geom.Rectangle2D; | |
14 import java.awt.geom.Rectangle2D.Double; | |
5 import java.io.OutputStream; | 15 import java.io.OutputStream; |
6 import java.io.IOException; | 16 import java.io.IOException; |
7 import javax.imageio.ImageIO; | 17 import javax.imageio.ImageIO; |
8 | 18 |
9 import org.apache.log4j.Logger; | 19 import org.apache.log4j.Logger; |
10 | 20 |
11 import org.jfree.chart.JFreeChart; | 21 import org.jfree.chart.JFreeChart; |
12 | 22 |
23 | |
13 /** | 24 /** |
14 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | 25 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> |
15 */ | 26 */ |
16 public class ChartExportHelper { | 27 public class ChartExportHelper { |
17 | 28 |
18 private static final String DEFAULT_PAGE_SIZE = "A4"; | 29 private static final String DEFAULT_PAGE_SIZE = "A4"; |
19 | 30 |
20 private static Logger log = Logger.getLogger(ChartExportHelper.class); | 31 private static Logger log = Logger.getLogger(ChartExportHelper.class); |
21 | 32 |
22 public static void exportImage( | 33 public static void exportImage( |
23 OutputStream out, | 34 OutputStream out, |
37 format, | 48 format, |
38 out | 49 out |
39 ); | 50 ); |
40 } | 51 } |
41 | 52 |
53 | |
42 public static void exportSVG() { | 54 public static void exportSVG() { |
43 log.info("export chart as svg (not implemented yet)"); | 55 log.info("export chart as svg (not implemented yet)"); |
44 } | 56 } |
45 | 57 |
46 public static void exportPDF() { | 58 |
47 log.info("export chart as pdf (not implemented yet)"); | 59 public static void exportPDF( |
60 OutputStream out, | |
61 JFreeChart chart, | |
62 String pageFormat, | |
63 boolean landscape, | |
64 float marginLeft, | |
65 float marginRight, | |
66 float marginTop, | |
67 float marginBottom | |
68 ) { | |
69 log.info("export chart as pdf."); | |
70 | |
71 if (pageFormat == null) | |
72 pageFormat = DEFAULT_PAGE_SIZE; | |
73 | |
74 Rectangle page = PageSize.getRectangle(pageFormat); | |
75 int pageWidth = (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); | |
76 int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom)); | |
77 | |
78 Document document = null; | |
79 if (landscape) { | |
80 document = new Document(page.rotate()); | |
81 log.debug("Create landscape pdf."); | |
82 } | |
83 else | |
84 document = new Document(page); | |
85 | |
86 try { | |
87 PdfWriter writer = PdfWriter.getInstance(document, out); | |
88 | |
89 document.addSubject(chart.getTitle().getText()); | |
90 document.addCreationDate(); | |
91 document.open(); | |
92 | |
93 PdfContentByte content = writer.getDirectContent(); | |
94 | |
95 int width = 0; | |
96 int height = 0; | |
97 if (landscape) { | |
98 width = pageHeight; | |
99 height = pageWidth; | |
100 } | |
101 else { | |
102 width = pageWidth; | |
103 height = pageHeight; | |
104 } | |
105 | |
106 PdfTemplate template = content.createTemplate(width, height); | |
107 Graphics2D graphics = template.createGraphics(width, height); | |
108 Rectangle2D area = new Rectangle2D.Double(0.0D, 0.0D,width,height); | |
109 | |
110 chart.draw(graphics, area); | |
111 graphics.dispose(); | |
112 content.addTemplate(template, marginLeft, marginBottom); | |
113 } | |
114 catch (DocumentException de) { | |
115 log.error("Error while exporting chart to pdf.", de); | |
116 } | |
117 finally { | |
118 document.close(); | |
119 } | |
48 } | 120 } |
49 } | 121 } |
122 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |