changeset 654:bbc966c81809

#90 Removed margins between data area border and curves. flys-artifacts/trunk@2050 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 03 Jun 2011 10:21:13 +0000
parents 67c7020f4ed3
children 913b52064449
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 2 files changed, 100 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jun 03 07:20:39 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jun 03 10:21:13 2011 +0000
@@ -1,3 +1,13 @@
+2011-06-03  Ingo Weinzierl <ingo@intevation.de>
+
+	  flys/issue90(Diagramm: Trennung der Diagrammfläche und Achsen aufheben)
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Determine the ranges of x and y axes. If no zoom ranges are given, we
+	  will determine the min and max xy values in the dataset manually,
+	  because JFreeCharts adds a margin to the left and right of the data
+	  area automatically..
+
 2011-06-03  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java: Moved
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jun 03 07:20:39 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jun 03 10:21:13 2011 +0000
@@ -15,6 +15,9 @@
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.data.Range;
+import org.jfree.data.xy.XYDataset;
+
+import org.jfree.ui.RectangleInsets;
 
 import de.intevation.flys.exports.ChartExportHelper;
 
@@ -27,7 +30,7 @@
 public abstract class XYChartGenerator extends ChartGenerator {
 
     /** The logger that is used in this generator.*/
-    private static Logger logger = Logger.getLogger(ChartGenerator.class);
+    private static Logger logger = Logger.getLogger(XYChartGenerator.class);
 
 
     public static final Color DEFAULT_GRID_COLOR      = Color.GRAY;
@@ -110,14 +113,44 @@
 
 
     /**
-     * Zooms the chart to the ranges specified in the attribute document.
+     * This method zooms the plot to the specified ranges in the attribute
+     * document or to the ranges specified by the min/max values in the
+     * datasets. <b>Note:</b> We determine the range manually if no zoom ranges
+     * are given, because JFreeCharts auto-zoom adds a margin to the left and
+     * right of the data area.
      *
      * @param plot The XYPlot.
      */
     protected void zoom(XYPlot plot) {
         logger.debug("Zoom to specified ranges.");
-        zoomX(plot);
-        zoomY(plot);
+
+        Range[] ranges = null;
+
+        boolean x = zoomX(plot);
+        if (!x) {
+            XYDataset dataset = plot.getDataset();
+
+            ranges = getRangesForDataset(dataset);
+
+            logger.debug("No x range specified. Set manually: " + ranges[0]);
+
+            ValueAxis axis = plot.getDomainAxis();
+            axis.setRange(ranges[0]);
+        }
+
+        boolean y = zoomY(plot);
+        if (!y) {
+            XYDataset dataset = plot.getDataset();
+
+            if (ranges == null) {
+                ranges = getRangesForDataset(dataset);
+            }
+
+            logger.debug("No y range specified. Set manually: " + ranges[1]);
+
+            ValueAxis axis = plot.getRangeAxis();
+            axis.setRange(ranges[1]);
+        }
     }
 
 
@@ -125,15 +158,21 @@
      * Zooms the x axis to the range specified in the attribute document.
      *
      * @param plot The XYPlot.
+     *
+     * @return true, if a zoom range was specified, otherwise false.
      */
-    protected void zoomX(XYPlot plot) {
+    protected boolean zoomX(XYPlot plot) {
         Range xrange = getDomainAxisRange();
         if (xrange != null) {
             ValueAxis xaxis = plot.getDomainAxis();
             xaxis.setRange(xrange);
 
             logger.debug("Zoom chart to X: " + xrange);
+
+            return true;
         }
+
+        return false;
     }
 
 
@@ -141,15 +180,58 @@
      * Zooms the y axis to the range specified in the attribute document.
      *
      * @param plot The XYPlot.
+     *
+     * @return true, if a zoom range was specified, otherwise false.
      */
-    protected void zoomY(XYPlot plot) {
+    protected boolean zoomY(XYPlot plot) {
         Range yrange = getValueAxisRange();
         if (yrange != null) {
             ValueAxis yaxis = plot.getRangeAxis();
             yaxis.setRange(yrange);
 
             logger.debug("Zoom chart to Y: " + yrange);
+
+            return true;
         }
+
+        return false;
+    }
+
+
+    /**
+     * This method extracts the minimum and maximum values for x and y axes.
+     *
+     * @param dataset The dataset that should be observed.
+     *
+     * @return a Range[] as follows: [x-Range, y-Range].
+     */
+    protected Range[] getRangesForDataset(XYDataset dataset) {
+        double[] xr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
+        double[] yr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
+
+        int sCount = dataset.getSeriesCount();
+
+        for (int i = 0; i < sCount; i++) {
+            int iCount = dataset.getItemCount(i);
+
+            for (int j = 0; j < iCount; j++) {
+                double x = dataset.getX(i, j).doubleValue();
+                double y = dataset.getY(i, j).doubleValue();
+
+                xr[0] = xr[0] < x ? xr[0] : x;
+                xr[1] = xr[1] > x ? xr[1] : x;
+                yr[0] = yr[0] < y ? yr[0] : y;
+                yr[1] = yr[1] > y ? yr[1] : y;
+            }
+        }
+
+        // this is only required, if there are no items in the dataset.
+        xr[0] = xr[0] < xr[1] ? xr[0] : xr[1];
+        xr[1] = xr[1] > xr[0] ? xr[1] : xr[0];
+        yr[0] = yr[0] < yr[1] ? yr[0] : yr[1];
+        yr[1] = yr[1] > yr[0] ? yr[1] : yr[0];
+
+        return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])};
     }
 
 
@@ -181,6 +263,8 @@
         plot.setRangeGridlineStroke(gridStroke);
         plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR);
         plot.setRangeGridlinesVisible(true);
+
+        plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d));
     }
 
 

http://dive4elements.wald.intevation.org