Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java @ 3131:bac0b2721451
FixA: Added delta W(t) chart in fix analysis.
flys-artifacts/trunk@4732 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Wed, 20 Jun 2012 16:10:59 +0000 |
parents | |
children | 5a0aef74c316 |
comparison
equal
deleted
inserted
replaced
3130:0dd569e9b9f1 | 3131:bac0b2721451 |
---|---|
1 package de.intevation.flys.exports.fixings; | |
2 | |
3 import java.util.Date; | |
4 | |
5 import java.awt.Color; | |
6 import java.awt.BasicStroke; | |
7 | |
8 import org.w3c.dom.Element; | |
9 import org.w3c.dom.Document; | |
10 | |
11 import org.apache.log4j.Logger; | |
12 | |
13 import org.jfree.chart.JFreeChart; | |
14 import org.jfree.chart.title.TextTitle; | |
15 | |
16 import org.jfree.data.general.SeriesException; | |
17 import org.jfree.data.time.Day; | |
18 import org.jfree.data.time.Month; | |
19 import org.jfree.data.time.RegularTimePeriod; | |
20 import org.jfree.data.time.TimeSeries; | |
21 import org.jfree.data.time.TimeSeriesCollection; | |
22 import org.jfree.chart.plot.Marker; | |
23 import org.jfree.chart.plot.ValueMarker; | |
24 import org.jfree.chart.plot.IntervalMarker; | |
25 | |
26 import java.io.OutputStream; | |
27 | |
28 import javax.xml.xpath.XPathConstants; | |
29 | |
30 import de.intevation.artifacts.ArtifactNamespaceContext; | |
31 import de.intevation.artifacts.common.utils.XMLUtils; | |
32 import de.intevation.artifacts.CallContext; | |
33 | |
34 | |
35 import de.intevation.artifactdatabase.state.ArtifactAndFacet; | |
36 | |
37 import de.intevation.flys.artifacts.FLYSArtifact; | |
38 import de.intevation.flys.artifacts.model.FacetTypes; | |
39 import de.intevation.flys.artifacts.model.HistoricalWQTimerange; | |
40 import de.intevation.flys.artifacts.model.Timerange; | |
41 import de.intevation.flys.artifacts.model.WQTimerange; | |
42 import de.intevation.flys.jfree.StyledTimeSeries; | |
43 import de.intevation.flys.utils.FLYSUtils; | |
44 import de.intevation.flys.exports.TimeseriesChartGenerator; | |
45 import de.intevation.flys.artifacts.model.fixings.QWD; | |
46 import de.intevation.flys.artifacts.model.fixings.DateRange; | |
47 | |
48 /** | |
49 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
50 */ | |
51 public class FixDeltaWtGenerator | |
52 extends TimeseriesChartGenerator | |
53 implements FacetTypes | |
54 { | |
55 private static Logger logger = | |
56 Logger.getLogger(FixDeltaWtGenerator.class); | |
57 | |
58 public static final String XPATH_CHART_CURRENTKM = | |
59 "/art:action/art:attributes/art:currentKm"; | |
60 | |
61 public static final String I18N_CHART_TITLE = | |
62 "chart.fix.deltawt.title"; | |
63 | |
64 public static final String I18N_CHART_SUBTITLE = | |
65 "chart.fix.deltawt.subtitle"; | |
66 | |
67 public static final String I18N_XAXIS_LABEL = | |
68 "chart.fix.deltawt.xaxis.label"; | |
69 | |
70 public static final String I18N_YAXIS_LABEL = | |
71 "chart.fix.deltawt.yaxis.label"; | |
72 | |
73 public static final String I18N_YAXIS_SECOND_LABEL = | |
74 "chart.fix.deltawt.yaxis.second.label"; | |
75 | |
76 | |
77 public static enum YAXIS { | |
78 dW(0); | |
79 protected int idx; | |
80 private YAXIS(int c) { | |
81 idx = c; | |
82 } | |
83 } | |
84 | |
85 | |
86 @Override | |
87 protected YAxisWalker getYAxisWalker() { | |
88 return new YAxisWalker() { | |
89 @Override | |
90 public int length() { | |
91 return YAXIS.values().length; | |
92 } | |
93 | |
94 @Override | |
95 public String getId(int idx) { | |
96 YAXIS[] yaxes = YAXIS.values(); | |
97 return yaxes[idx].toString(); | |
98 } | |
99 }; | |
100 } | |
101 | |
102 | |
103 @Override | |
104 protected String getDefaultChartTitle() { | |
105 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE); | |
106 } | |
107 | |
108 | |
109 @Override | |
110 protected String getDefaultChartSubtitle() { | |
111 String[] args = new String[] { | |
112 FLYSUtils.getReferenceGaugeName((FLYSArtifact) master) | |
113 }; | |
114 | |
115 return msg(I18N_CHART_SUBTITLE, "", args); | |
116 } | |
117 | |
118 | |
119 /** | |
120 * Empty (suppress subtitle). | |
121 */ | |
122 @Override | |
123 protected void addSubtitles(JFreeChart chart) { | |
124 String subtitle = getChartSubtitle(); | |
125 | |
126 if (subtitle != null && subtitle.length() > 0) { | |
127 chart.addSubtitle(new TextTitle(subtitle)); | |
128 } | |
129 } | |
130 | |
131 | |
132 @Override | |
133 protected String getDefaultXAxisLabel() { | |
134 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL); | |
135 } | |
136 | |
137 @Override | |
138 protected String getDefaultYAxisLabel(int pos) { | |
139 if (pos == 0) { | |
140 return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL); | |
141 } | |
142 else if (pos == 1) { | |
143 return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL); | |
144 } | |
145 else { | |
146 return "NO TITLE FOR Y AXIS: " + pos; | |
147 } | |
148 } | |
149 | |
150 | |
151 public void doOut( | |
152 ArtifactAndFacet artifactFacet, | |
153 Document theme, | |
154 boolean visible | |
155 ) { | |
156 String name = artifactFacet.getFacetName(); | |
157 logger.debug("FixDeltaWtGenerator.doOut: " + name); | |
158 logger.debug("Theme description is: " + artifactFacet.getFacetDescription()); | |
159 | |
160 if (name.contains(FIX_SECTOR_AVERAGE_DWT)) { | |
161 doSectorAverageOut( | |
162 (FLYSArtifact) artifactFacet.getArtifact(), | |
163 artifactFacet.getData(context), | |
164 artifactFacet.getFacetDescription(), | |
165 theme, | |
166 visible); | |
167 } | |
168 else if (name.equals(FIX_REFERENCE_EVENTS_DWT)) { | |
169 doReferenceEventsOut( | |
170 (FLYSArtifact) artifactFacet.getArtifact(), | |
171 artifactFacet.getData(context), | |
172 artifactFacet.getFacetDescription(), | |
173 theme, | |
174 visible); | |
175 } | |
176 else if (name.equals(FIX_ANALYSIS_EVENTS_DWT)) { | |
177 doAnalysisEventsOut( | |
178 (FLYSArtifact) artifactFacet.getArtifact(), | |
179 artifactFacet.getData(context), | |
180 artifactFacet.getFacetDescription(), | |
181 theme, | |
182 visible); | |
183 } | |
184 else if (name.equals(FIX_DEVIATION_DWT)) { | |
185 doDeviationOut( | |
186 (FLYSArtifact) artifactFacet.getArtifact(), | |
187 artifactFacet.getData(context), | |
188 artifactFacet.getFacetDescription(), | |
189 theme, | |
190 visible); | |
191 } | |
192 else if (name.equals(FIX_ANALYSIS_PERIODS_DWT)) { | |
193 doAnalysisPeriodsOut( | |
194 (FLYSArtifact) artifactFacet.getArtifact(), | |
195 artifactFacet.getData(context), | |
196 artifactFacet.getFacetDescription(), | |
197 theme, | |
198 visible); | |
199 } | |
200 else { | |
201 logger.warn("doOut(): unknown facet name: " + name); | |
202 return; | |
203 } | |
204 } | |
205 | |
206 | |
207 protected void doSectorAverageOut( | |
208 FLYSArtifact artifact, | |
209 Object data, | |
210 String desc, | |
211 Document theme, | |
212 boolean visible) | |
213 { | |
214 logger.debug("doSectorAverageOut(): description = " + desc); | |
215 | |
216 QWD qwd = (QWD) data; | |
217 TimeSeriesCollection tsc = new TimeSeriesCollection(); | |
218 TimeSeries series = new StyledTimeSeries(desc, theme); | |
219 | |
220 if (qwd == null) { | |
221 return; | |
222 } | |
223 RegularTimePeriod rtp = new Day(qwd.getDate()); | |
224 double value = qwd.getDeltaW(); | |
225 series.add(rtp, value); | |
226 tsc.addSeries(series); | |
227 | |
228 addAxisDataset(tsc, 0, visible); | |
229 } | |
230 | |
231 | |
232 protected void doAnalysisEventsOut( | |
233 FLYSArtifact artifact, | |
234 Object data, | |
235 String desc, | |
236 Document theme, | |
237 boolean visible | |
238 ) { | |
239 logger.debug("doAnalysisEventsOut: desc = " + desc); | |
240 | |
241 QWD[] qwds = (QWD[]) data; | |
242 TimeSeriesCollection tsc = new TimeSeriesCollection(); | |
243 TimeSeries series = new StyledTimeSeries(desc, theme); | |
244 | |
245 if (qwds == null) { | |
246 return; | |
247 } | |
248 for (int i = 0; i < qwds.length; i++) { | |
249 if (qwds[i] == null) { | |
250 continue; | |
251 } | |
252 RegularTimePeriod rtp = new Day(qwds[i].getDate()); | |
253 double value = qwds[i].getDeltaW(); | |
254 series.add(rtp, value); | |
255 } | |
256 tsc.addSeries(series); | |
257 | |
258 addAxisDataset(tsc, 0, visible); | |
259 | |
260 } | |
261 | |
262 | |
263 protected void doReferenceEventsOut( | |
264 FLYSArtifact artifact, | |
265 Object data, | |
266 String desc, | |
267 Document theme, | |
268 boolean visible | |
269 ) { | |
270 logger.debug("doReferenceEventsOut: desc = " + desc); | |
271 | |
272 QWD[] qwds = (QWD[]) data; | |
273 TimeSeriesCollection tsc = new TimeSeriesCollection(); | |
274 TimeSeries series = new StyledTimeSeries(desc, theme); | |
275 | |
276 if (qwds == null) { | |
277 return; | |
278 } | |
279 for (int i = 0; i < qwds.length; i++) { | |
280 if (qwds[i] == null) { | |
281 continue; | |
282 } | |
283 logger.debug("adding..." + qwds[i].getDate() + "; val: " + qwds[i].getDeltaW()); | |
284 RegularTimePeriod rtp = new Day(qwds[i].getDate()); | |
285 double value = qwds[i].getDeltaW(); | |
286 series.addOrUpdate(rtp, value); | |
287 } | |
288 tsc.addSeries(series); | |
289 | |
290 addAxisDataset(tsc, 0, visible); | |
291 } | |
292 | |
293 | |
294 protected void doDeviationOut( | |
295 FLYSArtifact artifact, | |
296 Object data, | |
297 String desc, | |
298 Document theme, | |
299 boolean visible | |
300 ) { | |
301 logger.debug("doDeviationOut: desc = " + desc); | |
302 | |
303 if (data == null || !visible) { | |
304 logger.debug("no standard deviation"); | |
305 return; | |
306 } | |
307 double[] value = (double[]) data; | |
308 IntervalMarker lower = new IntervalMarker((value[0] * -1), 0); | |
309 lower.setAlpha(0.2f); | |
310 lower.setPaint(Color.BLACK); | |
311 IntervalMarker upper = new IntervalMarker(0, value[0]); | |
312 upper.setAlpha(0.2f); | |
313 upper.setPaint(Color.BLACK); | |
314 | |
315 valueMarker.add(lower); | |
316 valueMarker.add(upper); | |
317 } | |
318 | |
319 | |
320 protected void doAnalysisPeriodsOut( | |
321 FLYSArtifact artifact, | |
322 Object data, | |
323 String desc, | |
324 Document theme, | |
325 boolean visible | |
326 ) { | |
327 logger.debug("doHistoricalDischargeDifferenceOut: desc = " + desc); | |
328 | |
329 DateRange[] ranges = (DateRange[]) data; | |
330 if (ranges == null || !visible) { | |
331 return; | |
332 } | |
333 for (int i = 0; i < ranges.length; i++) { | |
334 logger.debug("creating domain marker"); | |
335 RegularTimePeriod start = new Month(ranges[i].getFrom()); | |
336 RegularTimePeriod end = new Month(ranges[i].getTo()); | |
337 IntervalMarker marker = | |
338 new IntervalMarker(start.getMiddleMillisecond(), | |
339 end.getMiddleMillisecond()); | |
340 marker.setAlpha(0.3f); | |
341 if ((i % 2) == 0) { | |
342 marker.setPaint(Color.RED); | |
343 } | |
344 else { | |
345 marker.setPaint(Color.BLUE); | |
346 } | |
347 domainMarker.add(marker); | |
348 } | |
349 logger.debug("domainmarkers: " + domainMarker.size()); | |
350 } | |
351 | |
352 | |
353 @Override | |
354 public void init(Document request, OutputStream out, CallContext context) { | |
355 super.init(request, out, context); | |
356 | |
357 logger.debug("currentKm = " + new Double(getCurrentKmFromRequest())); | |
358 context.putContextValue("currentKm", new Double(getCurrentKmFromRequest())); | |
359 | |
360 ValueMarker marker = new ValueMarker(0); | |
361 marker.setPaint(Color.black); | |
362 marker.setStroke(new BasicStroke(2)); | |
363 valueMarker.add(marker); | |
364 } | |
365 | |
366 public double getCurrentKmFromRequest() { | |
367 Element km = (Element)XMLUtils.xpath( | |
368 request, | |
369 XPATH_CHART_CURRENTKM, | |
370 XPathConstants.NODE, | |
371 ArtifactNamespaceContext.INSTANCE); | |
372 | |
373 if (km == null) { | |
374 return -1d; | |
375 } | |
376 | |
377 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
378 | |
379 String currentKm = km.getAttributeNS(uri, "km"); | |
380 try { | |
381 double d = Double.valueOf(currentKm).doubleValue(); | |
382 return d; | |
383 } | |
384 catch(NumberFormatException nfe) { | |
385 return -1d; | |
386 } | |
387 } | |
388 | |
389 | |
390 } | |
391 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |