Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/jfree/AnnotationHelper.java @ 8341:130160b8d245
Introduce annotations, which know to which axis they belong.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 30 Sep 2014 11:50:43 +0200 |
parents | c086b06b81e5 |
children | 31272d799dad |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/jfree/AnnotationHelper.java Tue Sep 30 10:57:58 2014 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/jfree/AnnotationHelper.java Tue Sep 30 11:50:43 2014 +0200 @@ -10,7 +10,10 @@ import org.dive4elements.river.themes.ThemeDocument; import java.util.List; +import java.util.ArrayList; import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; import java.awt.BasicStroke; import java.awt.Color; @@ -22,6 +25,7 @@ import org.jfree.chart.LegendItemCollection; import org.jfree.chart.annotations.XYTextAnnotation; import org.jfree.chart.annotations.XYLineAnnotation; +import org.jfree.chart.renderer.xy.XYItemRenderer; import org.dive4elements.river.themes.LineStyle; import org.dive4elements.river.themes.TextStyle; @@ -41,16 +45,48 @@ public static final int DEFAULT_FONT_SIZE = 12; public static final String DEFAULT_FONT_NAME = "Tahoma"; + + public static void addYAnnotationsToRenderer( + SortedMap<Integer, RiverAnnotation> yAnnotations, + XYPlot plot, + ChartSettings settings, + Map<Integer, AxisDataset> datasets + ) { + List<RiverAnnotation> annotations = new ArrayList<RiverAnnotation>(); + + for (Map.Entry<Integer, RiverAnnotation> entry: + yAnnotations.entrySet()) { + int axis = entry.getKey(); + AxisDataset dataset = datasets.get(new Integer(axis)); + + if (dataset == null || dataset.getRange() == null) { + log.warn("No dataset available and active for axis " + axis); + } + else { + RiverAnnotation ya = entry.getValue(); + for (StickyAxisAnnotation sta: ya.getAxisTextAnnotations()) { + sta.setAxisSymbol(axis); + } + annotations.add(ya); + } + } + + addAnnotationsToRenderer(annotations, plot, settings, datasets); + } + /** * Add annotations (Sticky, Text and hyk zones) to a plot. * @param annotations Annotations to add - * @param plot Plot to add annotations to. + * @param plot XYPlot to add annotations to. * @param settings ChartSettings object for settings. + * @param datasets Map of axis index and datasets */ - public static void addAnnotationsToRenderer(List<RiverAnnotation> annotations, - XYPlot plot, ChartSettings settings, Map<Integer, AxisDataset> datasets) { - log.debug("addAnnotationsToRenderer"); - + public static void addAnnotationsToRenderer( + List<RiverAnnotation> annotations, + XYPlot plot, + ChartSettings settings, + Map<Integer, AxisDataset> datasets + ) { if (annotations == null || annotations.isEmpty()) { log.debug("addAnnotationsToRenderer: no annotations."); return; @@ -69,7 +105,7 @@ TextStyle textStyle = null; LineStyle lineStyle = null; - // Get Themeing information and add legend item. + // Get Theming information and add legend item. if (theme != null) { textStyle = theme.parseComplexTextStyle(); lineStyle = theme.parseComplexLineStyle(); @@ -150,7 +186,14 @@ XYLineAnnotation lineAnnotation = null; XYTextAnnotation textAnnotation = null; - int rendererIndex = 0; + int axisIndex = annotation.getAxisSymbol(); + XYItemRenderer renderer = null; + if (dataset.getDatasets().length > 0) { + renderer = plot.getRendererForDataset(dataset.getDatasets()[0]); + } + else { + renderer = plot.getRenderer(); + } if (annotation.atX()) { textAnnotation = new CollisionFreeXYTextAnnotation( @@ -163,31 +206,25 @@ textAnnotation.setTextAnchor(TextAnchor.CENTER_LEFT); } else { - // 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). - if (dataset == null) { - log.warn("Annotation should stick to unfindable y-axis: " - + annotation.getAxisSymbol()); - rendererIndex = 0; - } - else { - rendererIndex = dataset.getPlotAxisIndex(); - } - // Stick to the "right" (opposed to left) Y-Axis. - if (rendererIndex != 0) { + if (axisIndex != 0 && plot.getRangeAxis(axisIndex) != null) { // 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. - ChartArea area2 = new ChartArea(plot.getDomainAxis(), plot.getRangeAxis(rendererIndex)); textAnnotation = new CollisionFreeXYTextAnnotation( - annotation.getText(), area2.ofRight(TEXT_OFF), annotation.getPos()); + annotation.getText(), + area.ofRight(TEXT_OFF), + annotation.getPos() + ); textAnnotation.setRotationAnchor(TextAnchor.CENTER_RIGHT); textAnnotation.setTextAnchor(TextAnchor.CENTER_RIGHT); lineAnnotation = createRightStickAnnotation( - area2, annotation.getPos(), lineStyle); + area, annotation.getPos(), lineStyle); + + // hit-lines for duration curve + ChartArea area2 = new ChartArea( + plot.getDomainAxis(), plot.getRangeAxis(axisIndex)); if (!Float.isNaN(annotation.getHitPoint()) && theme != null) { // New line annotation to hit curve. if (theme.parseShowVerticalLine()) { @@ -196,7 +233,7 @@ StickyAxisAnnotation.SimpleAxis.X_AXIS, annotation.getHitPoint(), annotation.getPos(),// annotation.getHitPoint(), area2, lineStyle); - plot.getRenderer(rendererIndex).addAnnotation(hitLineAnnotation, + renderer.addAnnotation(hitLineAnnotation, org.jfree.ui.Layer.BACKGROUND); } if (theme.parseShowHorizontalLine()) { @@ -205,7 +242,7 @@ StickyAxisAnnotation.SimpleAxis.Y_AXIS2, annotation.getPos(), annotation.getHitPoint(), area2, lineStyle); - plot.getRenderer(rendererIndex).addAnnotation(lineBackAnnotation, + renderer.addAnnotation(lineBackAnnotation, org.jfree.ui.Layer.BACKGROUND); } } @@ -224,7 +261,7 @@ StickyAxisAnnotation.SimpleAxis.Y_AXIS, annotation.getPos(), annotation.getHitPoint(), area, lineStyle); - plot.getRenderer(rendererIndex).addAnnotation(hitLineAnnotation, + renderer.addAnnotation(hitLineAnnotation, org.jfree.ui.Layer.BACKGROUND); } if (theme.parseShowVerticalLine()) { @@ -233,7 +270,7 @@ StickyAxisAnnotation.SimpleAxis.X_AXIS, annotation.getHitPoint(), annotation.getPos(), area, lineStyle); - plot.getRenderer(rendererIndex).addAnnotation(lineBackAnnotation, + renderer.addAnnotation(lineBackAnnotation, org.jfree.ui.Layer.BACKGROUND); } } @@ -246,10 +283,8 @@ } // Add the Annotations to renderer. - plot.getRenderer(rendererIndex).addAnnotation(textAnnotation, - org.jfree.ui.Layer.FOREGROUND); - plot.getRenderer(rendererIndex).addAnnotation(lineAnnotation, - org.jfree.ui.Layer.FOREGROUND); + renderer.addAnnotation(textAnnotation, org.jfree.ui.Layer.FOREGROUND); + renderer.addAnnotation(lineAnnotation, org.jfree.ui.Layer.FOREGROUND); } /** @@ -288,8 +323,8 @@ // Style the line. if (lineStyle != null) { return new XYLineAnnotation( + area.atRight(), pos, area.ofRight(ANNOTATIONS_AXIS_OFFSET), pos, - area.atRight(), pos, new BasicStroke(lineStyle.getWidth()), lineStyle.getColor()); } else {