view flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java @ 4210:e47559908acc

Cosmetics.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 22 Oct 2012 09:10:40 +0200
parents 6d8c7a00e74e
children
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.");
    }


    /** Setup the log4j logging (read LOG4J_PROPERIES envvar). */
    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 };
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org