changeset 2666:6da7e064ae90

Allow basic and styled area labels, yet on static position within plot. flys-artifacts/trunk@4344 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 03 May 2012 20:27:01 +0000
parents bcf4011fa3f1
children b75681c09ef8
files flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java flys-artifacts/src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java
diffstat 4 files changed, 108 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Thu May 03 20:23:02 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Thu May 03 20:27:01 2012 +0000
@@ -80,6 +80,7 @@
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Graphics2D;
+import java.awt.Font;
 import java.awt.Paint;
 import java.awt.Shape;
 import java.awt.Stroke;
@@ -137,7 +138,6 @@
 
     private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class);
 
-    public static final int CALCULATE_NO_AREA       = 0;
     public static final int CALCULATE_POSITIVE_AREA = 1;
     public static final int CALCULATE_NEGATIVE_AREA = 2;
     public static final int CALCULATE_ALL_AREA =
@@ -169,11 +169,20 @@
 
     protected boolean drawOriginalSeries;
 
+    /** The color of the label showing the calculated area. */
+    protected Color labelColor;
+
+    /** Font to draw label of calculated area with. */
+    protected Font labelFont;
+
     protected int areaCalculationMode;
 
     protected double positiveArea;
     protected double negativeArea;
 
+    /** Whether or not to draw a label in the area. */
+    protected boolean labelArea = true;
+
 
     /**
      * This flag controls whether or not the x-coordinates (in Java2D space)
@@ -195,7 +204,7 @@
 
     public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
                                 boolean shapes) {
-        this(positivePaint, negativePaint, shapes, CALCULATE_NO_AREA);
+        this(positivePaint, negativePaint, shapes, CALCULATE_ALL_AREA);
     }
 
     /**
@@ -237,6 +246,37 @@
         this.areaCalculationMode = areaCalculationMode;
     }
 
+    public boolean isLabelArea() {
+        return this.labelArea;
+    }
+
+    public void setLabelArea(boolean label) {
+        this.labelArea = label;
+    }
+
+
+    /** Set font to paint label with. */
+    public void setLabelFont(Font font) {
+        this.labelFont = font;
+    }
+
+
+    /** Get font with which label is painted. */
+    public Font getLabelFont() {
+        return this.labelFont;
+    }
+
+    
+    /** Set color with which to paint label. */
+    public void setLabelColor(Color color) {
+        this.labelColor = color;
+    }
+
+
+    /** Get color with which label is painted. */
+    public Color getLabelColor() {
+        return this.labelColor;
+    }
 
 
     public double getCalculatedArea() {
@@ -807,6 +847,32 @@
                     plot, domainAxis, rangeAxis,
                     dataset, series, item, crosshairState);
         }
+
+        // Find geometric middle, calculate area and paint a string with it here.
+        // TODO also i18n
+        if (pass == 1 && this.labelArea) {
+            float center_x = 100f;
+            float center_y = 100f+ pass*50f;
+            float area = 0f;
+            if (areaCalculationMode == CALCULATE_POSITIVE_AREA
+               || areaCalculationMode == CALCULATE_ALL_AREA) {
+                area += Math.abs(positiveArea);
+            }
+            if (areaCalculationMode == CALCULATE_NEGATIVE_AREA
+               || areaCalculationMode == CALCULATE_ALL_AREA) {
+                area += Math.abs(negativeArea);
+            }
+            // TODO respect text bg settings.
+            if (area != 0f) {
+                Color oldColor = g2.getColor();
+                Font oldFont = g2.getFont();
+                g2.setFont(labelFont);
+                g2.setColor(labelColor);
+                g2.drawString("Area= "+area+"m2", center_x, center_y);
+                g2.setFont(oldFont);
+                g2.setColor(oldColor);
+            }
+        }
     }
 
     /**
@@ -1434,6 +1500,7 @@
                 || (l_subtrahendLast < l_minuendFirst));
     }
 
+
     public static double calculateArea(Object [] xValues, Object [] yValues) {
         double area = 0d;
 
@@ -1483,19 +1550,9 @@
         Object[] l_xValues = x_xValues.toArray();
         Object[] l_yValues = x_yValues.toArray();
 
-        int acm = areaCalculationMode;
-
-        if (acm != CALCULATE_NO_AREA) {
-            if ((x_positive && ((acm|CALCULATE_POSITIVE_AREA)
-                    == CALCULATE_POSITIVE_AREA))
-            || (!x_positive && ((acm|CALCULATE_NEGATIVE_AREA)
-                    == CALCULATE_NEGATIVE_AREA))
-            ) {
-                double area = calculateArea(l_xValues, l_yValues);
-                if (x_positive) positiveArea += area;
-                else            negativeArea += area;
-            }
-        }
+        double area = calculateArea(l_xValues, l_yValues);
+        if (x_positive) positiveArea += area;
+        else            negativeArea += area;
 
         GeneralPath l_path = new GeneralPath();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java	Thu May 03 20:23:02 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java	Thu May 03 20:27:01 2012 +0000
@@ -11,6 +11,7 @@
 import org.jfree.data.xy.XYSeriesCollection;
 
 import de.intevation.flys.utils.ThemeUtil;
+import de.intevation.flys.utils.ThemeAccess;
 import de.intevation.flys.jfree.StableXYDifferenceRenderer;
 
 
@@ -68,7 +69,18 @@
         applyShowShape(renderer);
         applyOutlineColor(renderer);
         applyOutlineStyle(renderer);
-
+        applyShowArea(renderer);
+        if (mode == FILL_MODE.UNDER) {
+            renderer.setAreaCalculationMode(StableXYDifferenceRenderer.CALCULATE_NEGATIVE_AREA);
+        }
+        else if (mode == FILL_MODE.ABOVE) {
+            renderer.setAreaCalculationMode(StableXYDifferenceRenderer.CALCULATE_POSITIVE_AREA);
+        }
+        else {
+            renderer.setAreaCalculationMode(StableXYDifferenceRenderer.CALCULATE_ALL_AREA);
+        }
+        // TODO apply all the text style for the calc. area label.
+        new ThemeAccess(theme).parseTextStyle().apply(renderer);
         return renderer;
     }
 
@@ -124,6 +136,11 @@
         int size = ThemeUtil.parseLineWidth(theme);
     }
 
+    /** Inform renderer whether it should draw a label. */
+    protected void applyShowArea(StableXYDifferenceRenderer renderer) {
+        renderer.setLabelArea(ThemeUtil.parseShowArea(theme));
+    }
+
     protected void applyOutlineStyle(StableXYDifferenceRenderer renderer) {
         float[] dashes = ThemeUtil.parseLineStyle(theme);
         int size       = ThemeUtil.parseLineWidth(theme);
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Thu May 03 20:23:02 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Thu May 03 20:27:01 2012 +0000
@@ -5,6 +5,8 @@
 
 import org.w3c.dom.Document;
 
+import de.intevation.flys.jfree.StableXYDifferenceRenderer;
+
 import org.jfree.chart.annotations.XYTextAnnotation;
 
 
@@ -161,6 +163,11 @@
                 ta.setRotationAngle(0f*Math.PI/180f);
             }
         }
+
+        public void apply(StableXYDifferenceRenderer renderer) {
+            renderer.setLabelColor(textColor);
+            renderer.setLabelFont(font);
+        }
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java	Thu May 03 20:23:02 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java	Thu May 03 20:27:01 2012 +0000
@@ -82,6 +82,9 @@
     public final static String XPATH_TRANSPARENCY =
         "/theme/field[@name='transparent']/@default";
 
+    public final static String XPATH_SHOW_AREA =
+        "/theme/field[@name='showarea']/@default";
+
     public final static String XPATH_TEXT_COLOR =
         "/theme/field[@name='textcolor']/@default";
 
@@ -245,6 +248,14 @@
         return parseBoolean(show, false);
     }
 
+    /**
+     * Parses the attribute 'showarea', defaults to false.
+     * @param theme The theme.
+     */
+    public static boolean parseShowArea(Document theme) {
+        String show = XMLUtils.xpathString(theme, XPATH_SHOW_AREA, null);
+        return parseBoolean(show, false);
+    }
 
     /**
      * Parses the attribute 'showlines', defaults to true.

http://dive4elements.wald.intevation.org