changeset 1053:6169ddc827ac

The format of a histogram pdf depends on the width and height specified by the user - or the default size (issue290). gnv-artifacts/trunk@1127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 26 May 2010 07:00:55 +0000
parents b30361464775
children 8430269ec73b
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, 56 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Wed May 26 06:45:03 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Wed May 26 07:00:55 2010 +0000
@@ -1,6 +1,18 @@
 2010-05-26  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
-	  Issue290 - PDF format belongs to the chart's aspect ratio.
+	  Issue290 - PDF format of histograms depends on histograms' apsect ratio.
+
+	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java:
+	  Removed the boolean parameter to adjust the page format when calling the
+	  pdf export method of a histogram.
+
+	* src/main/java/de/intevation/gnv/exports/ChartExportHelper.java: The format
+	  of a histogram depends on its width and height. Width and height depends
+	  on user input or the default size (600x400).
+
+2010-05-26  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	  Issue290 - PDF format depends on the chart's aspect ratio.
 
 	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java:
 	  Removed reading the system property "export.pdf.landscape" to adjust the
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Wed May 26 06:45:03 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Wed May 26 07:00:55 2010 +0000
@@ -339,11 +339,11 @@
         OutputStream out,
         Chart[]      histograms,
         String       pageFormat,
-        boolean      landscape,
         float        marginLeft,
         float        marginRight,
         float        marginTop,
-        float        marginBottom
+        float        marginBottom,
+        CallContext  context
     ) {
         log.info("export histogram as pdf.");
 
@@ -356,6 +356,39 @@
         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");
+
+        boolean landscape = chartWidth > chartHeight ? true : false;
+
+        int width  = 0;
+        int height = 0;
+        if (landscape) {
+            width  = pageHeight;
+            height = pageWidth;
+        }
+        else {
+            width  = pageWidth;
+            height = pageHeight;
+        }
+
+        if (chartWidth > width) {
+            log.warn("Histogram width is too big for pdf -> resize it now.");
+            double ratio = ((double)width) / chartWidth;
+            chartWidth  *= ratio;
+            chartHeight *= ratio;
+            log.debug("Resized histogram to " + chartWidth + "x" + chartHeight);
+        }
+
+        if (chartHeight > height) {
+            log.warn("Histogram height is too big for pdf -> resize it now.");
+            double ratio = ((double)height) / chartHeight;
+            chartWidth  *= ratio;
+            chartHeight *= ratio;
+            log.debug("Resized histogram to " + chartWidth + "x" + chartHeight);
+        }
+
         Document document = null;
         if (landscape) {
             document = new Document(page.rotate());
@@ -372,17 +405,6 @@
 
             PdfContentByte content  = writer.getDirectContent();
 
-            int width  = 0;
-            int height = 0;
-            if (landscape) {
-                width  = pageHeight;
-                height = pageWidth;
-            }
-            else {
-                width  = pageWidth;
-                height = pageHeight;
-            }
-
             int size = histograms.length;
             for (int i = 0; i < size; i++) {
                 if (i > 0) {
@@ -391,7 +413,8 @@
 
                 JFreeChart chart     = histograms[i].generateChart();
                 PdfTemplate template = content.createTemplate(width, height);
-                Graphics2D  graphics = template.createGraphics(width, height);
+                Graphics2D  graphics = template.createGraphics(
+                    chartWidth, chartHeight);
                 Rectangle2D area     = new Rectangle2D.Double(
                     0.0D, 0.0D,width,height);
 
@@ -401,7 +424,7 @@
             }
         }
         catch (DocumentException de) {
-            log.error("Error while exporting chart to pdf.", de);
+            log.error("Error while exporting histogram to pdf.", de);
         }
         finally {
             document.close();
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Wed May 26 06:45:03 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Wed May 26 07:00:55 2010 +0000
@@ -431,12 +431,15 @@
                     );
                 }
                 else if (mode.equalsIgnoreCase("pdf")) {
+                    callContext.putContextValue("chart.width", chartWidth);
+                    callContext.putContextValue("chart.height", chartHeight);
+
                     ChartExportHelper.exportHistogramsAsPDF(
                         outputStream,
                         histograms,
                         "A4",
-                        true,
-                        50F, 50F, 50F, 50F
+                        50F, 50F, 50F, 50F,
+                        callContext
                     );
                 }
                 else if (mode.equalsIgnoreCase("svg")) {

http://dive4elements.wald.intevation.org