ingo@2232: package de.intevation.flys.artifacts.charts;
ingo@2232: 
ingo@2232: import java.awt.Dimension;
ingo@2232: import java.util.Date;
ingo@2232: import java.util.GregorianCalendar;
ingo@2232: 
ingo@2232: import org.jfree.chart.ChartFactory;
ingo@2232: import org.jfree.chart.ChartPanel;
ingo@2232: import org.jfree.chart.JFreeChart;
ingo@2232: import org.jfree.ui.ApplicationFrame;
ingo@2232: 
ingo@2232: import org.jfree.data.time.SimpleTimePeriod;
ingo@2232: import org.jfree.data.time.Second;
ingo@2232: import org.jfree.data.time.TimeSeries;
ingo@2232: import org.jfree.data.time.TimeSeriesCollection;
ingo@2232: import org.jfree.data.time.TimePeriodValues;
ingo@2232: import org.jfree.data.time.TimePeriodValuesCollection;
ingo@2232: 
ingo@2232: import org.apache.log4j.Logger;
ingo@2232: import org.apache.log4j.PropertyConfigurator;
ingo@2232: 
ingo@2232: 
ingo@2232: public class TimeseriesStepChart extends ApplicationFrame {
ingo@2232: 
ingo@2232:     private static final Logger logger =
ingo@2232:         Logger.getLogger(TimeseriesStepChart.class);
ingo@2232: 
ingo@2232: 
ingo@2232:     public static final String LOG4J_PROPERTIES = "FLYS_CLIENT_LOG4J_PROPERIES";
ingo@2232: 
ingo@2232:     public static final int USER_CHART_TYPE =
ingo@2232:         Integer.getInteger("chart.type", -1);
ingo@2232: 
ingo@2232:     public static final int CHART_TYPE_TIMEPERIOD = 1;
ingo@2232:     public static final int CHART_TYPE_TIMESERIES = 2;
ingo@2232: 
ingo@2232: 
ingo@2232:     public static void main(String[] args) {
ingo@2232:         configureLogging();
ingo@2232: 
ingo@2232:         logger.info("Start TimeseriesStepChart demo.");
ingo@2232: 
ingo@2232:         if (USER_CHART_TYPE <= 0) {
ingo@2232:             logger.error("Unknown chart type: " + USER_CHART_TYPE);
ingo@2232:         }
ingo@2232: 
ingo@2232:         logger.debug("Create Chart from type: " + USER_CHART_TYPE);
ingo@2232: 
ingo@2232:         TimeseriesStepChart tsc = new TimeseriesStepChart();
ingo@2232:         tsc.build(USER_CHART_TYPE);
ingo@2232: 
ingo@2232:         logger.info("End of TimeseriesStepChart demo.");
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     public static void configureLogging() {
ingo@2232:         String log4jProperties = System.getenv(LOG4J_PROPERTIES);
ingo@2232: 
ingo@2232:         if (log4jProperties != null && log4jProperties.length() > 0) {
ingo@2232:             PropertyConfigurator.configure(log4jProperties);
ingo@2232:             logger.info("Log4J logging initialized.");
ingo@2232:         }
ingo@2232:         else {
ingo@2232:             System.out.println("Error while setting up Log4J configuration.");
ingo@2232:         }
ingo@2232: 
ingo@2232:         System.out.println("LoggingConfigurator.init finished");
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     public TimeseriesStepChart() {
ingo@2232:         super("TimeseriesStepChart");
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     public void build(int type) {
ingo@2232:         ChartPanel chartPanel = createChartPanel(type);
ingo@2232:         chartPanel.setPreferredSize(new Dimension(500, 300));
ingo@2232: 
ingo@2232:         setContentPane(chartPanel);
ingo@2232: 
ingo@2232:         pack();
ingo@2232:         setVisible(true);
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected ChartPanel createChartPanel(int type) {
ingo@2232:         JFreeChart chart = createJFreeChart(type);
ingo@2232:         ChartPanel panel = new ChartPanel(chart);
ingo@2232:         return panel;
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected JFreeChart createJFreeChart(int type) {
ingo@2232:         if (type == CHART_TYPE_TIMEPERIOD) {
ingo@2232:             return createTimePeriodValuesChart();
ingo@2232:         }
ingo@2232:         else if (type == CHART_TYPE_TIMESERIES) {
ingo@2232:             return createTimeSeriesChart();
ingo@2232:         }
ingo@2232: 
ingo@2232:         logger.error("Unknown chart type: " + type);
ingo@2232: 
ingo@2232:         return null;
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     public static Date createDate(int year, int month, int day) {
ingo@2232:         GregorianCalendar cal = new GregorianCalendar(year, month, day);
ingo@2232:         return cal.getTime();
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232: 
ingo@2232:     //
ingo@2232:     // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE
ingo@2232:     //
ingo@2232:     protected JFreeChart createTimePeriodValuesChart() {
ingo@2232:         return ChartFactory.createTimeSeriesChart(
ingo@2232:             "TimePeriodValues Chart",
ingo@2232:             "Time Axis",
ingo@2232:             "Y Axis",
ingo@2232:             createTimePeriodValuesDataset(),
ingo@2232:             true,
ingo@2232:             false,
ingo@2232:             false);
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected TimePeriodValuesCollection createTimePeriodValuesDataset() {
ingo@2232:         TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
ingo@2232:         TimePeriodValues series = createTimePeriodValues();
ingo@2232: 
ingo@2232:         dataset.addSeries(series);
ingo@2232: 
ingo@2232:         return dataset;
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected TimePeriodValues createTimePeriodValues() {
ingo@2232:         TimePeriodValues series1 = new TimePeriodValues("Series 1");
ingo@2232: 
ingo@2232:         Date start1 = createDate(2000, 0, 1);
ingo@2232:         Date end1   = createDate(2000, 11, 31);
ingo@2232: 
ingo@2232:         logger.debug("START DATE 1 = " + start1);
ingo@2232:         logger.debug("END   DATE 1 = " + end1);
ingo@2232: 
ingo@2232:         SimpleTimePeriod period1 = new SimpleTimePeriod(start1, end1);
ingo@2232: 
ingo@2232:         Date start2 = createDate(2001, 0, 1);
ingo@2232:         Date end2   = createDate(2001, 11, 31);
ingo@2232:         SimpleTimePeriod period2 = new SimpleTimePeriod(start2, end2);
ingo@2232: 
ingo@2232:         Date start3 = createDate(2002, 0, 1);
ingo@2232:         Date end3   = createDate(2002, 11, 31);
ingo@2232:         SimpleTimePeriod period3 = new SimpleTimePeriod(start3, end3);
ingo@2232: 
ingo@2232:         series1.add(period1, 100);
ingo@2232:         series1.add(period2, 200);
ingo@2232:         series1.add(period3, 150);
ingo@2232: 
ingo@2232:         return series1;
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     //
ingo@2232:     // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE
ingo@2232:     //
ingo@2232:     protected JFreeChart createTimeSeriesChart() {
ingo@2232:         return ChartFactory.createTimeSeriesChart(
ingo@2232:             "TimeSeriesCollection Chart",
ingo@2232:             "Time Axis",
ingo@2232:             "Y Axis",
ingo@2232:             createTimeSeriesCollectionDataset(),
ingo@2232:             true,
ingo@2232:             false,
ingo@2232:             false);
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected TimeSeriesCollection createTimeSeriesCollectionDataset() {
ingo@2232:         TimeSeriesCollection dataset = new TimeSeriesCollection();
ingo@2232:         TimeSeries[]         series  = createTimeSeries();
ingo@2232: 
ingo@2232:         for (TimeSeries tmp: series) {
ingo@2232:             dataset.addSeries(tmp);
ingo@2232:         }
ingo@2232: 
ingo@2232:         return dataset;
ingo@2232:     }
ingo@2232: 
ingo@2232: 
ingo@2232:     protected TimeSeries[] createTimeSeries() {
ingo@2232:         TimeSeries series1 = new TimeSeries("Series 1");
ingo@2232:         series1.add(new Second(0, 0, 0, 1, 1, 2000), 100);
ingo@2232:         series1.add(new Second(59, 59, 23, 31, 12, 2000), 100);
ingo@2232: 
ingo@2232:         series1.add(new Second(0, 0, 0, 1, 1, 2001), 200);
ingo@2232:         series1.add(new Second(59, 59, 23, 31, 12, 2001), 200);
ingo@2232: 
ingo@2232:         series1.add(new Second(0, 0, 0, 1, 1, 2002), 150);
ingo@2232:         series1.add(new Second(59, 59, 23, 31, 12, 2002), 150);
ingo@2232: 
ingo@2232:         TimeSeries series2 = new TimeSeries("Series 2");
ingo@2232:         series2.add(new Second(0, 0, 0, 1, 1, 2000), 10);
ingo@2232:         series2.add(new Second(59, 59, 23, 31, 12, 2000), 10);
ingo@2232: 
ingo@2232:         series2.add(new Second(0, 0, 0, 1, 1, 2001), 20);
ingo@2232:         series2.add(new Second(59, 59, 23, 31, 12, 2001), 20);
ingo@2232: 
ingo@2232:         series2.add(new Second(0, 0, 0, 1, 1, 2002), 15);
ingo@2232:         series2.add(new Second(59, 59, 23, 31, 12, 2002), 15);
ingo@2232: 
ingo@2232:         return new TimeSeries[] { series1, series2 };
ingo@2232:     }
ingo@2232: }