diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 923:7ca4a287cd0e

#135 Modified the way to store datasets for different chart axes. flys-artifacts/trunk@2275 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 01 Jul 2011 11:16:11 +0000
parents f3fd8c9b7f51
children f7761914f745
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jul 01 08:33:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jul 01 11:16:11 2011 +0000
@@ -16,6 +16,8 @@
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.data.Range;
 import org.jfree.data.xy.XYDataset;
+import org.jfree.data.xy.XYSeries;
+import org.jfree.data.xy.XYSeriesCollection;
 
 import org.jfree.ui.RectangleInsets;
 
@@ -33,6 +35,13 @@
     private static Logger logger = Logger.getLogger(XYChartGenerator.class);
 
 
+    /** SeriesCollection used for the first axis.*/
+    protected XYSeriesCollection first;
+
+    /** SeriesCollection used for the second axis.*/
+    protected XYSeriesCollection second;
+
+
     public static final Color DEFAULT_GRID_COLOR      = Color.GRAY;
     public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;
 
@@ -58,14 +67,6 @@
      */
     protected abstract String getYAxisLabel();
 
-    /**
-     * This method is called to add all datasets of a concrete XYChartGenerator
-     * to the JFreeChart.
-     *
-     * @param chart The JFreeChart object.
-     */
-    protected abstract void addDatasets(JFreeChart chart);
-
 
     public void generate()
     throws IOException
@@ -102,16 +103,64 @@
 
         XYPlot plot = (XYPlot) chart.getPlot();
 
-        addDatasets(chart);
+        addDatasets(plot);
         addSubtitles(chart);
         adjustPlot(plot);
         adjustAxes(plot);
+
+        removeEmptyRangeAxes(plot);
+
         autoZoom(plot);
 
         return chart;
     }
 
 
+    protected void addDatasets(XYPlot plot) {
+        if (first != null) {
+            logger.debug("Set the first axis dataset.");
+            plot.setDataset(0, first);
+        }
+        if (second != null) {
+            logger.debug("Set the second axis dataset.");
+            plot.setDataset(1, second);
+        }
+    }
+
+
+    public void addFirstAxisSeries(XYSeries series) {
+        if (first == null) {
+            first = new XYSeriesCollection();
+        }
+
+        if (series != null) {
+            first.addSeries(series);
+        }
+    }
+
+
+    public void addSecondAxisSeries(XYSeries series) {
+        if (second == null) {
+            second = new XYSeriesCollection();
+        }
+
+        if (series != null) {
+            second.addSeries(series);
+        }
+    }
+
+
+    private void removeEmptyRangeAxes(XYPlot plot) {
+        if (first == null) {
+            plot.setRangeAxis(0, null);
+        }
+
+        if (second == null) {
+            plot.setRangeAxis(1, null);
+        }
+    }
+
+
     /**
      * 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
@@ -129,6 +178,11 @@
 
         for (int i = 0, num = plot.getDatasetCount(); i < num; i++) {
             XYDataset dataset = plot.getDataset(i);
+
+            if (dataset == null) {
+                continue;
+            }
+
             Range[]   ranges  = getRangesForDataset(dataset);
 
             if (i == 0) {
@@ -261,6 +315,14 @@
         plot.setRangeGridlinesVisible(true);
 
         plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d));
+
+        if (plot.getDataset(0) != null) {
+            plot.mapDatasetToRangeAxis(0, 0);
+        }
+
+        if (plot.getDataset(1) != null) {
+            plot.mapDatasetToRangeAxis(1, 1);
+        }
     }
 
 

http://dive4elements.wald.intevation.org