changeset 3212:abc2db630815

Work in generalized annotations for chart generators flys-artifacts/trunk@4835 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Fri, 29 Jun 2012 12:35:06 +0000 (2012-06-29)
parents a1a434c163a4
children b75ebdaad095
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java
diffstat 7 files changed, 214 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jun 29 12:35:06 2012 +0000
@@ -1,3 +1,13 @@
+2012-06-29	Christian Lins <christian.lins@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java,
+	  src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java,
+	  src/main/java/de/intevation/flys/exports/ChartGenerator.java,
+	  src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java,
+	  src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java,
+	  src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Unfinished work on generalizing annotations in ChartGenerators.
+
 2012-06-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -55,6 +55,7 @@
 import de.intevation.flys.jfree.Bounds;
 import de.intevation.flys.jfree.DoubleBounds;
 import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer;
+import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StableXYDifferenceRenderer;
 import de.intevation.flys.jfree.StyledAreaSeriesCollection;
 import de.intevation.flys.jfree.Style;
@@ -113,7 +114,8 @@
     /** Map of datasets ("index"). */
     protected SortedMap<Integer, AxisDataset> datasets;
 
-
+    /** List of annotations to insert in plot. */
+    protected List<FLYSAnnotation> annotations;
 
     /**
      * A mini interface that allows to walk over the YAXIS enums defined in
@@ -158,6 +160,17 @@
     }
 
 
+    /**
+     * Adds annotations to list. The given annotation will be visible.
+     */
+    public void addAnnotations(FLYSAnnotation annotation) {
+        if (annotations == null) {
+            annotations = new ArrayList<FLYSAnnotation>();
+        }
+
+        annotations.add(annotation);
+    }
+
 
     /**
      * This method needs to be implemented by concrete subclasses to create new
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -275,7 +275,7 @@
 
         // Actual Styling is done in XYChartGenerator.
         if (visible) {
-            addVisibleAnnotations(new FLYSAnnotation(seriesName, null, zones, theme));
+            addAnnotations(new FLYSAnnotation(seriesName, null, zones, theme));
         }
     }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -6,12 +6,15 @@
 import de.intevation.flys.jfree.Bounds;
 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
 import de.intevation.flys.jfree.DoubleBounds;
+import de.intevation.flys.jfree.StickyAxisAnnotation;
 import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StyledTimeSeries;
 import de.intevation.flys.jfree.TimeBounds;
 
 import de.intevation.flys.utils.ThemeAccess;
 
+import de.intevation.flys.artifacts.model.HYKFactory;
+
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Font;
@@ -30,6 +33,8 @@
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItemCollection;
 
+import org.jfree.chart.annotations.XYBoxAnnotation;
+import org.jfree.chart.annotations.XYLineAnnotation;
 import org.jfree.chart.annotations.XYTextAnnotation;
 
 import org.jfree.chart.axis.ValueAxis;
@@ -614,11 +619,11 @@
 
 
     /** Add annotations (Sticky, Text and hyk zones). */
-    public void addAnnotationsToRenderer(XYPlot plot) {
-        logger.debug("XYChartGenerator.addAnnotationsToRenderer");
+   /* public void addAnnotationsToRenderer(XYPlot plot) {
+        logger.debug("addAnnotationsToRenderer");
 
         if (annotations == null) {
-            logger.debug("XYChartGenerator.addBoxAnnotations: no annotations.");
+            logger.debug("addAnnotationsToRenderer: no annotations.");
             return;
         }
 
@@ -672,6 +677,124 @@
                 plot.getRenderer().addAnnotation(ta, org.jfree.ui.Layer.FOREGROUND);
             }
         }
+    }*/
+
+        /**
+     * Add the annotations (Sticky, Text and hyk zones) stored
+     * in the annotations field.
+     */
+    public void addAnnotationsToRenderer(XYPlot plot) {
+        logger.debug("addAnnotationsToRenderer");
+
+        if (annotations == null) {
+            logger.debug("addAnnotationsToRenderer: no annotations.");
+            return;
+        }
+
+        // Paints for the boxes/lines.
+        Stroke basicStroke = new BasicStroke(1.0f);
+
+        Paint linePaint = new Color(255,  0,0,60);
+        Paint fillPaint = new Color(0,  255,0,60);
+        Paint tranPaint = new Color(0,    0,0, 0);
+
+        // OPTMIMIZE: Pre-calculate positions
+        Area area = new Area(
+            plot.getDomainAxis(0).getRange(),
+            plot.getRangeAxis().getRange());
+
+        // Walk over all Annotation sets.
+        for (FLYSAnnotation fa: annotations) {
+
+            // Access text styling, if any.
+            Document theme = fa.getTheme();
+            ThemeAccess.TextStyle textStyle = null;
+            ThemeAccess.LineStyle lineStyle = null;
+
+            // Get Themeing information and add legend item.
+            if (theme != null) {
+                ThemeAccess themeAccess = new ThemeAccess(theme);
+                textStyle = themeAccess.parseTextStyle();
+                lineStyle = themeAccess.parseLineStyle();
+                if (fa.getLabel() != null) {
+                    LegendItemCollection lic = new LegendItemCollection();
+                    LegendItemCollection old = plot.getFixedLegendItems();
+                    lic.add(createLegendItem(theme, fa.getLabel()));
+                    // (Re-)Add prior legend entries.
+                    if (old != null) {
+                        old.addAll(lic);
+                    }
+                    else {
+                        old = lic;
+                    }
+                    plot.setFixedLegendItems(old);
+                }
+            }
+
+            // The 'Sticky' Annotations (at axis, with line and text).
+    /*        for (StickyAxisAnnotation sta: fa.getAxisTextAnnotations()) {
+                addStickyAnnotation(
+                    sta, plot, area, lineStyle, textStyle, theme);
+            }*/
+
+            // Other Text Annotations (e.g. labels of manual points).
+            for (XYTextAnnotation ta: fa.getTextAnnotations()) {
+                // Style the text.
+                if (textStyle != null) {
+                    textStyle.apply(ta);
+                }
+                ta.setY(area.above(0.05d, ta.getY()));
+                plot.getRenderer().addAnnotation(ta, org.jfree.ui.Layer.FOREGROUND);
+            }
+
+            // Hyks.
+            for (HYKFactory.Zone zone: fa.getBoxes()) {
+                // For each zone, create a box to fill with color, a box to draw
+                // the lines and a text to display the type.
+                fillPaint = colorForHYKZone(zone.getName());
+
+                XYBoxAnnotation boxA = new XYBoxAnnotation(zone.getFrom(), area.atGround(),
+                    zone.getTo(), area.ofGround(0.03f), basicStroke, tranPaint, fillPaint);
+                XYBoxAnnotation boxB = new XYBoxAnnotation(zone.getFrom(), area.atGround(),
+                    zone.getTo(), area.atTop(), basicStroke, fillPaint, tranPaint);
+
+                XYTextAnnotation tex = new XYTextAnnotation(zone.getName(),
+                    zone.getFrom() + (zone.getTo() - zone.getFrom()) / 1.0d,
+                    area.ofGround(0.015f));
+                if (textStyle != null) {
+                    textStyle.apply(tex);
+                }
+
+                plot.getRenderer().addAnnotation(boxA, org.jfree.ui.Layer.BACKGROUND);
+                plot.getRenderer().addAnnotation(boxB, org.jfree.ui.Layer.BACKGROUND);
+                plot.getRenderer().addAnnotation(tex,  org.jfree.ui.Layer.BACKGROUND);
+            }
+        }
+    }
+
+        /** Get color for hyk zones by their type (which is the name). */
+    public Paint colorForHYKZone(String zoneName) {
+        if (zoneName.startsWith("R")) {
+            // Brownish.
+            return new Color(153, 60, 0);
+        }
+        else if (zoneName.startsWith("V")) {
+            // Greenish.
+            return new Color(0, 255, 0);
+        }
+        else if (zoneName.startsWith("B")) {
+            // Grayish.
+            return new Color(128, 128, 128);
+        }
+        else if (zoneName.startsWith("H")) {
+            // Blueish.
+            return new Color(0, 0, 255);
+        }
+        else {
+            // Default.
+            logger.debug("Unknown zone type found.");
+            return new Color(255, 0, 0);
+        }
     }
 
     public void addDomainAxisMarker(XYPlot plot) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -188,9 +188,6 @@
     /** The logger that is used in this generator. */
     private static Logger logger = Logger.getLogger(XYChartGenerator.class);
 
-    /** List of annotations to insert in plot. */
-    protected List<FLYSAnnotation> annotations;
-
     protected List<Marker> domainMarkers = new ArrayList<Marker>();
 
     protected List<Marker> valueMarkers = new ArrayList<Marker>();
@@ -459,18 +456,6 @@
 
 
     /**
-     * Adds annotations to list (if visible is true).
-     */
-    public void addVisibleAnnotations(FLYSAnnotation annotation) {
-        if (annotations == null) {
-            annotations = new ArrayList<FLYSAnnotation>();
-        }
-
-        annotations.add(annotation);
-    }
-
-
-    /**
      * If no data is visible, draw at least empty axis.
      */
     private void recoverEmptyPlot(XYPlot plot) {
@@ -1008,10 +993,10 @@
      * in the annotations field.
      */
     public void addAnnotationsToRenderer(XYPlot plot) {
-        logger.debug("XYChartGenerator.addAnnotationsToRenderer");
+        logger.debug("addAnnotationsToRenderer");
 
         if (annotations == null) {
-            logger.debug("XYChartGenerator.addAnnotationsToRenderer: no annotations.");
+            logger.debug("addAnnotationsToRenderer: no annotations.");
             return;
         }
 
@@ -1083,7 +1068,7 @@
                     zone.getTo(), area.atTop(), basicStroke, fillPaint, tranPaint);
 
                 XYTextAnnotation tex = new XYTextAnnotation(zone.getName(),
-                    zone.getFrom() + (zone.getTo() - zone.getFrom()) / 2.0d,
+                    zone.getFrom() + (zone.getTo() - zone.getFrom()) / 1.0d,
                     area.ofGround(0.015f));
                 if (textStyle != null) {
                     textStyle.apply(tex);
@@ -1207,7 +1192,7 @@
         }
 
         if (visible) {
-            addVisibleAnnotations(annotations);
+            addAnnotations(annotations);
         }
     }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -16,6 +16,8 @@
 
 import de.intevation.flys.exports.TimeseriesChartGenerator;
 
+import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
+import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StyledTimeSeries;
 
 import de.intevation.flys.utils.FLYSUtils;
@@ -25,12 +27,20 @@
 
 import java.io.OutputStream;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.JFreeChart;
 
+import org.jfree.chart.annotations.XYTextAnnotation;
+
 import org.jfree.chart.plot.IntervalMarker;
 import org.jfree.chart.plot.ValueMarker;
 
@@ -246,10 +256,20 @@
         TimeSeries interpol = new StyledTimeSeries(desc + "interpol", theme);
 
         if (qwds == null) {
+            logger.debug("doAnalysisEventsOut: qwds == null");
             return;
         }
+
+        if (qwds.length == 0) {
+            logger.debug("doAnalysisEventsOut: qwds.length == 0");
+            return;
+        }
+
+        Map<Integer, int[]> annoIdxMap = new HashMap<Integer, int[]>();
+
         for (int i = 0; i < qwds.length; i++) {
             if (qwds[i] == null) {
+                logger.debug("doAnalysisEventsOut: qwds[" + i + "] == null");
                 continue;
             }
             RegularTimePeriod rtp = new Day(qwds[i].getDate());
@@ -259,6 +279,9 @@
                 interpol.add(rtp, value);
             }
             else {
+                annoIdxMap.put(
+                    i,
+                    new int[]{series.getItemCount(), tsc.getSeriesCount()});
                 series.add(rtp, value);
             }
         }
@@ -267,6 +290,30 @@
         addAxisDataset(tsc, 0, visible);
         addAttribute(desc + "interpol", "interpolate");
         addAttribute(desc, "outline");
+
+        doQWDTextAnnotations(annoIdxMap, tsc, qwds, theme, visible);
+    }
+
+
+    protected void doQWDTextAnnotations(Map<Integer, int[]> annoIdxMap,
+            TimeSeriesCollection tsc, QWD[] qwds, Document theme, boolean visible) {
+        List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();
+        Set<Map.Entry<Integer, int[]>> entries = annoIdxMap.entrySet();
+        for(Map.Entry<Integer, int[]> entry : entries) {
+            QWD qwd = qwds[entry.getKey()];
+            int[] idxs = entry.getValue();
+            double x = tsc.getXValue(idxs[0], idxs[1]);
+                XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
+                    qwd.getQ() + " m\u00B3/s",
+                    x,
+                    qwd.getDeltaW());
+                textAnnos.add(anno);
+            logger.debug("annotation: " + x + "/" + qwd.getDeltaW());
+        }
+
+        FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme);
+        flysAnno.setTextAnnotations(textAnnos);
+        addAnnotations(flysAnno, visible);
     }
 
 
@@ -288,6 +335,9 @@
         if (qwds == null) {
             return;
         }
+
+        Map<Integer, int[]> annoIdxMap = new HashMap<Integer, int[]>();
+
         for (int i = 0; i < qwds.length; i++) {
             if (qwds[i] == null) {
                 continue;
@@ -299,8 +349,12 @@
                 interpol.addOrUpdate(rtp, value);
             }
             else {
+                annoIdxMap.put(
+                    i,
+                    new int[]{series.getItemCount(), tsc.getSeriesCount()});
                 series.addOrUpdate(rtp, value);
             }
+
         }
         tsc.addSeries(series);
         tsc.addSeries(interpol);
@@ -308,6 +362,8 @@
         addAxisDataset(tsc, 0, visible);
         addAttribute(desc + "interpol", "interpolate");
         addAttribute(desc, "outline");
+
+        //doQWDTextAnnotations(annoIdxMap, tsc, qwds, theme, visible);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java	Thu Jun 28 16:20:33 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java	Fri Jun 29 12:35:06 2012 +0000
@@ -179,15 +179,14 @@
                     dateFormat.format(qw.getDate()),
                     qw.getQ(),
                     qw.getW());
-                //anno.setRotationAngle(0);
                 textAnnos.add(anno);
             }
 
-            FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, doc);
-            flysAnno.setTextAnnotations(textAnnos);
             addAxisSeries(series, 0, visible);
             if(visible) {
-                addVisibleAnnotations(flysAnno);
+                FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, doc);
+                flysAnno.setTextAnnotations(textAnnos);
+                addAnnotations(flysAnno);
             }
         }
     }

http://dive4elements.wald.intevation.org