changeset 3278:c27c4e06dd87

Re-add HYK rendering code to CrossSectionGenerator flys-artifacts/trunk@4924 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Wed, 11 Jul 2012 09:24:07 +0000
parents fc0d613e5073
children 5a7b4f890d53
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java 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
diffstat 5 files changed, 149 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Jul 10 19:28:44 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jul 11 09:24:07 2012 +0000
@@ -1,3 +1,11 @@
+2012-07-11	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:
+	  Re-add HYK rendering code again to CrossSectionGenerator (fix for #712).
+
+
 2012-07-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/SQKMChartService.java,
@@ -246,7 +254,7 @@
 	Fix issue710 (crashing diagrams with only one point).
 
 	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
-	  Expand the range/bounds where its added. 
+	  Expand the range/bounds where its added.
 
 2012-07-06	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java	Tue Jul 10 19:28:44 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java	Wed Jul 11 09:24:07 2012 +0000
@@ -1,28 +1,24 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.artifacts.cache.CacheFactory;
+import de.intevation.flys.backend.SessionHolder;
+import de.intevation.flys.model.HYK;
+import de.intevation.flys.model.HYKFlowZone;
+import de.intevation.flys.model.HYKFormation;
+
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
-import java.io.Serializable;
-
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.Element;
 
 import org.apache.log4j.Logger;
-
 import org.hibernate.Query;
+import org.hibernate.SQLQuery;
 import org.hibernate.Session;
-import org.hibernate.SQLQuery;
 import org.hibernate.type.StandardBasicTypes;
 
-import de.intevation.flys.model.HYK;
-import de.intevation.flys.model.HYKFormation;
-import de.intevation.flys.model.HYKFlowZone;
-
-import de.intevation.flys.artifacts.cache.CacheFactory;
-
-import de.intevation.flys.backend.SessionHolder;
-
 
 /**
  * Factory to access HYKs (hydrographic values).
@@ -54,7 +50,7 @@
             Element element = cache.get(cacheKey);
             if (element != null) {
                 logger.debug("Got hyk from cache");
-                return (List<Zone>)element.getValue();
+                return element.getValue();
             }
         }
         else {
@@ -180,7 +176,7 @@
     }
 
 
-    /** Labelled section. */
+    /** Labeled section. */
     public static class Zone implements Serializable {
         /** Lower end of segment. */
         protected double  from;
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Tue Jul 10 19:28:44 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Wed Jul 11 09:24:07 2012 +0000
@@ -1,21 +1,15 @@
 package de.intevation.flys.exports;
 
 import de.intevation.artifactdatabase.state.ArtifactAndFacet;
-import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.Settings;
-
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 import de.intevation.artifacts.PreferredLocale;
-
 import de.intevation.artifacts.common.utils.XMLUtils;
-
 import de.intevation.flys.artifacts.FLYSArtifact;
-
 import de.intevation.flys.artifacts.resources.Resources;
-
 import de.intevation.flys.jfree.Bounds;
 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
 import de.intevation.flys.jfree.DoubleBounds;
@@ -26,11 +20,8 @@
 import de.intevation.flys.jfree.Style;
 import de.intevation.flys.jfree.StyledAreaSeriesCollection;
 import de.intevation.flys.jfree.StyledSeries;
-
 import de.intevation.flys.model.River;
-
 import de.intevation.flys.themes.ThemeAccess;
-
 import de.intevation.flys.utils.FLYSUtils;
 import de.intevation.flys.utils.ThemeUtil;
 
@@ -40,14 +31,10 @@
 import java.awt.Paint;
 import java.awt.Stroke;
 import java.awt.TexturePaint;
-
 import java.awt.geom.Rectangle2D;
-
 import java.awt.image.BufferedImage;
-
 import java.io.IOException;
 import java.io.OutputStream;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -58,29 +45,19 @@
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
-
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItem;
 import org.jfree.chart.LegendItemCollection;
-
 import org.jfree.chart.annotations.XYLineAnnotation;
 import org.jfree.chart.annotations.XYTextAnnotation;
-
 import org.jfree.chart.axis.NumberAxis;
-
 import org.jfree.chart.plot.XYPlot;
-
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
-
 import org.jfree.data.Range;
-
 import org.jfree.data.general.Series;
-
 import org.jfree.data.xy.XYDataset;
-
 import org.jfree.ui.RectangleInsets;
 import org.jfree.ui.TextAnchor;
-
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -137,7 +114,7 @@
     protected SortedMap<Integer, AxisDataset> datasets;
 
     /** List of annotations to insert in plot. */
-    protected List<FLYSAnnotation> annotations;
+    protected List<FLYSAnnotation> annotations = new ArrayList<FLYSAnnotation>();
 
     /**
      * A mini interface that allows to walk over the YAXIS enums defined in
@@ -186,10 +163,6 @@
      * 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);
     }
 
@@ -229,7 +202,7 @@
             // There is one nasty case (duration curves, where annotations
             // might stick to the second y-axis).
             // FIXME: Remove dependency to XYChartGenerator here
-            AxisDataset dataset = (XYChartGenerator.XYAxisDataset) getAxisDataset(
+            AxisDataset dataset = getAxisDataset(
                 new Integer(annotation.getAxisSymbol()));
             if (dataset == null) {
                 logger.warn("Annotation should stick to unfindable y-axis: "
@@ -449,13 +422,6 @@
             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
         ChartArea area = new ChartArea(
             plot.getDomainAxis(0).getRange(),
@@ -517,6 +483,7 @@
     public abstract JFreeChart generateChart();
 
 
+    @Override
     public abstract void doOut(
         ArtifactAndFacet bundle,
         Document         attr,
@@ -627,13 +594,11 @@
         Document theme,
         boolean visible
     ){
-        // Running into trouble here.
         logger.debug("doAnnotations");
 
         // Add all annotations to our annotation pool.
         annotations.setTheme(theme);
         if (aandf != null) {
-            Facet facet = aandf.getFacet();
             annotations.setLabel(aandf.getFacetDescription());
         }
         else {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Tue Jul 10 19:28:44 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Wed Jul 11 09:24:07 2012 +0000
@@ -1,14 +1,5 @@
 package de.intevation.flys.exports;
 
-import java.text.NumberFormat;
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.jfree.chart.JFreeChart;
-import org.jfree.chart.title.TextTitle;
-import org.jfree.data.xy.XYSeries;
-import org.w3c.dom.Document;
-
 import de.intevation.artifactdatabase.state.ArtifactAndFacet;
 import de.intevation.artifacts.DataProvider;
 import de.intevation.flys.artifacts.geom.Lines;
@@ -18,9 +9,27 @@
 import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StyledXYSeries;
 import de.intevation.flys.model.FastCrossSectionLine;
+import de.intevation.flys.themes.ThemeAccess;
 import de.intevation.flys.utils.Formatter;
 import de.intevation.flys.utils.ThemeUtil;
 
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Paint;
+import java.awt.Stroke;
+import java.text.NumberFormat;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.annotations.XYBoxAnnotation;
+import org.jfree.chart.annotations.XYTextAnnotation;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.title.TextTitle;
+import org.jfree.data.xy.XYSeries;
+import org.w3c.dom.Document;
+
 
 /**
  * An OutGenerator that generates cross section graphs.
@@ -125,6 +134,98 @@
         chart.addSubtitle(new TextTitle(subtitle));
     }
 
+    /** Get color for hyk zones by their type (which is the name). */
+    protected 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);
+        }
+    }
+    
+    @Override
+    protected void addAnnotationsToRenderer(XYPlot plot) {
+        super.addAnnotationsToRenderer(plot);
+        
+        // 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
+        ChartArea area = new ChartArea(
+            plot.getDomainAxis(0).getRange(),
+            plot.getRangeAxis().getRange());
+        
+        for(FLYSAnnotation fa : this.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);
+                }
+            }
+            
+            // 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);
+            }
+        }
+    }
 
     @Override
     protected String getDefaultXAxisLabel() {
@@ -141,6 +242,7 @@
     /**
      * Let one facet do its job.
      */
+    @Override
     public void doOut(
         ArtifactAndFacet artifactFacet,
         Document         attr,
@@ -201,6 +303,7 @@
 
 
     /** Look up the axis identifier for a given facet type. */
+    @Override
     public int axisIdxForFacet(String facetName) {
         // TODO Where to add thid axis too.
         return 0;
@@ -229,7 +332,7 @@
             series.setLabel("");
         }
         if (ThemeUtil.parseShowWidth(theme)) {
-            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
+            NumberFormat nf = Formatter.getMeterFormat(this.context);
             String labelAdd = "b=" + nf.format(lines.width) + "m";
             if (series.getLabel().equals("")) {
                series.setLabel(labelAdd);
@@ -239,8 +342,8 @@
             }
         }
         if (ThemeUtil.parseShowLevel(theme) && lines.points.length >0
-            && lines.points[1].length > 0) {
-            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
+                && lines.points[1].length > 0) {
+            NumberFormat nf = Formatter.getMeterFormat(this.context);
             String labelAdd = "W=" + nf.format(lines.points[1][0]) + "NN+m";
             if (series.getLabel().equals("")) {
                series.setLabel(labelAdd);
@@ -250,7 +353,7 @@
             }
         }
         if (ThemeUtil.parseShowMiddleHeight(theme) && lines.width != 0) {
-            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
+            NumberFormat nf = Formatter.getMeterFormat(this.context);
             String labelAdd = "H=" + nf.format(lines.area / lines.width) + "m";
                 // : " + lines.area + "/" + lines.width);
             if (series.getLabel().equals("")) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Tue Jul 10 19:28:44 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Wed Jul 11 09:24:07 2012 +0000
@@ -1,8 +1,15 @@
 package de.intevation.flys.exports;
 
+import de.intevation.artifactdatabase.state.ArtifactAndFacet;
+import de.intevation.flys.jfree.Bounds;
+import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
+import de.intevation.flys.jfree.DoubleBounds;
+import de.intevation.flys.jfree.FLYSAnnotation;
+import de.intevation.flys.jfree.StyledTimeSeries;
+import de.intevation.flys.jfree.TimeBounds;
+
 import java.awt.Color;
 import java.awt.Font;
-import java.awt.Paint;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -28,14 +35,6 @@
 import org.json.JSONException;
 import org.w3c.dom.Document;
 
-import de.intevation.artifactdatabase.state.ArtifactAndFacet;
-import de.intevation.flys.jfree.Bounds;
-import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
-import de.intevation.flys.jfree.DoubleBounds;
-import de.intevation.flys.jfree.FLYSAnnotation;
-import de.intevation.flys.jfree.StyledTimeSeries;
-import de.intevation.flys.jfree.TimeBounds;
-
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
@@ -77,8 +76,7 @@
 
         @Override
         public XYDataset[] getDatasets() {
-            return (XYDataset[])
-                datasets.toArray(new XYDataset[datasets.size()]);
+            return datasets.toArray(new XYDataset[datasets.size()]);
         }
 
 
@@ -431,8 +429,8 @@
             long max  = total.getUpper().longValue();
             long diff = max > min ? max - min : min - max;
 
-            long newMin = (long) Math.round(min + user.getLower().doubleValue() * diff);
-            long newMax = (long) Math.round(min + user.getUpper().doubleValue() * diff);
+            long newMin = Math.round(min + user.getLower().doubleValue() * diff);
+            long newMax = Math.round(min + user.getUpper().doubleValue() * diff);
 
             TimeBounds newBounds = new TimeBounds(newMin, newMax);
 
@@ -566,31 +564,6 @@
         addAxisDataset(tsc, axisIndex, visible);
     }
 
-    /** 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) {
         logger.debug("domainmarkers: " + domainMarker.size());
         for (Marker marker: domainMarker) {

http://dive4elements.wald.intevation.org