changeset 2232:78ac5058da67

Added demo standalone app for evaluating different timeseries charts. flys-artifacts/trunk@3876 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 02 Feb 2012 11:57:38 +0000
parents 5648b5b34ae2
children 958a10e2e246
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java
diffstat 2 files changed, 222 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Feb 02 10:54:21 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Feb 02 11:57:38 2012 +0000
@@ -1,3 +1,8 @@
+2012-02-02  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java:
+	  Added standalone demo app for evaluating different timeseries charts.
+
 2012-02-02  Raimund Renkert <raimund.renkert@intevation.de>
 
 	Issue 466: CSV export for chart themes.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java	Thu Feb 02 11:57:38 2012 +0000
@@ -0,0 +1,217 @@
+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