diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 2138:59bb5c895be3

Improved HYK/Zones- handling. flys-artifacts/trunk@3716 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 19 Jan 2012 11:00:27 +0000
parents e8fc770d2f8c
children c5d24e0587ce
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jan 19 10:51:20 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Jan 19 11:00:27 2012 +0000
@@ -30,6 +30,7 @@
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItem;
 import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.annotations.XYBoxAnnotation;
 import org.jfree.chart.annotations.XYTextAnnotation;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.axis.ValueAxis;
@@ -60,6 +61,8 @@
 
 import de.intevation.flys.utils.ThemeAccess;
 
+import de.intevation.flys.artifacts.model.HYKFactory;
+
 /**
  * An abstract base class for creating XY charts.
  *
@@ -512,6 +515,9 @@
         adjustAxes(plot);
         autoZoom(plot);
 
+        // These have to go after the autozoom.
+        addBoxAnnotations(plot);
+
         return chart;
     }
 
@@ -998,7 +1004,7 @@
 
             lic.add(li);
 
-            for (XYTextAnnotation ta: fa.getAnnotations()) {
+            for (XYTextAnnotation ta: fa.getTextAnnotations()) {
                 if(ta instanceof StickyAxisAnnotation) {
                     StickyAxisAnnotation sta = (StickyAxisAnnotation)ta;
                     sta.applyTheme(themeAccess);
@@ -1023,6 +1029,94 @@
         plot.setFixedLegendItems(old);
     }
 
+    /**
+     * Get "lowest" Y Value for first axis. This value is exactly at the
+     * border of the plot.
+     */
+    protected double getLowestYValue(XYPlot plot) {
+        ValueAxis yaxis = plot.getRangeAxis(0);
+        if (yaxis == null) {
+            logger.warn("No first Y-Axis to find lowest value for.");
+        }
+        return yaxis.getRange().getLowerBound();
+    }
+
+
+    /**
+     * Get "lowest" Y Value for first axis. This value is exactly at the
+     * border of the plot.
+     */
+    protected double getUppestYValue(XYPlot plot) {
+        ValueAxis yaxis = plot.getRangeAxis(0);
+        if (yaxis == null) {
+            logger.warn("No first Y-Axis to find uppest value for.");
+        }
+        return yaxis.getRange().getUpperBound();
+    }
+
+
+    /** 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);
+        }
+    }
+
+
+    /** Add box annotations (currently, only hyk zones). */
+    public void addBoxAnnotations(XYPlot plot) {
+        logger.debug("XYChartGenerator.addBoxAnnotations");
+
+        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);
+
+        double fillPercent = 0.05;
+        double low = getLowestYValue(plot);
+        double up  = getUppestYValue(plot);
+        double upb = low + (up - low) * fillPercent;
+        double upt = low + (up - low) * fillPercent/2.0d;
+
+        for (FLYSAnnotation fa: annotations) {
+            for (HYKFactory.Zone zone: fa.getBoxes()) {
+                fillPaint = colorForHYKZone(zone.getName());
+
+                XYBoxAnnotation boxA = new XYBoxAnnotation(zone.getFrom(), low,
+                    zone.getTo(), upb, basicStroke, tranPaint, fillPaint);
+                XYBoxAnnotation boxB = new XYBoxAnnotation(zone.getFrom(), low,
+                    zone.getTo(), up, basicStroke, fillPaint, tranPaint);
+
+                // TODO style text
+                XYTextAnnotation tex = new XYTextAnnotation(zone.getName(),
+                    zone.getFrom() + (zone.getTo() - zone.getFrom()) / 2.0d,
+                    upt);
+
+                plot.getRenderer().addAnnotation(boxA);
+                plot.getRenderer().addAnnotation(boxB);
+                plot.getRenderer().addAnnotation(tex);
+            }
+        }
+    }
 
     /**
      * Adjusts the axes of a plot. This method sets the <i>labelFont</i> of the

http://dive4elements.wald.intevation.org