Mercurial > dive4elements > gnv-client
changeset 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 | 778d86255d76 |
children | 8f836fb6f592 |
files | gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java |
diffstat | 3 files changed, 62 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog Tue May 25 13:57:48 2010 +0000 +++ b/gnv-artifacts/ChangeLog Tue May 25 15:41:31 2010 +0000 @@ -1,3 +1,16 @@ +2010-05-25 Ingo Weinzierl <ingo.weinzierl@intevation.de> + + Issue290 + + * src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java: + Added chart width and chart height as new parameters for the pdf creation. + + * src/main/java/de/intevation/gnv/exports/ChartExportHelper.java: Don't use + the maximum size of a pdf page to draw the chart but use the parameter + chart width and chart height. Scale the chart down if its size and/or + height exceed the maximum size of the pdf page - the original aspect + ratio keeps alive. + 2010-05-25 Ingo Weinzierl <ingo.weinzierl@intevation.de> Issue287
--- 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();
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java Tue May 25 13:57:48 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java Tue May 25 15:41:31 2010 +0000 @@ -375,6 +375,12 @@ ); } else if (mode.equalsIgnoreCase("pdf")) { + callContext.putContextValue("chart.width", chartWidth); + callContext.putContextValue("chart.height", chartHeight); + callContext.putContextValue("shapes.visible", sVisible); + callContext.putContextValue("lines.visible", lVisible); + callContext.putContextValue("locale", locale); + createPDF( outputStream, parameters, @@ -384,9 +390,6 @@ uuid, "A4", true, - lVisible, - sVisible, - locale, callContext ); } @@ -846,9 +849,6 @@ String uuid, String exportFormat, boolean landscape, - boolean linesVisible, - boolean shapesVisible, - Locale locale, CallContext context ) { Chart chart = getChart( @@ -858,10 +858,10 @@ measurements, dates, getChartResult(uuid, context), - locale, + (Locale) context.getContextValue("locale"), uuid, - linesVisible, - shapesVisible, + Boolean.TRUE.equals(context.getContextValue("lines.visible")), + Boolean.TRUE.equals(context.getContextValue("shapes.visible")), context ); @@ -875,7 +875,8 @@ chart.generateChart(), "A4", PDF_FORMAT_LANDSCAPE, - 50F, 50F, 50F, 50F + 50F, 50F, 50F, 50F, + context ); }