view gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesVectorChart.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

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