Mercurial > dive4elements > gnv-client
changeset 1086:f2127cd0fe31
Centered charts in a pdf exports.
gnv-artifacts/trunk@1188 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 10 Jun 2010 09:05:58 +0000 |
parents | ec512e7992c6 |
children | 92fce3b3d07f |
files | gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java |
diffstat | 2 files changed, 79 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog Wed Jun 09 17:11:54 2010 +0000 +++ b/gnv-artifacts/ChangeLog Thu Jun 10 09:05:58 2010 +0000 @@ -1,3 +1,10 @@ +2010-06-10 Ingo Weinzierl <ingo.weinzierl@intevation.de> + + Issue290 - Centering of pdf exports + + * src/main/java/de/intevation/gnv/exports/ChartExportHelper.java: PDF + exports of charts are centered now. + 2010-06-09 Ingo Weinzierl <ingo.weinzierl@intevation.de> * src/main/java/de/intevation/gnv/chart/VerticalProfileVectorChart.java,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java Wed Jun 09 17:11:54 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java Thu Jun 10 09:05:58 2010 +0000 @@ -248,8 +248,8 @@ // 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)); + float pageWidth = page.getWidth(); + float pageHeight = page.getHeight(); // the chart width int chartWidth = (Integer) context.getContextValue("chart.width"); @@ -257,8 +257,8 @@ boolean landscape = chartWidth > chartHeight ? true : false; - int width = 0; - int height = 0; + float width = 0; + float height = 0; if (landscape) { width = pageHeight; height = pageWidth; @@ -268,17 +268,19 @@ height = pageHeight; } - if (chartWidth > width) { + float spaceX = width - marginLeft - marginRight; + if (chartWidth > spaceX) { log.warn("Width of the chart is too big for pdf -> resize it now."); - double ratio = ((double)width) / chartWidth; + double ratio = ((double)spaceX) / chartWidth; chartWidth *= ratio; chartHeight *= ratio; log.debug("Resized chart to " + chartWidth + "x" + chartHeight); } - if (chartHeight > height) { + float spaceY = height - marginTop - marginBottom; + if (chartHeight > spaceY) { log.warn("Height of the chart is too big for pdf -> resize it now."); - double ratio = ((double)height) / chartHeight; + double ratio = ((double)spaceY) / chartHeight; chartWidth *= ratio; chartHeight *= ratio; log.debug("Resized chart to " + chartWidth + "x" + chartHeight); @@ -302,14 +304,19 @@ PdfContentByte content = writer.getDirectContent(); PdfTemplate template = content.createTemplate(width, height); - Graphics2D graphics = template.createGraphics( + Graphics2D graphics = template.createGraphics(width, height); + + double[] origin = getCenteredAnchor( + marginLeft, marginRight, marginBottom, marginTop, + width, height, chartWidth, chartHeight); + Rectangle2D area = new Rectangle2D.Double( - 0.0D, 0.0D, chartWidth, chartHeight); + origin[0], origin[1], chartWidth, chartHeight); chart.draw(graphics, area); graphics.dispose(); - content.addTemplate(template, marginLeft, marginBottom); + content.addTemplate(template, 0f, 0f); } catch (DocumentException de) { log.error("Error while exporting chart to pdf.", de); @@ -431,5 +438,59 @@ document.close(); } } + + + /** + * This method returns the anchor of the chart so that the chart is centered + * according to the given parameters. + * + * @param mLeft Left margin + * @param mRight Right margin + * @param mBottom Bottom margin + * @param mTop Top margin + * @param width The complete width of the drawing area. + * @param height The complete height of the drawing area. + * @param chartWidth The width of the chart. + * @param chartHeight The height of the chart. + * + * @return an array that contains the anchor for a chart with the given + * parameters. The first value is the x point, the second value is the y + * point. + */ + public static double[] getCenteredAnchor( + double mLeft, double mRight, double mBottom, double mTop, + double width, double height, + double chartWidth, double chartHeight + ) { + if (log.isDebugEnabled()) { + log.debug("Calculate centered origin..."); + log.debug("-> PDF width : " + width); + log.debug("-> PDF height : " + height); + log.debug("-> Chart width : " + chartWidth); + log.debug("-> Chart height : " + chartHeight); + log.debug("-> margin left : " + mLeft); + log.debug("-> margin right : " + mRight); + log.debug("-> margin bottom: " + mBottom); + log.debug("-> margin top : " + mTop); + } + + double[] origin = new double[2]; + + double centerX = width / 2; + double centerY = height / 2; + + origin[0] = centerX - chartWidth / 2; + origin[1] = centerY - chartHeight / 2; + + origin[0] = origin[0] >= mLeft ? origin[0] : mLeft; + origin[1] = origin[1] >= mTop ? origin[1] : mTop; + + if (log.isDebugEnabled()) { + log.debug("==> centered left origin: " + origin[0]); + log.debug("==> centered top origin: " + origin[1]); + } + + return origin; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :