changeset 298:80f7c5dc09c6

Implementation of classes for creating timeseries, verticalprofile and horizontalprofile charts. gnv-artifacts/trunk@352 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 19 Nov 2009 15:30:27 +0000
parents 3f43392df3f6
children d6c75171f1e9
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/chart/HorizontalProfileChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java
diffstat 4 files changed, 379 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Thu Nov 19 15:24:45 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Thu Nov 19 15:30:27 2009 +0000
@@ -1,3 +1,15 @@
+2009-11-19  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java,
+	  src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java,
+	  src/main/java/de/intevation/gnv/chart/HorizontalProfileChart.java:
+	  Classes for creating charts of different output transitions, inherit from
+	  AbstractXYLineChart. The subclasses should only implement special code.
+	  General configurations should be done in AbstractChart or
+	  AbstractXYLineChart.
+
+	  TODO: Take care of gaps between two values.
+
 2009-11-19  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/chart/Chart.java: Interface, which
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/HorizontalProfileChart.java	Thu Nov 19 15:30:27 2009 +0000
@@ -0,0 +1,93 @@
+package de.intevation.gnv.chart;
+
+import java.util.Collection;
+import java.util.Locale;
+
+import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.io.WKTReader;
+import com.vividsolutions.jts.io.ParseException;
+
+import org.apache.log4j.Logger;
+
+import org.jfree.chart.ChartTheme;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.general.Series;
+import org.jfree.data.xy.XYSeries;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.utils.DistanceCalculator;
+
+
+/**
+ * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
+ */
+public class HorizontalProfileChart
+extends      VerticalProfileChart
+{
+    private static Logger log = Logger.getLogger(HorizontalProfileChart.class);
+
+    private static WKTReader wktReader = new WKTReader();
+    private        Point     lastPoint;
+    private        double    distance;
+
+
+    public HorizontalProfileChart(
+        ChartLabels labels,
+        ChartTheme  theme,
+        Collection  parameters,
+        Collection  measurements,
+        Collection  result,
+        Collection  dates,
+        Locale      locale
+    ) {
+        super(labels, theme, parameters, measurements, result, dates, locale);
+        this.PLOT_ORIENTATION = PlotOrientation.VERTICAL;
+        this.distance         = 0;
+    }
+
+
+    protected void addValue(Result row, Series series) {
+        // TODO look for gaps between two values
+        try {
+            Point point = (Point) wktReader.read(row.getString("SHAPE"));
+            if (lastPoint != null)
+                distance = distance + DistanceCalculator.calculateDistance(
+                    lastPoint, point
+                );
+            lastPoint = point;
+
+            ((XYSeries) series).add(
+                distance,
+                row.getDouble("YORDINATE")
+            );
+        }
+        catch(ParseException pe) {
+            log.warn("No data found while parsing.");
+        }
+    }
+
+
+    protected void addSeries(Series series, int idx) {
+        super.addSeries(series, idx);
+
+        // reset values used by current series for next series
+        lastPoint = null;
+        distance  = 0;
+    }
+
+
+    protected String createSeriesName(
+        String breakPoint1,
+        String breakPoint2,
+        String breakPoint3
+    ) {
+        log.debug("create seriesname of horizontalprofile chart");
+        return super.createSeriesName(
+            breakPoint1,
+            breakPoint2,
+            breakPoint3) +
+            " " +
+            findValueTitle(dates, breakPoint3);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=latin1 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java	Thu Nov 19 15:30:27 2009 +0000
@@ -0,0 +1,138 @@
+package de.intevation.gnv.chart;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Locale;
+
+import org.apache.log4j.Logger;
+
+import org.jfree.chart.ChartTheme;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.general.Series;
+import org.jfree.data.time.TimeSeries;
+import org.jfree.data.time.Minute;
+import org.jfree.data.time.TimeSeriesCollection;
+
+import de.intevation.gnv.geobackend.base.Result;
+
+
+/**
+ * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
+ */
+public class TimeSeriesChart
+extends      AbstractXYLineChart
+{
+
+    private static Logger log = Logger.getLogger(TimeSeriesChart.class);
+
+
+    public TimeSeriesChart(
+        ChartLabels labels,
+        ChartTheme  theme,
+        Collection  parameters,
+        Collection  measurements,
+        Collection  result,
+        Collection  dates,
+        Locale      locale
+    ) {
+        this.labels           = labels;
+        this.theme            = theme;
+        this.parameters       = parameters;
+        this.measurements     = measurements;
+        this.resultSet        = result;
+        this.dates            = dates;
+        this.locale           = locale;
+        this.PLOT_ORIENTATION = PlotOrientation.VERTICAL;
+    }
+
+
+    protected void initData() {
+        log.debug("init data for timeseries chart");
+
+        String  breakPoint1       = null;
+        String  breakPoint2       = null;
+        String  breakPoint3       = null;
+
+        Iterator   iter       = resultSet.iterator();
+        Result     row        = null;
+        String     seriesName = null;
+        TimeSeries series     = null;
+
+        int idx = 0;
+
+        while (iter.hasNext()) {
+            row = (Result) iter.next();
+
+            // add current data to plot and prepare for next one
+            if (!row.getString("GROUP1").equals(breakPoint1) ||
+                !row.getString("GROUP2").equals(breakPoint2) ||
+                !row.getString("GROUP3").equals(breakPoint3)
+            ) {
+                log.debug("prepare data/plot for next dataset");
+
+                if(series != null) {
+                    addSeries(series, idx);
+                    prepareAxis(((String)series.getKey()), idx++);
+                }
+
+                // prepare variables for next plot
+                breakPoint1 = row.getString("GROUP1");
+                breakPoint2 = row.getString("GROUP2");
+                breakPoint3 = row.getString("GROUP3");
+
+                seriesName = createSeriesName(
+                    breakPoint1,
+                    breakPoint2,
+                    breakPoint3
+                );
+
+                log.debug("next dataset is '" + seriesName + "'");
+                series = new TimeSeries(seriesName, Minute.class);
+            }
+
+            addValue(row, series);
+        }
+
+        // add the last dataset if existing to plot and prepare its axis
+        addSeries(series, idx);
+        prepareAxis(((String)series.getKey()), idx++);
+    }
+
+
+    protected void addValue(Result row, Series series) {
+        // TODO look for gaps between two values
+        ((TimeSeries) series).addOrUpdate(
+            new Minute(row.getDate("XORDINATE")),
+            row.getDouble("YORDINATE")
+        );
+    }
+
+
+    protected void addSeries(Series series, int idx) {
+        log.debug("add series to timeseries chart");
+        XYPlot plot = chart.getXYPlot();
+
+        if (series == null) {
+            log.warn("no data to add");
+            return;
+        }
+
+        TimeSeriesCollection tsc = new TimeSeriesCollection((TimeSeries)series);
+        plot.setDataset(idx, tsc);
+    }
+
+
+    protected String createSeriesName(
+        String breakPoint1,
+        String breakPoint2,
+        String breakPoint3
+    ) {
+        log.debug("create seriesname of timeseries chart");
+        return findValueTitle(parameters, breakPoint1) +
+            " " +
+            findValueTitle(measurements, breakPoint2) +
+            "m";
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=latin1 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java	Thu Nov 19 15:30:27 2009 +0000
@@ -0,0 +1,136 @@
+package de.intevation.gnv.chart;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Locale;
+
+import org.apache.log4j.Logger;
+
+import org.jfree.chart.ChartTheme;
+import org.jfree.data.xy.XYSeries;
+import org.jfree.data.general.Series;
+import org.jfree.data.xy.XYDataset;
+import org.jfree.data.xy.XYSeriesCollection;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.plot.PlotOrientation;
+
+import de.intevation.gnv.geobackend.base.Result;
+
+
+/**
+ * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
+ */
+public class VerticalProfileChart
+extends      AbstractXYLineChart
+{
+    private static Logger log = Logger.getLogger(VerticalProfileChart.class);
+
+
+    public VerticalProfileChart(
+        ChartLabels labels,
+        ChartTheme  theme,
+        Collection  parameters,
+        Collection  measurements,
+        Collection  result,
+        Collection  dates,
+        Locale      locale
+    ) {
+        this.labels           = labels;
+        this.theme            = theme;
+        this.parameters       = parameters;
+        this.measurements     = measurements;
+        this.resultSet        = result;
+        this.dates            = dates;
+        this.locale           = locale;
+        this.PLOT_ORIENTATION = PlotOrientation.HORIZONTAL;
+    }
+
+
+    protected void initData() {
+        log.debug("init data for VerticalProfileChart");
+
+        String  breakPoint1       = null;
+        String  breakPoint2       = null;
+        String  breakPoint3       = null;
+
+        Iterator iter       = resultSet.iterator();
+        Result   row        = null;
+        String   seriesName = null;
+        XYSeries series     = null;
+
+        int idx = 0;
+
+        while (iter.hasNext()) {
+            row = (Result) iter.next();
+
+            // add current data to plot and prepare for next one
+            if (!row.getString("GROUP1").equals(breakPoint1) ||
+                !row.getString("GROUP2").equals(breakPoint2) ||
+                !row.getString("GROUP3").equals(breakPoint3)
+            ) {
+                log.debug("prepare data/plot for next dataset");
+
+                if(series != null) {
+                    addSeries(series, idx);
+                    prepareAxis(((String)series.getKey()), idx++);
+                }
+
+                // prepare variables for next plot
+                breakPoint1 = row.getString("GROUP1");
+                breakPoint2 = row.getString("GROUP2");
+                breakPoint3 = row.getString("GROUP3");
+
+                seriesName  = createSeriesName(
+                    breakPoint1,
+                    breakPoint2,
+                    breakPoint3
+                );
+
+                log.debug("next dataset is '" + seriesName + "'");
+                series = new XYSeries(seriesName);
+            }
+
+            addValue(row, series);
+        }
+
+        addSeries(series, idx);
+        prepareAxis(((String)series.getKey()), idx++);
+    }
+
+
+    protected void addValue(Result row, Series series) {
+        // TODO look for gaps between two values
+        ((XYSeries) series).add(
+            row.getDouble("XORDINATE"),
+            row.getDouble("YORDINATE")
+        );
+    }
+
+
+    protected void addSeries(Series series, int idx) {
+        log.debug("add series to verticalprofile chart");
+        XYPlot plot = chart.getXYPlot();
+
+        if (series == null) {
+            log.warn("no data to add");
+            return;
+        }
+
+        XYSeriesCollection xysc = new XYSeriesCollection((XYSeries)series);
+        plot.setDataset(idx, xysc);
+    }
+
+
+    protected String createSeriesName(
+        String breakPoint1,
+        String breakPoint2,
+        String breakPoint3
+    ) {
+        log.debug("create seriesname of verticalprofile chart");
+        return findValueTitle(parameters, breakPoint1) +
+            " " +
+            findValueTitle(measurements, breakPoint2) +
+            "m";
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=latin1 :

http://dive4elements.wald.intevation.org