Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 1050:7f3154331bc1
Use the chart size/height to export it to pdf. The chart's aspect ratio keeps alive if the size exceeds the maximum page size (issue290).
gnv-artifacts/trunk@1124 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 25 May 2010 15:41:31 +0000 |
parents | 22c18083225e |
children | b30361464775 |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java Tue May 25 13:57:48 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java Tue May 25 15:41:31 2010 +0000 @@ -9,6 +9,8 @@ import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; +import de.intevation.artifacts.CallContext; + import de.intevation.artifactdatabase.XMLUtils; import de.intevation.gnv.chart.Chart; @@ -237,17 +239,50 @@ float marginLeft, float marginRight, float marginTop, - float marginBottom + float marginBottom, + CallContext context ) { log.info("export chart as pdf."); if (pageFormat == null) pageFormat = DEFAULT_PAGE_SIZE; + // max size of the chart Rectangle page = PageSize.getRectangle(pageFormat); int pageWidth = (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom)); + // the chart width + int chartWidth = (Integer) context.getContextValue("chart.width"); + int chartHeight = (Integer) context.getContextValue("chart.height"); + + int width = 0; + int height = 0; + if (landscape) { + width = pageHeight; + height = pageWidth; + } + else { + width = pageWidth; + height = pageHeight; + } + + if (chartWidth > width) { + log.warn("Width of the chart is too big for pdf -> resize it now."); + double ratio = ((double)width) / chartWidth; + chartWidth *= ratio; + chartHeight *= ratio; + log.debug("Resized chart to " + chartWidth + "x" + chartHeight); + } + + if (chartHeight > height) { + log.warn("Height of the chart is too big for pdf -> resize it now."); + double ratio = ((double)height) / chartHeight; + chartWidth *= ratio; + chartHeight *= ratio; + log.debug("Resized chart to " + chartWidth + "x" + chartHeight); + } + Document document = null; if (landscape) { document = new Document(page.rotate()); @@ -265,20 +300,10 @@ PdfContentByte content = writer.getDirectContent(); - int width = 0; - int height = 0; - if (landscape) { - width = pageHeight; - height = pageWidth; - } - else { - width = pageWidth; - height = pageHeight; - } - PdfTemplate template = content.createTemplate(width, height); Graphics2D graphics = template.createGraphics(width, height); - Rectangle2D area = new Rectangle2D.Double(0.0D, 0.0D,width,height); + Rectangle2D area = new Rectangle2D.Double( + 0.0D, 0.0D, chartWidth, chartHeight); chart.draw(graphics, area); graphics.dispose();