diff flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java @ 2238:23c7c51df772

Some more refactoring in XYChartGenerator and ChartGenerator; implemented necessary stuff in TimeseriesChartGenerator and return new empty instances of timeseries charts. flys-artifacts/trunk@3885 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 02 Feb 2012 15:44:13 +0000
parents c2b15d9c0f43
children 7e8e1d5384c0
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Feb 02 15:39:28 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Feb 02 15:44:13 2012 +0000
@@ -9,6 +9,8 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.TreeMap;
+import java.util.SortedMap;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -21,6 +23,7 @@
 import org.jfree.chart.LegendItem;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.data.Range;
+import org.jfree.data.xy.XYDataset;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
@@ -88,17 +91,43 @@
     /** The settings that should be used during output creation.*/
     protected Settings settings;
 
+    /** Map of datasets ("index"). */
+    protected SortedMap<Integer, AxisDataset> datasets;
+
+
 
     /**
      * A mini interface that allows to walk over the YAXIS enums defined in
      * subclasses.
      */
     public interface YAxisWalker {
+
         int length();
+
         String getId(int idx);
+    } // end of YAxisWalker interface
+
+
+
+    public interface AxisDataset {
+
+        void addDataset(XYDataset dataset);
+
+        boolean isEmpty();
+
+    } // end of AxisDataset interface
+
+
+
+    /**
+     * Default constructor that initializes internal data structures.
+     */
+    public ChartGenerator() {
+        datasets = new TreeMap<Integer, AxisDataset>();
     }
 
 
+
     /**
      * This method needs to be implemented by concrete subclasses to create new
      * instances of JFreeChart.
@@ -145,6 +174,25 @@
 
 
     /**
+     * This method is used to create new AxisDataset instances which may differ
+     * in concrete subclasses.
+     *
+     * @param idx The index of an axis.
+     */
+    protected abstract AxisDataset createAxisDataset(int idx);
+
+
+    /**
+     * Combines the ranges of the X axis at index <i>idx</i>.
+     *
+     * @param range A new range.
+     * @param idx The index of the X axis that should be comined with
+     * <i>range</i>.
+     */
+    protected abstract void combineXRanges(Range range, int idx);
+
+
+    /**
      * This method should be used by concrete subclasses to add subtitle to
      * <i>chart</i>. <b>The method in this implementation is empty</b>.
      *
@@ -731,6 +779,59 @@
 
 
     /**
+     * Adds a new AxisDataset which contains <i>dataset</i> at index <i>idx</i>.
+     *
+     * @param dataset An XYDataset.
+     * @param idx The axis index.
+     * @param visible Determines, if the dataset should be visible or not.
+     */
+    public void addAxisDataset(XYDataset dataset, int idx, boolean visible) {
+        if (dataset == null || idx < 0) {
+            return;
+        }
+
+        AxisDataset axisDataset = getAxisDataset(idx);
+
+        Range[] xyRanges = ChartHelper.getRanges(dataset);
+
+        if (visible) {
+            logger.debug("Add new AxisDataset at index: " + idx);
+            axisDataset.addDataset(dataset);
+            combineXRanges(xyRanges[0], 0);
+        }
+        else {
+            combineXRanges(xyRanges[0], 0);
+
+            // TODO
+            // Expand y range provided by 'timeseries' to have a proper range
+            // set which includes all data.
+            // iw: I am not sure if we still need this
+        }
+    }
+
+
+    /**
+     * This method grants access to the AxisDatasets stored in <i>datasets</i>.
+     * If no AxisDataset exists for index <i>idx</i>, a new AxisDataset is
+     * created using <i>createAxisDataset()</i>.
+     *
+     * @param idx The index of the desired AxisDataset.
+     *
+     * @return an existing or new AxisDataset.
+     */
+    public AxisDataset getAxisDataset(int idx) {
+        AxisDataset axisDataset = datasets.get(idx);
+
+        if (axisDataset == null) {
+            axisDataset = createAxisDataset(idx);
+            datasets.put(idx, axisDataset);
+        }
+
+        return axisDataset;
+    }
+
+
+    /**
      * This helper mehtod is used to extract the current locale from instance
      * vairable <i>context</i>.
      *
@@ -947,6 +1048,29 @@
 
 
     /**
+     * Create Y (range) axis for given index.
+     * Shall be overriden by subclasses.
+     */
+    protected NumberAxis createYAxis(int index) {
+        YAxisWalker walker = getYAxisWalker();
+
+        Font labelFont = new Font(
+            DEFAULT_FONT_NAME,
+            Font.BOLD,
+            getYAxisFontSize(index));
+
+        IdentifiableNumberAxis axis = new IdentifiableNumberAxis(
+            walker.getId(index),
+            getYAxisLabel(index));
+
+        axis.setAutoRangeIncludesZero(false);
+        axis.setLabelFont(labelFont);
+
+        return axis;
+    }
+
+
+    /**
      * Creates a new LegendItem with <i>name</i> and font provided by
      * <i>createLegendLabelFont()</i>.
      *

http://dive4elements.wald.intevation.org