diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.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/XYChartGenerator.java	Thu Feb 02 15:39:28 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Feb 02 15:44:13 2012 +0000
@@ -83,7 +83,7 @@
 public abstract class XYChartGenerator extends ChartGenerator {
 
     // TODO Consider storing the renderer here.
-    private class AxisDataset {
+    private class XYAxisDataset implements AxisDataset {
         /** Symbolic integer, but also coding the priority (0 goes first). */
         protected int axisSymbol;
         /** List of assigned datasets (in order). */
@@ -94,7 +94,7 @@
         protected int plotAxisIndex;
 
         /** Create AxisDataset. */
-        public AxisDataset(int symb) {
+        public XYAxisDataset(int symb) {
             this.axisSymbol = symb;
             datasets        = new ArrayList<XYDataset>();
         }
@@ -114,10 +114,16 @@
             range = Range.combine(range, subRange);
         }
 
+
+        @Override
+        public void addDataset(XYDataset dataset) {
+            datasets.add(dataset);
+            includeYRange(((XYSeriesCollection) dataset).getSeries(0));
+        }
+
         /** Add a dataset, include its range. */
-        public void addDataset(XYSeries dataset) {
-            this.datasets.add(new XYSeriesCollection(dataset));
-            includeYRange(dataset);
+        public void addDataset(XYSeries series) {
+            addDataset(new XYSeriesCollection(series));
         }
 
         public void addArea(StyledAreaSeriesCollection series) {
@@ -172,9 +178,6 @@
     /** The logger that is used in this generator. */
     private static Logger logger = Logger.getLogger(XYChartGenerator.class);
 
-    /** Map of datasets ("index"). */
-    protected SortedMap<Integer, AxisDataset> datasets;
-
     /** List of annotations to insert in plot. */
     protected List<FLYSAnnotation> annotations;
 
@@ -186,9 +189,10 @@
 
 
     public XYChartGenerator() {
+        super();
+
         xRanges  = new HashMap<Integer, Range>();
         yRanges  = new HashMap<Integer, Range>();
-        datasets = new TreeMap<Integer, AxisDataset>();
     }
 
 
@@ -236,6 +240,13 @@
     }
 
 
+    @Override
+    protected AxisDataset createAxisDataset(int idx) {
+        logger.debug("Create new XYAxisDataset for index: " + idx);
+        return new XYAxisDataset(idx);
+    }
+
+
     /**
      * Put debug output about datasets.
      */
@@ -288,7 +299,7 @@
         for (Map.Entry<Integer, AxisDataset> entry: datasets.entrySet()) {
             if (!entry.getValue().isEmpty()) {
                 // Add axis and range information.
-                AxisDataset axisDataset = entry.getValue();
+                XYAxisDataset axisDataset = (XYAxisDataset) entry.getValue();
                 NumberAxis axis = createYAxis(entry.getKey());
 
                 plot.setRangeAxis(axisIndex, axis);
@@ -324,12 +335,8 @@
             logger.warn("Cannot yet render above/under curve.");
             return;
         }
-        AxisDataset axisDataset = datasets.get(index);
 
-        if (axisDataset == null) {
-            axisDataset = new AxisDataset(index);
-            datasets.put(index, axisDataset);
-        }
+        XYAxisDataset axisDataset = (XYAxisDataset) getAxisDataset(index);
 
         if (visible) {
             axisDataset.addArea(area);
@@ -349,30 +356,19 @@
      * @param visible whether or not the data should be plotted.
      */
     public void addAxisSeries(XYSeries series, int index, boolean visible) {
+        addAxisDataset(new XYSeriesCollection(series), index, visible);
+
         if (series == null) {
             return;
         }
 
-        AxisDataset axisDataset = datasets.get(index);
-
-        if (axisDataset == null) {
-            axisDataset = new AxisDataset(index);
-            datasets.put(index, axisDataset);
-        }
+        XYAxisDataset axisDataset = (XYAxisDataset) getAxisDataset(index);
 
-        logger.debug("addAxisSeries: extent X " + series.getMinX() + " : " + series.getMaxX()
-            + " extent y " + series.getMinY() + " : " + series.getMaxY());
-
-        if (visible) {
-            axisDataset.addDataset(series);
-        }
-        else {
+        if (!visible) {
             // Do this also when not visible to have axis scaled by default such
             // that every data-point could be seen (except for annotations).
             axisDataset.includeYRange(series);
         }
-
-        combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
     }
 
 
@@ -381,7 +377,7 @@
      * @param range the given ("minimal") range.
      * @param index index of axis to be merged.
      */
-    private void combineXRanges(Range range, int index) {
+    protected void combineXRanges(Range range, int index) {
 
         if (range == null
             || Double.isNaN(range.getLowerBound())
@@ -416,29 +412,6 @@
 
 
     /**
-     * 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;
-    }
-
-
-    /**
      * If no data is visible, draw at least empty axis.
      */
     private void recoverEmptyPlot(XYPlot plot) {
@@ -687,7 +660,7 @@
             // Do the more complicated case where we stick to the Y-Axis.
             // There is one nasty case (duration curves, where annotations
             // might stick to the second y-axis).
-            AxisDataset dataset = this.datasets.get(
+            XYAxisDataset dataset = (XYAxisDataset) getAxisDataset(
                 new Integer(annotation.getAxisSymbol()));
             if (dataset == null) {
                 logger.warn("Annotation should stick to unfindable y-axis: "

http://dive4elements.wald.intevation.org