diff gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesVectorChart.java @ 1040:70653c29fc1d

Finished vector support for timeseriespoints (Issue27). gnv-artifacts/trunk@1112 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 19 May 2010 14:05:33 +0000
parents
children f953c9a559d8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesVectorChart.java	Wed May 19 14:05:33 2010 +0000
@@ -0,0 +1,142 @@
+package de.intevation.gnv.chart;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
+import java.util.Collection;
+import java.util.Date;
+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.data.time.Minute;
+import org.jfree.data.time.TimeSeries;
+import org.jfree.data.time.TimeSeriesCollection;
+
+
+/**
+ * This class is used to create timeseries charts that contain components of a
+ * vector parameter. The domain axis contains multiple date/time objects.
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class TimeSeriesVectorChart extends TimeSeriesChart {
+
+    private static Logger logger =Logger.getLogger(TimeSeriesVectorChart.class);
+
+    public TimeSeriesVectorChart(
+        ChartLabels labels,
+        ChartTheme  theme,
+        Collection  parameters,
+        Collection  measurements,
+        Collection  dates,
+        Collection  result,
+        Collection  timeGaps,
+        Locale      locale,
+        boolean     linesVisible,
+        boolean     shapesVisible
+    ) {
+        super(labels, theme, parameters, measurements, dates, result,
+              timeGaps, locale, linesVisible, shapesVisible);
+    }
+
+
+    @Override
+    protected void initData() {
+        logger.debug("init data for timeseries vector chart");
+
+        Iterator   iter       = resultSet.iterator();
+        Result     row        = null;
+        String     seriesName = null;
+        TimeSeries series     = null;
+
+        int  idx       = 0;
+        int  startPos  = 0;
+        int  endPos    = 0;
+        Date startDate = null;
+        Date endDate   = null;
+
+        ResultDescriptor rd = null;
+        int idxSeries       = -1;
+        int idxX            = -1;
+        int idxY            = -1;
+
+        Result[] results =
+            (Result[]) resultSet.toArray(new Result[resultSet.size()]);
+
+        while (iter.hasNext()) {
+            row = (Result) iter.next();
+
+            if (rd == null) {
+                rd        = row.getResultDescriptor();
+                idxSeries = rd.getColumnIndex("SERIES");
+                idxX      = rd.getColumnIndex("XORDINATE");
+                idxY      = rd.getColumnIndex("YORDINATE");
+            }
+
+            // add current data to plot and prepare for next one
+            if (!row.getString(idxSeries).equals(seriesName)) {
+                logger.debug("prepare data/plot for next dataset");
+
+                if(series != null) {
+                    // add gaps before adding series to chart
+                    startDate = results[startPos].getDate(idxX);
+                    endDate   = results[endPos-1].getDate(idxX);
+                    addGaps(results,series,startDate,endDate,startPos,endPos);
+                    addSeries(series, seriesName, idx);
+
+                    startPos  = endPos + 1;
+                }
+
+                // prepare variables for next plot
+                seriesName = row.getString(idxSeries);
+
+                logger.debug("next dataset is '" + seriesName + "'");
+                series = new TimeSeries(seriesName, Minute.class);
+            }
+
+            addValue(row, series);
+            storeMaxRange(ranges, row.getDouble(idxY), seriesName);
+            endPos++;
+        }
+
+        if (startPos < results.length && endPos-1 < results.length) {
+            // add the last dataset if existing to plot and prepare its axis
+            startDate = results[startPos].getDate(idxX);
+            endDate = results[endPos-1].getDate(idxX);
+            addGaps(results, series, startDate, endDate, startPos, endPos);
+            addSeries(series, seriesName, idx);
+        }
+
+        addDatasets();
+    }
+
+
+    @Override
+    protected void addDatasets() {
+        XYPlot     plot = chart.getXYPlot();
+        int        idx  = 0;
+
+        TimeSeriesCollection tsc  = null;
+        Iterator             iter = datasets.keySet().iterator();
+
+        while (iter.hasNext()) {
+            String key = (String) iter.next();
+            tsc  = (TimeSeriesCollection)datasets.get(key);
+            plot.setDataset(idx, tsc );
+            logger.debug("Added " + key + " parameter to plot.");
+            prepareAxis(key, idx);
+            adjustRenderer(
+                idx++,
+                tsc.getSeriesCount(),
+                linesVisible,
+                shapesVisible
+            );
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org