view flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java @ 2792:fe987587ebc9

Merged revisions 4539-4540,4543,4545-4546 via svnmerge from file:///home/clients/bsh/bsh-generischer-viewer/Material/SVN/flys-artifacts/trunk ........ r4539 | teichmann | 2012-05-27 20:02:13 +0200 (So, 27 Mai 2012) | 1 line FixA: Added forgotten csv/report facets/generators to conf. ........ r4540 | teichmann | 2012-05-27 20:11:31 +0200 (So, 27 Mai 2012) | 1 line FixA: Fixed class cast bug in report facet. ........ r4543 | teichmann | 2012-05-28 20:35:01 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Added facet to return delta w/t as CSV ........ r4545 | teichmann | 2012-05-28 22:59:27 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Made Delta W/t calculation finally work ........ r4546 | teichmann | 2012-05-28 23:34:24 +0200 (Mo, 28 Mai 2012) | 1 line FixA: corrected fitting (Q->W instead W->Q). ........ flys-artifacts/tags/2.7@4547 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 29 May 2012 04:58:29 +0000
parents 78ac5058da67
children 4bd3d8bbb60c
line wrap: on
line source
package de.intevation.flys.artifacts.charts;

import java.awt.Dimension;
import java.util.Date;
import java.util.GregorianCalendar;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApplicationFrame;

import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimePeriodValues;
import org.jfree.data.time.TimePeriodValuesCollection;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class TimeseriesStepChart extends ApplicationFrame {

    private static final Logger logger =
        Logger.getLogger(TimeseriesStepChart.class);


    public static final String LOG4J_PROPERTIES = "FLYS_CLIENT_LOG4J_PROPERIES";

    public static final int USER_CHART_TYPE =
        Integer.getInteger("chart.type", -1);

    public static final int CHART_TYPE_TIMEPERIOD = 1;
    public static final int CHART_TYPE_TIMESERIES = 2;


    public static void main(String[] args) {
        configureLogging();

        logger.info("Start TimeseriesStepChart demo.");

        if (USER_CHART_TYPE <= 0) {
            logger.error("Unknown chart type: " + USER_CHART_TYPE);
        }

        logger.debug("Create Chart from type: " + USER_CHART_TYPE);

        TimeseriesStepChart tsc = new TimeseriesStepChart();
        tsc.build(USER_CHART_TYPE);

        logger.info("End of TimeseriesStepChart demo.");
    }


    public static void configureLogging() {
        String log4jProperties = System.getenv(LOG4J_PROPERTIES);

        if (log4jProperties != null && log4jProperties.length() > 0) {
            PropertyConfigurator.configure(log4jProperties);
            logger.info("Log4J logging initialized.");
        }
        else {
            System.out.println("Error while setting up Log4J configuration.");
        }

        System.out.println("LoggingConfigurator.init finished");
    }


    public TimeseriesStepChart() {
        super("TimeseriesStepChart");
    }


    public void build(int type) {
        ChartPanel chartPanel = createChartPanel(type);
        chartPanel.setPreferredSize(new Dimension(500, 300));

        setContentPane(chartPanel);

        pack();
        setVisible(true);
    }


    protected ChartPanel createChartPanel(int type) {
        JFreeChart chart = createJFreeChart(type);
        ChartPanel panel = new ChartPanel(chart);
        return panel;
    }


    protected JFreeChart createJFreeChart(int type) {
        if (type == CHART_TYPE_TIMEPERIOD) {
            return createTimePeriodValuesChart();
        }
        else if (type == CHART_TYPE_TIMESERIES) {
            return createTimeSeriesChart();
        }

        logger.error("Unknown chart type: " + type);

        return null;
    }


    public static Date createDate(int year, int month, int day) {
        GregorianCalendar cal = new GregorianCalendar(year, month, day);
        return cal.getTime();
    }



    //
    // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE
    //
    protected JFreeChart createTimePeriodValuesChart() {
        return ChartFactory.createTimeSeriesChart(
            "TimePeriodValues Chart",
            "Time Axis",
            "Y Axis",
            createTimePeriodValuesDataset(),
            true,
            false,
            false);
    }


    protected TimePeriodValuesCollection createTimePeriodValuesDataset() {
        TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
        TimePeriodValues series = createTimePeriodValues();

        dataset.addSeries(series);

        return dataset;
    }


    protected TimePeriodValues createTimePeriodValues() {
        TimePeriodValues series1 = new TimePeriodValues("Series 1");

        Date start1 = createDate(2000, 0, 1);
        Date end1   = createDate(2000, 11, 31);

        logger.debug("START DATE 1 = " + start1);
        logger.debug("END   DATE 1 = " + end1);

        SimpleTimePeriod period1 = new SimpleTimePeriod(start1, end1);

        Date start2 = createDate(2001, 0, 1);
        Date end2   = createDate(2001, 11, 31);
        SimpleTimePeriod period2 = new SimpleTimePeriod(start2, end2);

        Date start3 = createDate(2002, 0, 1);
        Date end3   = createDate(2002, 11, 31);
        SimpleTimePeriod period3 = new SimpleTimePeriod(start3, end3);

        series1.add(period1, 100);
        series1.add(period2, 200);
        series1.add(period3, 150);

        return series1;
    }


    //
    // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE
    //
    protected JFreeChart createTimeSeriesChart() {
        return ChartFactory.createTimeSeriesChart(
            "TimeSeriesCollection Chart",
            "Time Axis",
            "Y Axis",
            createTimeSeriesCollectionDataset(),
            true,
            false,
            false);
    }


    protected TimeSeriesCollection createTimeSeriesCollectionDataset() {
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        TimeSeries[]         series  = createTimeSeries();

        for (TimeSeries tmp: series) {
            dataset.addSeries(tmp);
        }

        return dataset;
    }


    protected TimeSeries[] createTimeSeries() {
        TimeSeries series1 = new TimeSeries("Series 1");
        series1.add(new Second(0, 0, 0, 1, 1, 2000), 100);
        series1.add(new Second(59, 59, 23, 31, 12, 2000), 100);

        series1.add(new Second(0, 0, 0, 1, 1, 2001), 200);
        series1.add(new Second(59, 59, 23, 31, 12, 2001), 200);

        series1.add(new Second(0, 0, 0, 1, 1, 2002), 150);
        series1.add(new Second(59, 59, 23, 31, 12, 2002), 150);

        TimeSeries series2 = new TimeSeries("Series 2");
        series2.add(new Second(0, 0, 0, 1, 1, 2000), 10);
        series2.add(new Second(59, 59, 23, 31, 12, 2000), 10);

        series2.add(new Second(0, 0, 0, 1, 1, 2001), 20);
        series2.add(new Second(59, 59, 23, 31, 12, 2001), 20);

        series2.add(new Second(0, 0, 0, 1, 1, 2002), 15);
        series2.add(new Second(59, 59, 23, 31, 12, 2002), 15);

        return new TimeSeries[] { series1, series2 };
    }
}

http://dive4elements.wald.intevation.org