comparison flys-artifacts/src/main/java/org/dive4elements/river/exports/HistoricalDischargeCurveGenerator.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveGenerator.java@b195fede1c3b
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.exports;
2
3 import java.util.Date;
4
5 import org.apache.log4j.Logger;
6 import org.jfree.chart.plot.XYPlot;
7 import org.jfree.data.general.SeriesException;
8 import org.jfree.data.time.Day;
9 import org.jfree.data.time.RegularTimePeriod;
10 import org.jfree.data.time.TimeSeries;
11 import org.jfree.data.time.TimeSeriesCollection;
12 import org.w3c.dom.Document;
13
14 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
15 import org.dive4elements.river.artifacts.FLYSArtifact;
16 import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess;
17 import org.dive4elements.river.artifacts.model.FacetTypes;
18 import org.dive4elements.river.artifacts.model.HistoricalWQTimerange;
19 import org.dive4elements.river.artifacts.model.Timerange;
20 import org.dive4elements.river.artifacts.model.WQTimerange;
21 import org.dive4elements.river.jfree.StyledTimeSeries;
22 import org.dive4elements.river.utils.FLYSUtils;
23
24
25 /**
26 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
27 */
28 public class HistoricalDischargeCurveGenerator extends TimeseriesChartGenerator
29 implements FacetTypes {
30
31 private static Logger logger = Logger
32 .getLogger(HistoricalDischargeCurveGenerator.class);
33
34 public static final String I18N_CHART_TITLE = "chart.historical.discharge.title";
35
36 public static final String I18N_CHART_SUBTITLE = "chart.historical.discharge.subtitle";
37
38 public static final String I18N_XAXIS_LABEL = "chart.historical.discharge.xaxis.label";
39
40 public static final String I18N_YAXIS_LABEL = "chart.historical.discharge.yaxis.label";
41
42 public static final String I18N_YAXIS_SECOND_LABEL = "chart.historical.discharge.yaxis.second.label";
43
44 public static enum YAXIS {
45 W(0), Q(1);
46
47 protected int idx;
48
49 private YAXIS(int c) {
50 idx = c;
51 }
52 }
53
54 @Override
55 protected YAxisWalker getYAxisWalker() {
56 return new YAxisWalker() {
57
58 @Override
59 public int length() {
60 return YAXIS.values().length;
61 }
62
63 @Override
64 public String getId(int idx) {
65 YAXIS[] yaxes = YAXIS.values();
66 return yaxes[idx].toString();
67 }
68 };
69 }
70
71 @Override
72 protected String getDefaultChartTitle() {
73 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE);
74 }
75
76 @Override
77 protected String getDefaultChartSubtitle() {
78 FLYSArtifact flys = (FLYSArtifact) master;
79 Timerange evalTime = new HistoricalDischargeAccess(flys)
80 .getEvaluationTimerange();
81
82 Object[] args = new Object[] { FLYSUtils.getReferenceGaugeName(flys),
83 evalTime.getStart(), evalTime.getEnd() };
84
85 return msg(I18N_CHART_SUBTITLE, "", args);
86 }
87
88 @Override
89 protected String getDefaultXAxisLabel() {
90 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL);
91 }
92
93 @Override
94 protected String getDefaultYAxisLabel(int pos) {
95 if (pos == 0) {
96 return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL);
97 }
98 else if (pos == 1) {
99 return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL);
100 }
101 else {
102 return "NO TITLE FOR Y AXIS: " + pos;
103 }
104 }
105
106 @Override
107 protected void adjustPlot(XYPlot plot) {
108 super.adjustPlot(plot);
109 plot.setRangeZeroBaselineVisible(true);
110 }
111
112 @Override
113 public void doOut(ArtifactAndFacet artifactFacet, Document theme,
114 boolean visible) {
115 String name = artifactFacet.getFacetName();
116 logger.debug("HistoricalDischargeCurveGenerator.doOut: " + name);
117 logger.debug("Theme description is: "
118 + artifactFacet.getFacetDescription());
119
120 if (name.equals(HISTORICAL_DISCHARGE_Q)) {
121 doHistoricalDischargeOutQ(
122 (FLYSArtifact) artifactFacet.getArtifact(),
123 artifactFacet.getData(context),
124 artifactFacet.getFacetDescription(), theme, visible);
125 }
126 else if (name.equals(HISTORICAL_DISCHARGE_W)) {
127 doHistoricalDischargeOutW(
128 (FLYSArtifact) artifactFacet.getArtifact(),
129 artifactFacet.getData(context),
130 artifactFacet.getFacetDescription(), theme, visible);
131 }
132 else if (name.equals(HISTORICAL_DISCHARGE_Q_DIFF)) {
133 doHistoricalDischargeDifferenceOutQ(
134 (FLYSArtifact) artifactFacet.getArtifact(),
135 artifactFacet.getData(context),
136 artifactFacet.getFacetDescription(), theme, visible);
137 }
138 else if (name.equals(HISTORICAL_DISCHARGE_W_DIFF)) {
139 doHistoricalDischargeDifferenceOutW(
140 (FLYSArtifact) artifactFacet.getArtifact(),
141 artifactFacet.getData(context),
142 artifactFacet.getFacetDescription(), theme, visible);
143 }
144 else if (FacetTypes.IS.MANUALPOINTS(name)) {
145 doPoints(artifactFacet.getData(context), artifactFacet, theme,
146 visible, YAXIS.Q.idx);
147 }
148 else {
149 logger.warn("doOut(): unknown facet name: " + name);
150 return;
151 }
152 }
153
154 protected void doHistoricalDischargeOutQ(FLYSArtifact artifact,
155 Object data, String desc, Document theme, boolean visible) {
156 logger.debug("doHistoricalDischargeOut(): description = " + desc);
157
158 WQTimerange wqt = (WQTimerange) data;
159
160 TimeSeriesCollection tsc = newTimeSeriesCollection(wqt.getTimeranges(),
161 wqt.getQs(), theme, desc);
162
163 addAxisDataset(tsc, YAXIS.Q.idx, visible);
164 }
165
166 protected void doHistoricalDischargeOutW(FLYSArtifact artifact,
167 Object data, String desc, Document theme, boolean visible) {
168 logger.debug("doHistoricalDischargeOut(): description = " + desc);
169
170 WQTimerange wqt = (WQTimerange) data;
171
172 TimeSeriesCollection tsc = newTimeSeriesCollection(wqt.getTimeranges(),
173 wqt.getWs(), theme, desc);
174
175 addAxisDataset(tsc, YAXIS.W.idx, visible);
176 }
177
178 protected void doHistoricalDischargeDifferenceOutQ(FLYSArtifact artifact,
179 Object data, String desc, Document theme, boolean visible) {
180 logger.debug("doHistoricalDischargeDifferenceOut: desc = " + desc);
181
182 HistoricalWQTimerange wqt = (HistoricalWQTimerange) data;
183
184 TimeSeriesCollection tsc = newTimeSeriesCollection(wqt.getTimeranges(),
185 wqt.getDiffs(), theme, desc);
186
187 addAxisDataset(tsc, YAXIS.Q.idx, visible);
188 }
189
190 protected void doHistoricalDischargeDifferenceOutW(FLYSArtifact artifact,
191 Object data, String desc, Document theme, boolean visible) {
192 logger.debug("doHistoricalDischargeDifferenceOut: desc = " + desc);
193
194 HistoricalWQTimerange wqt = (HistoricalWQTimerange) data;
195
196 TimeSeriesCollection tsc = newTimeSeriesCollection(wqt.getTimeranges(),
197 wqt.getDiffs(), theme, desc);
198
199 addAxisDataset(tsc, YAXIS.W.idx, visible);
200 }
201
202 /**
203 * Creates a new TimeSeriesCollection with a single TimeSeries. The
204 * TimeSeries will consist of two RegularTimePeriods for each W/Q value
205 * provided by <i>wqt</i>. This has the effect, that the line in the chart
206 * looks like a "step chart".
207 */
208 protected TimeSeriesCollection newTimeSeriesCollection(
209 Timerange[] timeranges, double[] values, Document theme, String desc) {
210 logger.debug("Create new TimeSeriesCollection for: " + desc);
211
212 TimeSeriesCollection tsc = new TimeSeriesCollection();
213 TimeSeries series = new StyledTimeSeries(desc, theme);
214
215 for (int i = 0, n = timeranges.length; i < n; i++) {
216 RegularTimePeriod[] rtp = newRegularTimePeriod(timeranges[i]);
217
218 try {
219 if (Double.isNaN(values[i])) {
220 logger.warn("Skip TimePeriod because value is NaN.");
221 continue;
222 }
223
224 series.add(rtp[0], values[i]);
225 series.add(rtp[1], values[i]);
226
227 if (logger.isDebugEnabled()) {
228 logger.debug("added Item to TimeSeries:");
229 logger.debug(" TimePeriod: " + rtp[0] + " - " + rtp[1]);
230 logger.debug(" Value: " + values[i]);
231 }
232 }
233 catch (SeriesException se) {
234 logger.warn("Error while adding TimePeriod: " + se);
235 }
236 }
237
238 tsc.addSeries(series);
239
240 return tsc;
241 }
242
243 /**
244 * Creates an array that consists of two <i>Minute</i> periods [start, end].
245 *
246 * @param timerange
247 * Supports start and end time.
248 *
249 * @return an array with two <i>Minute</i> periods [start, end].
250 */
251 protected RegularTimePeriod[] newRegularTimePeriod(Timerange timerange) {
252 Date start = new Date(timerange.getStart());
253 Date end = new Date(timerange.getEnd() - 1000 * 60 * 60 * 24);
254
255 return new RegularTimePeriod[] { new Day(start), new Day(end) };
256 }
257 }
258 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org