Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/TimeseriesStepChart.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 4bd3d8bbb60c |
children | 6d8c7a00e74e |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.artifacts.charts; | |
2 | |
3 import java.awt.Dimension; | |
4 import java.util.Date; | |
5 import java.util.GregorianCalendar; | |
6 | |
7 import org.jfree.chart.ChartFactory; | |
8 import org.jfree.chart.ChartPanel; | |
9 import org.jfree.chart.JFreeChart; | |
10 import org.jfree.ui.ApplicationFrame; | |
11 | |
12 import org.jfree.data.time.SimpleTimePeriod; | |
13 import org.jfree.data.time.Second; | |
14 import org.jfree.data.time.TimeSeries; | |
15 import org.jfree.data.time.TimeSeriesCollection; | |
16 import org.jfree.data.time.TimePeriodValues; | |
17 import org.jfree.data.time.TimePeriodValuesCollection; | |
18 | |
19 import org.apache.log4j.Logger; | |
20 import org.apache.log4j.PropertyConfigurator; | |
21 | |
22 | |
23 public class TimeseriesStepChart extends ApplicationFrame { | |
24 | |
25 private static final Logger logger = | |
26 Logger.getLogger(TimeseriesStepChart.class); | |
27 | |
28 | |
29 public static final String LOG4J_PROPERTIES = "FLYS_CLIENT_LOG4J_PROPERIES"; | |
30 | |
31 public static final int USER_CHART_TYPE = | |
32 Integer.getInteger("chart.type", -1); | |
33 | |
34 public static final int CHART_TYPE_TIMEPERIOD = 1; | |
35 public static final int CHART_TYPE_TIMESERIES = 2; | |
36 | |
37 | |
38 public static void main(String[] args) { | |
39 configureLogging(); | |
40 | |
41 logger.info("Start TimeseriesStepChart demo."); | |
42 | |
43 if (USER_CHART_TYPE <= 0) { | |
44 logger.error("Unknown chart type: " + USER_CHART_TYPE); | |
45 } | |
46 | |
47 logger.debug("Create Chart from type: " + USER_CHART_TYPE); | |
48 | |
49 TimeseriesStepChart tsc = new TimeseriesStepChart(); | |
50 tsc.build(USER_CHART_TYPE); | |
51 | |
52 logger.info("End of TimeseriesStepChart demo."); | |
53 } | |
54 | |
55 | |
56 public static void configureLogging() { | |
57 String log4jProperties = System.getenv(LOG4J_PROPERTIES); | |
58 | |
59 if (log4jProperties != null && log4jProperties.length() > 0) { | |
60 PropertyConfigurator.configure(log4jProperties); | |
61 logger.info("Log4J logging initialized."); | |
62 } | |
63 else { | |
64 System.out.println("Error while setting up Log4J configuration."); | |
65 } | |
66 | |
67 System.out.println("LoggingConfigurator.init finished"); | |
68 } | |
69 | |
70 | |
71 public TimeseriesStepChart() { | |
72 super("TimeseriesStepChart"); | |
73 } | |
74 | |
75 | |
76 public void build(int type) { | |
77 ChartPanel chartPanel = createChartPanel(type); | |
78 chartPanel.setPreferredSize(new Dimension(500, 300)); | |
79 | |
80 setContentPane(chartPanel); | |
81 | |
82 pack(); | |
83 setVisible(true); | |
84 } | |
85 | |
86 | |
87 protected ChartPanel createChartPanel(int type) { | |
88 JFreeChart chart = createJFreeChart(type); | |
89 ChartPanel panel = new ChartPanel(chart); | |
90 return panel; | |
91 } | |
92 | |
93 | |
94 protected JFreeChart createJFreeChart(int type) { | |
95 if (type == CHART_TYPE_TIMEPERIOD) { | |
96 return createTimePeriodValuesChart(); | |
97 } | |
98 else if (type == CHART_TYPE_TIMESERIES) { | |
99 return createTimeSeriesChart(); | |
100 } | |
101 | |
102 logger.error("Unknown chart type: " + type); | |
103 | |
104 return null; | |
105 } | |
106 | |
107 | |
108 public static Date createDate(int year, int month, int day) { | |
109 GregorianCalendar cal = new GregorianCalendar(year, month, day); | |
110 return cal.getTime(); | |
111 } | |
112 | |
113 | |
114 | |
115 // | |
116 // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE | |
117 // | |
118 protected JFreeChart createTimePeriodValuesChart() { | |
119 return ChartFactory.createTimeSeriesChart( | |
120 "TimePeriodValues Chart", | |
121 "Time Axis", | |
122 "Y Axis", | |
123 createTimePeriodValuesDataset(), | |
124 true, | |
125 false, | |
126 false); | |
127 } | |
128 | |
129 | |
130 protected TimePeriodValuesCollection createTimePeriodValuesDataset() { | |
131 TimePeriodValuesCollection dataset = new TimePeriodValuesCollection(); | |
132 TimePeriodValues series = createTimePeriodValues(); | |
133 | |
134 dataset.addSeries(series); | |
135 | |
136 return dataset; | |
137 } | |
138 | |
139 | |
140 protected TimePeriodValues createTimePeriodValues() { | |
141 TimePeriodValues series1 = new TimePeriodValues("Series 1"); | |
142 | |
143 Date start1 = createDate(2000, 0, 1); | |
144 Date end1 = createDate(2000, 11, 31); | |
145 | |
146 logger.debug("START DATE 1 = " + start1); | |
147 logger.debug("END DATE 1 = " + end1); | |
148 | |
149 SimpleTimePeriod period1 = new SimpleTimePeriod(start1, end1); | |
150 | |
151 Date start2 = createDate(2001, 0, 1); | |
152 Date end2 = createDate(2001, 11, 31); | |
153 SimpleTimePeriod period2 = new SimpleTimePeriod(start2, end2); | |
154 | |
155 Date start3 = createDate(2002, 0, 1); | |
156 Date end3 = createDate(2002, 11, 31); | |
157 SimpleTimePeriod period3 = new SimpleTimePeriod(start3, end3); | |
158 | |
159 series1.add(period1, 100); | |
160 series1.add(period2, 200); | |
161 series1.add(period3, 150); | |
162 | |
163 return series1; | |
164 } | |
165 | |
166 | |
167 // | |
168 // XXX FOLLOWING METHODS ARE USED FOR "TimePeriodValues" CHART TYPE | |
169 // | |
170 protected JFreeChart createTimeSeriesChart() { | |
171 return ChartFactory.createTimeSeriesChart( | |
172 "TimeSeriesCollection Chart", | |
173 "Time Axis", | |
174 "Y Axis", | |
175 createTimeSeriesCollectionDataset(), | |
176 true, | |
177 false, | |
178 false); | |
179 } | |
180 | |
181 | |
182 protected TimeSeriesCollection createTimeSeriesCollectionDataset() { | |
183 TimeSeriesCollection dataset = new TimeSeriesCollection(); | |
184 TimeSeries[] series = createTimeSeries(); | |
185 | |
186 for (TimeSeries tmp: series) { | |
187 dataset.addSeries(tmp); | |
188 } | |
189 | |
190 return dataset; | |
191 } | |
192 | |
193 | |
194 protected TimeSeries[] createTimeSeries() { | |
195 TimeSeries series1 = new TimeSeries("Series 1"); | |
196 series1.add(new Second(0, 0, 0, 1, 1, 2000), 100); | |
197 series1.add(new Second(59, 59, 23, 31, 12, 2000), 100); | |
198 | |
199 series1.add(new Second(0, 0, 0, 1, 1, 2001), 200); | |
200 series1.add(new Second(59, 59, 23, 31, 12, 2001), 200); | |
201 | |
202 series1.add(new Second(0, 0, 0, 1, 1, 2002), 150); | |
203 series1.add(new Second(59, 59, 23, 31, 12, 2002), 150); | |
204 | |
205 TimeSeries series2 = new TimeSeries("Series 2"); | |
206 series2.add(new Second(0, 0, 0, 1, 1, 2000), 10); | |
207 series2.add(new Second(59, 59, 23, 31, 12, 2000), 10); | |
208 | |
209 series2.add(new Second(0, 0, 0, 1, 1, 2001), 20); | |
210 series2.add(new Second(59, 59, 23, 31, 12, 2001), 20); | |
211 | |
212 series2.add(new Second(0, 0, 0, 1, 1, 2002), 15); | |
213 series2.add(new Second(59, 59, 23, 31, 12, 2002), 15); | |
214 | |
215 return new TimeSeries[] { series1, series2 }; | |
216 } | |
217 } | |
218 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |