diff flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java @ 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 b5cc53a84b66
children b75681c09ef8
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();
 

http://dive4elements.wald.intevation.org