changeset 2667:b75681c09ef8

Respect area label bg text style, draw label roughly at centroid of polygons (sofar, better but not yet good). flys-artifacts/trunk@4346 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 04 May 2012 13:47:37 +0000
parents 6da7e064ae90
children 53f42adf4505
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/themes.xml 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
diffstat 5 files changed, 99 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu May 03 20:27:01 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri May 04 13:47:37 2012 +0000
@@ -1,3 +1,32 @@
+2012-05-04	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	* doc/conf/themes: Add text-bg properties to  Area style.
+
+	* src/main/java/de/intevation/flys/themes/ThemeAccess.java
+	  (TextStyle.apply): Apply text bg theme-properties to renderer.
+
+	* src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java:
+	  Cosmetics.
+
+	* src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java:
+	  Added field to store labels background color, calculate arithmetic
+	  middles of polygons, to put label there (improves situation slightly).
+
+2012-05-03	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/ThemeUtil.java
+	  (parseShowArea): parse show area field of theme.
+
+	* src/main/java/de/intevation/flys/themes/ThemeAccess.java
+	  (TextStyle.apply): Be applicable to StableXYDifferenceRenderers.
+
+	* src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java:
+	  Apply setting from theme to renderer.
+
+	* src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java:
+	  Simplified area calculation (always calculate). Added getters and
+	  setters for text for label style, respect most of that.
+
 2012-05-03	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* doc/conf/themes: Let Area style not inherit from colorlines (as labels
--- a/flys-artifacts/doc/conf/themes.xml	Thu May 03 20:27:01 2012 +0000
+++ b/flys-artifacts/doc/conf/themes.xml	Fri May 04 13:47:37 2012 +0000
@@ -840,6 +840,8 @@
             <inherit from="Text"/>
         </inherits>
         <fields>
+            <field name="backgroundcolor"  type="Color"   display="Texthintergrund"      default="255, 255, 255"/>
+            <field name="showbackground"   type="boolean" display="Hintergrund anzeigen" default="false"/>
             <field name="linecolor" type="Color" display="Linienfarbe" default="Color.BLACK"/>
             <field name="showlines" type="boolean" display="Linie anzeigen" default="true"/>
             <field name="linesize"  type="int"     display="Liniendicke"    default="1"/>
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Thu May 03 20:27:01 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java	Fri May 04 13:47:37 2012 +0000
@@ -82,6 +82,7 @@
 import java.awt.Graphics2D;
 import java.awt.Font;
 import java.awt.Paint;
+import java.awt.geom.Point2D;
 import java.awt.Shape;
 import java.awt.Stroke;
 import java.awt.geom.GeneralPath;
@@ -172,6 +173,9 @@
     /** The color of the label showing the calculated area. */
     protected Color labelColor;
 
+    /** The background color of the label showing the calculated area. */
+    protected Color labelBGColor;
+
     /** Font to draw label of calculated area with. */
     protected Font labelFont;
 
@@ -184,6 +188,14 @@
     protected boolean labelArea = true;
 
 
+    /** Arithmetic centroid of drawn polygons. */
+    protected Point2D.Double centroid;
+
+
+    /** Number of points that contributed to the centroid. */
+    protected int centroidNPoints = 0;
+
+
     /**
      * This flag controls whether or not the x-coordinates (in Java2D space)
      * are rounded to integers.  When set to true, this can avoid the vertical
@@ -236,6 +248,8 @@
         this.outlinePaint  = Color.black;
         this.drawOriginalSeries = false;
         this.areaCalculationMode = areaCalculationMode;
+        this.labelBGColor  = null;
+        this.centroid      = new Point2D.Double(0,0);
     }
 
     public int getAreaCalculationMode() {
@@ -279,6 +293,18 @@
     }
 
 
+    /** Set color with which to paint label bg. */
+    public void setLabelBGColor(Color color) {
+        this.labelBGColor = color;
+    }
+
+
+    /** Get color with which label is painted. */
+    public Color getLabelBGColor() {
+        return this.labelBGColor;
+    }
+
+
     public double getCalculatedArea() {
         return positiveArea + negativeArea;
     }
@@ -851,8 +877,15 @@
         // 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;
+            double center_x = centroid.getX();
+            double center_y = centroid.getY();
+            center_x = domainAxis.valueToJava2D(center_x, dataArea,
+                plot.getDomainAxisEdge());
+            center_y = rangeAxis.valueToJava2D(center_y, dataArea,
+                plot.getRangeAxisEdge());
+
+            // Respect text-extend if text should appear really centered.
+
             float area = 0f;
             if (areaCalculationMode == CALCULATE_POSITIVE_AREA
                || areaCalculationMode == CALCULATE_ALL_AREA) {
@@ -862,13 +895,17 @@
                || 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);
+                String labelText = "Area= " + area + "m2";
+                if (labelBGColor != null) {
+                    EnhancedLineAndShapeRenderer.drawTextBox(g2, labelText,
+                        (float)center_x, (float)center_y, labelBGColor);
+                }
                 g2.setColor(labelColor);
-                g2.drawString("Area= "+area+"m2", center_x, center_y);
+                g2.drawString(labelText, (float)center_x, (float)center_y);
                 g2.setFont(oldFont);
                 g2.setColor(oldColor);
             }
@@ -1501,6 +1538,26 @@
     }
 
 
+    public void updateCentroid(Object [] xValues, Object [] yValues) {
+        double x = 0d, y = 0d;
+
+        for (int i = 0, N = xValues.length; i < N; ++i) {
+            x += ((Double)xValues[i]).doubleValue();
+            y += ((Double)yValues[i]).doubleValue();
+        }
+
+        x /= xValues.length;
+        y /= yValues.length;
+
+        centroidNPoints++;
+        double factorNew = 1d / centroidNPoints;
+        double factorOld = 1d - factorNew;
+
+        centroid = new Point2D.Double((factorNew * x + factorOld * centroid.x),
+            (factorNew * y + factorOld * centroid.y));
+    }
+
+
     public static double calculateArea(Object [] xValues, Object [] yValues) {
         double area = 0d;
 
@@ -1513,6 +1570,7 @@
 
             area += xi*yj;
             area -= xj*yi;
+            // TODO centroid calculation here?
         }
 
         return 0.5d*area;
@@ -1553,6 +1611,7 @@
         double area = calculateArea(l_xValues, l_yValues);
         if (x_positive) positiveArea += area;
         else            negativeArea += area;
+        updateCentroid(l_xValues, l_yValues);
 
         GeneralPath l_path = new GeneralPath();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java	Thu May 03 20:27:01 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StyledAreaSeriesCollection.java	Fri May 04 13:47:37 2012 +0000
@@ -79,7 +79,8 @@
         else {
             renderer.setAreaCalculationMode(StableXYDifferenceRenderer.CALCULATE_ALL_AREA);
         }
-        // TODO apply all the text style for the calc. area label.
+
+        // Apply text style.
         new ThemeAccess(theme).parseTextStyle().apply(renderer);
         return renderer;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Thu May 03 20:27:01 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Fri May 04 13:47:37 2012 +0000
@@ -167,6 +167,9 @@
         public void apply(StableXYDifferenceRenderer renderer) {
             renderer.setLabelColor(textColor);
             renderer.setLabelFont(font);
+            if (this.showBg) {
+                renderer.setLabelBGColor(bgColor);
+            }
         }
     }
 }

http://dive4elements.wald.intevation.org