diff flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 1679:69929c471646

Improved the creation/rendering of annotations (km favorites, mainvalues). flys-artifacts/trunk@2896 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 05 Oct 2011 14:23:53 +0000
parents 4a8251eae217
children bdb05dc9b763
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Wed Oct 05 13:16:44 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Wed Oct 05 14:23:53 2011 +0000
@@ -8,14 +8,23 @@
 
 import java.text.NumberFormat;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.w3c.dom.Document;
+
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.LegendItem;
+import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.annotations.XYTextAnnotation;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.axis.ValueAxis;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 import org.jfree.data.Range;
 import org.jfree.data.xy.XYDataset;
@@ -25,6 +34,8 @@
 import org.jfree.ui.RectangleInsets;
 
 import de.intevation.flys.exports.ChartExportHelper;
+import de.intevation.flys.jfree.FLYSAnnotation;
+import de.intevation.flys.utils.ThemeUtil;
 
 
 /**
@@ -44,6 +55,10 @@
     /** SeriesCollection used for the second axis. */
     protected XYSeriesCollection second;
 
+    /** List of annotations to insert in plot. */
+    protected List<FLYSAnnotation> annotations;
+
+
     public static final Color DEFAULT_GRID_COLOR      = Color.GRAY;
     public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;
 
@@ -106,6 +121,7 @@
         XYPlot plot = (XYPlot) chart.getPlot();
 
         addDatasets(plot);
+        addAnnotations(plot);
         addSubtitles(chart);
         adjustPlot(plot);
         adjustAxes(plot);
@@ -155,6 +171,15 @@
     }
 
 
+    public void addAnnotations(FLYSAnnotation annotation) {
+        if (annotations == null) {
+            annotations = new ArrayList<FLYSAnnotation>();
+        }
+
+        annotations.add(annotation);
+    }
+
+
     private void removeEmptyRangeAxes(XYPlot plot) {
         if (first == null) {
             plot.setRangeAxis(0, null);
@@ -292,6 +317,46 @@
     }
 
 
+    protected void addAnnotations(XYPlot plot) {
+        plot.clearAnnotations();
+
+        if (annotations == null) {
+            logger.debug("No Annotations given.");
+            return;
+        }
+
+        LegendItemCollection lic = new LegendItemCollection();
+
+        int idx = 0;
+        if (plot.getRangeAxis(idx) == null && plot.getRangeAxisCount() >= 2) {
+            idx = 1;
+        }
+
+        XYItemRenderer renderer = plot.getRenderer(idx);
+
+        for (FLYSAnnotation fa: annotations) {
+            Document theme = fa.getTheme();
+
+            Color color = theme != null
+                ? ThemeUtil.parseLineColorField(theme)
+                : null;
+
+            if (color == null) {
+                color = Color.black;
+            }
+
+            lic.add(new LegendItem(fa.getLabel(), color));
+
+            for (XYTextAnnotation ta: fa.getAnnotations()) {
+                ta.setPaint(color);
+                renderer.addAnnotation(ta);
+            }
+
+            plot.setFixedLegendItems(lic);
+        }
+    }
+
+
     /**
      * Adjusts the axes of a plot.
      *
@@ -406,6 +471,9 @@
 
 
     protected void applyThemes(XYPlot plot, XYSeriesCollection dataset, int i) {
+        LegendItemCollection lic  = new LegendItemCollection();
+        LegendItemCollection anno = plot.getFixedLegendItems();
+
         XYLineAndShapeRenderer r = getRenderer(plot, i);
 
         for (int s = 0, num = dataset.getSeriesCount(); s < num; s++) {
@@ -414,8 +482,16 @@
             if (series instanceof StyledXYSeries) {
                 ((StyledXYSeries) series).applyTheme(r, s);
             }
+
+            lic.add(r.getLegendItem(i, s));
         }
 
+        if (anno != null) {
+            lic.addAll(anno);
+        }
+
+        plot.setFixedLegendItems(lic);
+
         plot.setRenderer(i, r);
     }
 

http://dive4elements.wald.intevation.org