diff flys-artifacts/src/main/java/de/intevation/flys/jfree/StickyAxisAnnotation.java @ 1085:07878836ee0d

Plot "real" (respecting parameters) W and Q MainValues, yet in wrong scale. flys-artifacts/trunk@2588 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 26 Aug 2011 08:49:18 +0000
parents f16b66839e59
children e298c4d28927
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/StickyAxisAnnotation.java	Fri Aug 26 08:07:47 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/StickyAxisAnnotation.java	Fri Aug 26 08:49:18 2011 +0000
@@ -11,7 +11,6 @@
 import org.jfree.chart.annotations.XYTextAnnotation;
 import org.jfree.chart.axis.ValueAxis;
 import org.jfree.chart.util.LineUtilities;
-import org.jfree.text.TextUtilities;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.entity.XYAnnotationEntity;
@@ -19,6 +18,10 @@
 import org.jfree.chart.ChartRenderingInfo;
 import org.jfree.chart.plot.Plot;
 
+import org.jfree.data.Range;
+
+import org.jfree.text.TextUtilities;
+
 import org.jfree.ui.RectangleEdge;
 import org.jfree.ui.TextAnchor;
 
@@ -43,6 +46,9 @@
     /** Which axis to stick to. */
     protected SimpleAxis stickyAxis = SimpleAxis.X_AXIS;
 
+    /** The 1-dimensional position of this annotation. */
+    protected float pos;
+
 
     /**
      * Trivial constructor.
@@ -50,20 +56,39 @@
      * @param text Text to display.
      * @param x    X-position in dataspace (typical horizontal, in km).
      * @param y    Y-position in dataspace (typical vertical, in m).
+     * @deprecated
      */
     public StickyAxisAnnotation(String text, float x, float y) {
         super(text, x, y);
         setStickyAxis(SimpleAxis.X_AXIS);
     }
 
+
+    /**
+     * Constructor with given explicit axis.
+     * @param text       the text to display.
+     * @param pos        the position at which to draw the text and mark.
+     * @param stickyAxis the axis at which to stick (and to which 'pos' is
+     *                   relative).
+     */
+    public StickyAxisAnnotation(String text, float pos, SimpleAxis stickAxis) {
+        super(text, pos, pos);
+        setStickyAxis(stickAxis);
+        this.pos = pos;
+    }
+
+    /**
+     * Legacy-Constructor.
+     * @deprecated
+     */
     public StickyAxisAnnotation(String text, float x, float y,
             SimpleAxis stickAxis) {
         super(text, x, y);
         setStickyAxis(stickAxis);
+        this.pos = x;
     }
 
 
-
     /**
      * Sets the "sticky axis" (whether to draw annotations at the
      * X- or the Y-Axis.
@@ -76,6 +101,10 @@
             this.setRotationAngle(270f * (Math.PI / 180f));
             this.setRotationAnchor(TextAnchor.CENTER_LEFT);
             this.setTextAnchor(TextAnchor.CENTER_LEFT);
+        } else {
+            this.setRotationAngle(0f * (Math.PI / 180f));
+            this.setRotationAnchor(TextAnchor.CENTER_LEFT);
+            this.setTextAnchor(TextAnchor.CENTER_LEFT);
         }
     }
 
@@ -131,15 +160,28 @@
         g2.setPaint(this.paint);
         g2.setStroke(this.stroke);
         */
-        j2DY1 = (float) RectangleEdge.coordinate(dataArea, domainEdge);
-        double rangeLow = rangeAxis.getRange().getLowerBound();
-        // Line ends at 1.5% of full distance.
-        j2DY2 = (float) rangeAxis.valueToJava2D(
-                  (1f - 0.015f) * rangeLow + 0.015f * 
-                  rangeAxis.getRange().getUpperBound(),
-                 dataArea, rangeEdge);
-        j2DX1 = (float) domainAxis.valueToJava2D(x, dataArea, domainEdge);
-        j2DX2 = j2DX1;
+        if (this.stickyAxis == SimpleAxis.X_AXIS) {
+            j2DY1 = (float) RectangleEdge.coordinate(dataArea, domainEdge);
+            double rangeLow = rangeAxis.getRange().getLowerBound();
+            // Line ends at 1.5% of full distance.
+            j2DY2 = (float) rangeAxis.valueToJava2D(
+                      (1f - 0.015f) * rangeLow + 0.015f * 
+                      rangeAxis.getRange().getUpperBound(),
+                     dataArea, rangeEdge);
+            j2DX1 = (float) domainAxis.valueToJava2D(x, dataArea, domainEdge);
+            j2DX2 = j2DX1;
+        } else {
+            j2DX1 = (float) RectangleEdge.coordinate(dataArea, rangeEdge);
+            Range domainRange = domainAxis.getRange();
+            double rangeLow = domainRange.getLowerBound();
+            // Line ends at 1.5% of full distance.
+            j2DX2 = (float) domainAxis.valueToJava2D(
+                      (1f - 0.015f) * rangeLow + 0.015f * 
+                      domainRange.getUpperBound(),
+                     dataArea, domainEdge);
+            j2DY1 = (float) rangeAxis.valueToJava2D(pos, dataArea, rangeEdge);
+            j2DY2 = j2DY1;
+        }
 
         Line2D line = new Line2D.Float(j2DX1, j2DY1, j2DX2, j2DY2);
 
@@ -187,10 +229,29 @@
             plot.getDomainAxisLocation(), orientation);
         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
             plot.getRangeAxisLocation(), orientation);
-        float anchorX = (float) domainAxis.valueToJava2D(
-            getX(), dataArea, domainEdge);
-        float anchorY = (float) rangeAxis.valueToJava2D(
-            getY(), dataArea, rangeEdge);
+        float anchorX = 0f;
+        float anchorY = 0.0f;
+        if (this.stickyAxis == SimpleAxis.X_AXIS) {
+            // Text starts at 1.5% of full distance.
+            float rangeLow = (float) rangeAxis.getRange().getLowerBound();
+            float y = rangeLow + 0.02f * ((float)
+                    rangeAxis.getRange().getUpperBound() - rangeLow);
+            setY(y);
+
+            anchorX = (float) domainAxis.valueToJava2D(
+                getX(), dataArea, domainEdge);
+            anchorY = (float) rangeAxis.valueToJava2D(
+                getY(), dataArea, rangeEdge);
+        } else {
+            float rangeLow = (float) domainAxis.getRange().getLowerBound();
+            float x = rangeLow + 0.02f * ((float)
+                    domainAxis.getRange().getUpperBound() - rangeLow);
+            setX(x);
+            anchorX = (float) domainAxis.valueToJava2D(
+                getX(), dataArea, domainEdge);
+            anchorY = (float) rangeAxis.valueToJava2D(
+                getY(), dataArea, rangeEdge);
+        }
         if (orientation == PlotOrientation.HORIZONTAL) {
             float tempAnchor = anchorX;
             anchorX = anchorY;

http://dive4elements.wald.intevation.org