changeset 1087:92fce3b3d07f

Centered histograms in pdf exports. gnv-artifacts/trunk@1189 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 10 Jun 2010 09:23:33 +0000
parents f2127cd0fe31
children 46cc1ab1ee15
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java
diffstat 2 files changed, 27 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Thu Jun 10 09:05:58 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Thu Jun 10 09:23:33 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 histograms are centered now.
+
 2010-06-10  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	  Issue290 - Centering of pdf exports
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Thu Jun 10 09:05:58 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Thu Jun 10 09:23:33 2010 +0000
@@ -359,10 +359,8 @@
             pageFormat = DEFAULT_PAGE_SIZE;
 
         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");
@@ -370,8 +368,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;
@@ -381,20 +379,22 @@
             height = pageHeight;
         }
 
-        if (chartWidth > width) {
+        float spaceX = width  - marginLeft - marginRight;
+        if (chartWidth > spaceX) {
             log.warn("Histogram width 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 histogram to " + chartWidth + "x" + chartHeight);
+            log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
         }
 
-        if (chartHeight > height) {
+        float spaceY = height - marginTop  - marginBottom;
+        if (chartHeight > spaceY) {
             log.warn("Histogram height 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 histogram to " + chartWidth + "x" + chartHeight);
+            log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
         }
 
         Document document = null;
@@ -421,14 +421,19 @@
 
                 JFreeChart chart     = histograms[i].generateChart();
                 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) {

http://dive4elements.wald.intevation.org