diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 2163:105097966111

Theoretically allow annotations on second y ais. Practically allow Q MainValues on Q Axis in Duration Curves. flys-artifacts/trunk@3750 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 23 Jan 2012 14:18:53 +0000
parents c68f4f227c09
children a79d5cd26083
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Mon Jan 23 11:06:06 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Mon Jan 23 14:18:53 2012 +0000
@@ -90,6 +90,8 @@
         protected List<XYDataset> datasets;
         /** Range to use to include all given datasets. */
         protected Range range;
+        /** Index of axis in plot. */
+        protected int plotAxisIndex;
 
         /** Create AxisDataset. */
         public AxisDataset(int symb) {
@@ -136,6 +138,16 @@
         public boolean isEmpty() {
             return this.datasets.isEmpty();
         }
+
+        /** Set the 'real' axis index that this axis is mapped to. */
+        public void setPlotAxisIndex(int axisIndex) {
+            this.plotAxisIndex = axisIndex;
+        }
+
+        /** Get the 'real' axis index that this axis is mapped to. */
+        public int getPlotAxisIndex() {
+            return this.plotAxisIndex;
+        }
     } // class AxisDataset
 
 
@@ -618,6 +630,7 @@
                         axisDataset.isArea((XYSeriesCollection)dataset));
                     datasetIndex++;
                 }
+                axisDataset.setPlotAxisIndex(axisIndex);
                 axisIndex++;
             }
         }
@@ -626,8 +639,9 @@
 
     /**
      * Registers an area to be drawn.
-     * @param lower the lower curve to draw the area from.
-     * @param upper the upper curve to draw the ara from.
+     * @param area Area to be drawn.
+     * @param index 'axis index'
+     * @param visible Whether or not to be visible (important for range calculations).
      */
     public void addAreaSeries(StyledAreaSeriesCollection area, int index, boolean visible) {
         if (area == null) {
@@ -655,7 +669,7 @@
      * Add given series if visible, if not visible adjust ranges (such that
      * all points in data would be plotted once visible).
      * @param series the dataseries to include in plot.
-     * @param index  index of the series and of its axis.
+     * @param index  ('symbolic') index of the series and of its axis.
      * @param visible whether or not the data should be plotted.
      */
     public void addAxisSeries(XYSeries series, int index, boolean visible) {
@@ -982,62 +996,6 @@
     }
 
 
-    /**
-     * Get "lowest" X Value for first axis. This value is exactly at the
-     * border of the plot.
-     * @return lowest value on first 'x'-axis.
-     */
-    protected double getLowestXValue(XYPlot plot) {
-        ValueAxis axis = plot.getDomainAxis();
-        if (axis == null) {
-            logger.warn("No X-Axis to find lowest value for.");
-        }
-        return axis.getRange().getLowerBound();
-    }
-
-
-    /**
-     * Get "lowest" X Value for first axis. This value is exactly at the
-     * border of the plot.
-     * @return highest value on first 'x'-axis.
-     */
-    protected double getUppestXValue(XYPlot plot) {
-        ValueAxis axis = plot.getDomainAxis();
-        if (axis == null) {
-            logger.warn("No first Y-Axis to find uppest value for.");
-        }
-        return axis.getRange().getUpperBound();
-    }
-
-
-    /**
-     * Get "lowest" Y Value for first axis. This value is exactly at the
-     * border of the plot.
-     * @return lowest value on first 'y'-axis.
-     */
-    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.
-     * @return highest value on first 'y'-axis.
-     */
-    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")) {
@@ -1081,6 +1039,8 @@
         XYLineAnnotation lineAnnotation = null;
         XYTextAnnotation textAnnotation = null;
 
+        int rendererIndex = 0;
+
         if (annotation.atX()) {
             textAnnotation = new CollisionFreeXYTextAnnotation(
                 annotation.getText(), annotation.getPos(), area.ofGround(TEXT_OFF));
@@ -1091,25 +1051,69 @@
                 lineAnnotation = new XYLineAnnotation(annotation.getPos(),
                     area.atGround(), annotation.getPos(), area.ofGround(LINE_OFF),
                     new BasicStroke(lineStyle.getWidth()),lineStyle.getColor());
+                textAnnotation.setRotationAnchor(TextAnchor.CENTER_LEFT);
+                textAnnotation.setTextAnchor(TextAnchor.CENTER_LEFT);
             }
             else {
                 lineAnnotation = new XYLineAnnotation(annotation.getPos(),
                     area.atGround(), annotation.getPos(), area.ofGround(LINE_OFF));
+                textAnnotation.setRotationAnchor(TextAnchor.CENTER_LEFT);
+                textAnnotation.setTextAnchor(TextAnchor.CENTER_LEFT);
             }
         }
         else {
-            textAnnotation = new CollisionFreeXYTextAnnotation(
-                annotation.getText(), area.ofLeft(TEXT_OFF), annotation.getPos());
-            // Style the line.
-            if (lineStyle != null) {
-                lineAnnotation = new XYLineAnnotation(area.atLeft(),
-                    annotation.getPos(), area.ofLeft(LINE_OFF),
-                    annotation.getPos(), new BasicStroke(lineStyle.getWidth()),
-                    lineStyle.getColor());
+            // Do the more complicated case where we stick to the Y-Axis.
+            // There is one nasty case (duration curves, where annotations
+            // might stick to the second y-axis).
+            AxisDataset dataset = this.datasets.get(
+                new Integer(annotation.getAxisSymbol()));
+            if (dataset == null) {
+                logger.warn("Annotation should stick to unfindable y-axis: "
+                    + annotation.getAxisSymbol());
+                rendererIndex = 0;
             }
             else {
-                lineAnnotation = new XYLineAnnotation(area.atLeft(),
-                    annotation.getPos(), area.ofLeft(LINE_OFF), annotation.getPos());
+                rendererIndex = dataset.getPlotAxisIndex();
+            }
+
+            if (rendererIndex != 0) {
+                // OPTIMIZE: Pass a different area to this function,
+                //           do the adding to renderer outside (let this
+                //           function return the annotations).
+                //           Note that this path is travelled rarely.
+                Area area2 = new Area(plot.getDomainAxis(), plot.getRangeAxis(rendererIndex));
+                textAnnotation = new CollisionFreeXYTextAnnotation(
+                    annotation.getText(), area2.ofRight(TEXT_OFF), annotation.getPos());
+                textAnnotation.setRotationAnchor(TextAnchor.CENTER_RIGHT);
+                textAnnotation.setTextAnchor(TextAnchor.CENTER_RIGHT);
+                // Style the line.
+                if (lineStyle != null) {
+                    lineAnnotation = new XYLineAnnotation(area2.ofRight(LINE_OFF),
+                        annotation.getPos(), area2.atRight(),
+                        annotation.getPos(), new BasicStroke(lineStyle.getWidth()),
+                        lineStyle.getColor());
+                }
+                else {
+                    lineAnnotation = new XYLineAnnotation(area2.atRight(),
+                        annotation.getPos(), area2.ofRight(LINE_OFF), annotation.getPos());
+                }
+            }
+            else {
+                textAnnotation = new CollisionFreeXYTextAnnotation(
+                    annotation.getText(), area.ofLeft(TEXT_OFF), annotation.getPos());
+                textAnnotation.setRotationAnchor(TextAnchor.CENTER_LEFT);
+                textAnnotation.setTextAnchor(TextAnchor.CENTER_LEFT);
+                // Style the line.
+                if (lineStyle != null) {
+                    lineAnnotation = new XYLineAnnotation(area.atLeft(),
+                        annotation.getPos(), area.ofLeft(LINE_OFF),
+                        annotation.getPos(), new BasicStroke(lineStyle.getWidth()),
+                        lineStyle.getColor());
+                }
+                else {
+                    lineAnnotation = new XYLineAnnotation(area.atLeft(),
+                        annotation.getPos(), area.ofLeft(LINE_OFF), annotation.getPos());
+                }
             }
         }
 
@@ -1119,12 +1123,9 @@
         }
 
         // Add the Annotations to renderer.
-        textAnnotation.setRotationAnchor(TextAnchor.CENTER_LEFT);
-        textAnnotation.setTextAnchor(TextAnchor.CENTER_LEFT);
-
-        plot.getRenderer().addAnnotation(textAnnotation,
+        plot.getRenderer(rendererIndex).addAnnotation(textAnnotation,
             org.jfree.ui.Layer.BACKGROUND);
-        plot.getRenderer().addAnnotation(lineAnnotation,
+        plot.getRenderer(rendererIndex).addAnnotation(lineAnnotation,
             org.jfree.ui.Layer.BACKGROUND);
     }
 
@@ -1635,11 +1636,21 @@
             this.yRange = rangeY;
         }
 
+        public Area(ValueAxis axisX, ValueAxis axisY) {
+            this.xRange = axisX.getRange();
+            this.yRange = axisY.getRange();
+        }
+
         public double ofLeft(double percent) {
             return xRange.getLowerBound()
                 + xRange.getLength() * percent;
         }
 
+        public double ofRight(double percent) {
+            return xRange.getUpperBound()
+                - xRange.getLength() * percent;
+        }
+
         public double ofGround(double percent) {
             return yRange.getLowerBound()
                 + yRange.getLength() * percent;
@@ -1653,6 +1664,10 @@
             return yRange.getLowerBound();
         }
 
+        public double atRight() {
+            return xRange.getUpperBound();
+        }
+
         public double atLeft() {
             return xRange.getLowerBound();
         }

http://dive4elements.wald.intevation.org