comparison artifacts/src/main/java/org/dive4elements/river/exports/DurationCurveGenerator.java @ 9312:740d65e4aa14

Q [m³/s] one message
author gernotbelger
date Thu, 26 Jul 2018 15:54:20 +0200
parents 1cc7653ca84f
children 9b8e8fc1f408
comparison
equal deleted inserted replaced
9311:7c7f73e5e01e 9312:740d65e4aa14
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.exports; 9 package org.dive4elements.river.exports;
10 10
11 import java.awt.Font;
12 import java.awt.geom.Point2D;
13
14 import org.apache.log4j.Logger;
11 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; 15 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
12 import org.dive4elements.artifacts.CallContext; 16 import org.dive4elements.artifacts.CallContext;
13 import org.dive4elements.river.artifacts.model.FacetTypes; 17 import org.dive4elements.river.artifacts.model.FacetTypes;
14 import org.dive4elements.river.artifacts.model.WQDay; 18 import org.dive4elements.river.artifacts.model.WQDay;
15 import org.dive4elements.river.jfree.Bounds; 19 import org.dive4elements.river.jfree.Bounds;
16 import org.dive4elements.river.jfree.RiverAnnotation; 20 import org.dive4elements.river.jfree.RiverAnnotation;
17 import org.dive4elements.river.jfree.StyledXYSeries; 21 import org.dive4elements.river.jfree.StyledXYSeries;
18 import org.dive4elements.river.themes.ThemeDocument; 22 import org.dive4elements.river.themes.ThemeDocument;
19
20 import java.awt.Font;
21 import java.awt.geom.Point2D;
22
23 import org.apache.log4j.Logger;
24 import org.jfree.chart.axis.NumberAxis; 23 import org.jfree.chart.axis.NumberAxis;
25 import org.jfree.chart.axis.ValueAxis; 24 import org.jfree.chart.axis.ValueAxis;
26 import org.jfree.chart.plot.XYPlot; 25 import org.jfree.chart.plot.XYPlot;
27 import org.jfree.data.Range; 26 import org.jfree.data.Range;
28 import org.jfree.data.xy.XYSeries; 27 import org.jfree.data.xy.XYSeries;
29 28
30
31 /** 29 /**
32 * An OutGenerator that generates duration curves. 30 * An OutGenerator that generates duration curves.
33 * 31 *
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
35 */ 33 */
36 public class DurationCurveGenerator 34 public class DurationCurveGenerator extends XYChartGenerator implements FacetTypes {
37 extends XYChartGenerator
38 implements FacetTypes
39 {
40 public static enum YAXIS { 35 public static enum YAXIS {
41 W(0), 36 W(0), Q(1);
42 Q(1);
43 public int idx; 37 public int idx;
44 private YAXIS(int c) { 38
45 idx = c; 39 private YAXIS(final int c) {
40 this.idx = c;
46 } 41 }
47 } 42 }
48 43
49 /** Local log. */ 44 /** Local log. */
50 private static Logger log = 45 private static Logger log = Logger.getLogger(DurationCurveGenerator.class);
51 Logger.getLogger(DurationCurveGenerator.class); 46
52 47 public static final String I18N_CHART_TITLE = "chart.duration.curve.title";
53 public static final String I18N_CHART_TITLE = 48
54 "chart.duration.curve.title"; 49 public static final String I18N_CHART_SUBTITLE = "chart.duration.curve.subtitle";
55 50
56 public static final String I18N_CHART_SUBTITLE = 51 public static final String I18N_XAXIS_LABEL = "chart.duration.curve.xaxis.label";
57 "chart.duration.curve.subtitle"; 52
58 53 public static final String I18N_YAXIS_LABEL_W = "chart.duration.curve.yaxis.label.w";
59 public static final String I18N_XAXIS_LABEL = 54
60 "chart.duration.curve.xaxis.label"; 55 public static final String I18N_YAXIS_LABEL_Q = "common.export.csv.header.q";
61 56
62 public static final String I18N_YAXIS_LABEL_W = 57 public static final String I18N_CHART_TITLE_DEFAULT = "Dauerlinie";
63 "chart.duration.curve.yaxis.label.w"; 58
64 59 public static final String I18N_XAXIS_LABEL_DEFAULT = "Unterschreitungsdauer [Tage]";
65 public static final String I18N_YAXIS_LABEL_Q =
66 "chart.duration.curve.yaxis.label.q";
67
68 public static final String I18N_CHART_TITLE_DEFAULT =
69 "Dauerlinie";
70
71 public static final String I18N_XAXIS_LABEL_DEFAULT =
72 "Unterschreitungsdauer [Tage]";
73
74 60
75 public DurationCurveGenerator() { 61 public DurationCurveGenerator() {
76 super(); 62 super();
77 } 63 }
78 64
79
80 /** 65 /**
81 * Create Axis for given index. 66 * Create Axis for given index.
67 *
82 * @return axis with according internationalized label. 68 * @return axis with according internationalized label.
83 */ 69 */
84 @Override 70 @Override
85 protected NumberAxis createYAxis(int index) { 71 protected NumberAxis createYAxis(final int index) {
86 Font labelFont = new Font("Tahoma", Font.BOLD, 14); 72 final Font labelFont = new Font("Tahoma", Font.BOLD, 14);
87 String label = getYAxisLabel(index); 73 final String label = getYAxisLabel(index);
88 74
89 NumberAxis axis = createNumberAxis(index, label); 75 final NumberAxis axis = createNumberAxis(index, label);
90 if (index == YAXIS.W.idx) { 76 if (index == YAXIS.W.idx) {
91 axis.setAutoRangeIncludesZero(false); 77 axis.setAutoRangeIncludesZero(false);
92 } 78 }
93 axis.setLabelFont(labelFont); 79 axis.setLabelFont(labelFont);
94 return axis; 80 return axis;
95 } 81 }
96 82
97
98 @Override 83 @Override
99 protected String getDefaultChartTitle(final CallContext context) { 84 protected String getDefaultChartTitle(final CallContext context) {
100 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); 85 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
101 } 86 }
102 87
103
104 @Override 88 @Override
105 protected String getDefaultChartSubtitle(final CallContext context) { 89 protected String getDefaultChartSubtitle(final CallContext context) {
106 double[] dist = getRange(); 90 final double[] dist = getRange();
107 91
108 Object[] args = new Object[] { 92 final Object[] args = new Object[] { getRiverName(), dist[0] };
109 getRiverName(),
110 dist[0]
111 };
112 93
113 return msg(I18N_CHART_SUBTITLE, "", args); 94 return msg(I18N_CHART_SUBTITLE, "", args);
114 } 95 }
115
116 96
117 @Override 97 @Override
118 protected String getDefaultXAxisLabel(final CallContext context) { 98 protected String getDefaultXAxisLabel(final CallContext context) {
119 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT); 99 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
120 } 100 }
121 101
122 102 @Override
123 @Override 103 protected String getDefaultYAxisLabel(final int index) {
124 protected String getDefaultYAxisLabel(int index) {
125 String label = "default"; 104 String label = "default";
126 if (index == YAXIS.W.idx) { 105 if (index == YAXIS.W.idx) {
127 label = msg(I18N_YAXIS_LABEL_W, I18N_YAXIS_LABEL_W, new Object[] { getRiverUnit() }); 106 label = msg(I18N_YAXIS_LABEL_W, I18N_YAXIS_LABEL_W, new Object[] { getRiverUnit() });
128 } 107 } else if (index == YAXIS.Q.idx) {
129 else if (index == YAXIS.Q.idx) {
130 label = msg(I18N_YAXIS_LABEL_Q); 108 label = msg(I18N_YAXIS_LABEL_Q);
131 } 109 }
132 110
133 return label; 111 return label;
134 } 112 }
135 113
136 114 @Override
137 @Override 115 protected boolean zoomX(final XYPlot plot, final ValueAxis axis, final Bounds bounds, final Range x) {
138 protected boolean zoomX( 116 final boolean zoomin = super.zoom(plot, axis, bounds, x);
139 XYPlot plot,
140 ValueAxis axis,
141 Bounds bounds,
142 Range x
143 ) {
144 boolean zoomin = super.zoom(plot, axis, bounds, x);
145 117
146 if (!zoomin) { 118 if (!zoomin) {
147 axis.setLowerBound(0d); 119 axis.setLowerBound(0d);
148 } 120 }
149 121
150 axis.setUpperBound(364); 122 axis.setUpperBound(364);
151 123
152 return zoomin; 124 return zoomin;
153 } 125 }
154
155 126
156 /** 127 /**
157 * This method overrides the method in the parent class to set the lower 128 * This method overrides the method in the parent class to set the lower
158 * bounds of the Q axis to 0. This axis should never display negative 129 * bounds of the Q axis to 0. This axis should never display negative
159 * values on its own. 130 * values on its own.
160 */ 131 */
161 @Override 132 @Override
162 protected boolean zoomY( 133 protected boolean zoomY(final XYPlot plot, final ValueAxis axis, final Bounds bounds, final Range x) {
163 XYPlot plot, 134 final boolean zoomin = super.zoom(plot, axis, bounds, x);
164 ValueAxis axis,
165 Bounds bounds,
166 Range x
167 ) {
168 boolean zoomin = super.zoom(plot, axis, bounds, x);
169 135
170 if (!zoomin && axis instanceof IdentifiableNumberAxis) { 136 if (!zoomin && axis instanceof IdentifiableNumberAxis) {
171 String id = ((IdentifiableNumberAxis) axis).getId(); 137 final String id = ((IdentifiableNumberAxis) axis).getId();
172 138
173 if (YAXIS.Q.toString().equals(id)) { 139 if (YAXIS.Q.toString().equals(id)) {
174 axis.setLowerBound(0d); 140 axis.setLowerBound(0d);
175 } 141 }
176 } 142 }
177 143
178 return zoomin; 144 return zoomin;
179 } 145 }
180 146
181 147 @Override
182 @Override 148 public void doOut(final ArtifactAndFacet artifactFacet, final ThemeDocument attr, final boolean visible) {
183 public void doOut( 149 final String name = artifactFacet.getFacetName();
184 ArtifactAndFacet artifactFacet,
185 ThemeDocument attr,
186 boolean visible
187 ) {
188 String name = artifactFacet.getFacetName();
189 150
190 log.debug("DurationCurveGenerator.doOut: " + name); 151 log.debug("DurationCurveGenerator.doOut: " + name);
191 152
192 if (name == null || name.length() == 0) { 153 if (name == null || name.length() == 0) {
193 log.error("No facet given. Cannot create dataset."); 154 log.error("No facet given. Cannot create dataset.");
194 return; 155 return;
195 } 156 }
196 157
197 final CallContext context = getContext(); 158 final CallContext context = getContext();
198 159
199 if (name.equals(DURATION_W)) { 160 if (name.equals(DURATION_W)) {
200 doWOut( 161 doWOut((WQDay) artifactFacet.getData(context), artifactFacet, attr, visible);
201 (WQDay) artifactFacet.getData(context), 162 } else if (name.equals(DURATION_Q)) {
202 artifactFacet, 163 doQOut((WQDay) artifactFacet.getData(context), artifactFacet, attr, visible);
203 attr, 164 } else if (name.equals(MAINVALUES_Q) || name.equals(MAINVALUES_W)) {
204 visible); 165 doAnnotations((RiverAnnotation) artifactFacet.getData(context), artifactFacet, attr, visible);
205 } 166 } else if (name.equals(RELATIVE_POINT)) {
206 else if (name.equals(DURATION_Q)) { 167 doPointOut((Point2D) artifactFacet.getData(context), artifactFacet, attr, visible);
207 doQOut( 168 } else if (FacetTypes.IS.MANUALPOINTS(name)) {
208 (WQDay) artifactFacet.getData(context), 169 doPoints(artifactFacet.getData(context), artifactFacet, attr, visible, YAXIS.W.idx);
209 artifactFacet, 170 } else {
210 attr,
211 visible);
212 }
213 else if (name.equals(MAINVALUES_Q) || name.equals(MAINVALUES_W)) {
214 doAnnotations(
215 (RiverAnnotation) artifactFacet.getData(context),
216 artifactFacet,
217 attr,
218 visible);
219 }
220 else if (name.equals(RELATIVE_POINT)) {
221 doPointOut((Point2D) artifactFacet.getData(context),
222 artifactFacet,
223 attr,
224 visible);
225 }
226 else if (FacetTypes.IS.MANUALPOINTS(name)) {
227 doPoints(
228 artifactFacet.getData(context),
229 artifactFacet,
230 attr, visible, YAXIS.W.idx);
231 }
232 else {
233 log.warn("Unknown facet name: " + name); 171 log.warn("Unknown facet name: " + name);
234 return; 172 return;
235 } 173 }
236 } 174 }
237 175
238
239 /** 176 /**
240 * Creates the series for a duration curve's W facet. 177 * Creates the series for a duration curve's W facet.
241 * 178 *
242 * @param wqdays The WQDay store that contains the Ws. 179 * @param wqdays
180 * The WQDay store that contains the Ws.
243 * @param theme 181 * @param theme
244 */ 182 */
245 protected void doWOut( 183 protected void doWOut(final WQDay wqdays, final ArtifactAndFacet aaf, final ThemeDocument theme, final boolean visible) {
246 WQDay wqdays,
247 ArtifactAndFacet aaf,
248 ThemeDocument theme,
249 boolean visible
250 ) {
251 log.debug("DurationCurveGenerator.doWOut"); 184 log.debug("DurationCurveGenerator.doWOut");
252 185
253 XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme); 186 final XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
254 187
255 int size = wqdays.size(); 188 final int size = wqdays.size();
256 for (int i = 0; i < size; i++) { 189 for (int i = 0; i < size; i++) {
257 int day = wqdays.getDay(i); 190 final int day = wqdays.getDay(i);
258 double w = wqdays.getW(i); 191 final double w = wqdays.getW(i);
259 192
260 series.add(day, w); 193 series.add(day, w);
261 } 194 }
262 195
263 addAxisSeries(series, YAXIS.W.idx, visible); 196 addAxisSeries(series, YAXIS.W.idx, visible);
264 } 197 }
265 198
266 protected void doPointOut( 199 protected void doPointOut(final Point2D point, final ArtifactAndFacet aandf, final ThemeDocument theme, final boolean visible) {
267 Point2D point,
268 ArtifactAndFacet aandf,
269 ThemeDocument theme,
270 boolean visible
271 ){
272 log.debug("DurationCurveGenerator.doPointOut"); 200 log.debug("DurationCurveGenerator.doPointOut");
273 201
274 XYSeries series = 202 final XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);
275 new StyledXYSeries(aandf.getFacetDescription(), theme);
276 203
277 series.add(point.getX(), point.getY()); 204 series.add(point.getX(), point.getY());
278 205
279 addAxisSeries(series, YAXIS.W.idx, visible); 206 addAxisSeries(series, YAXIS.W.idx, visible);
280 } 207 }
281
282 208
283 /** 209 /**
284 * Creates the series for a duration curve's Q facet. 210 * Creates the series for a duration curve's Q facet.
285 * 211 *
286 * @param wqdays The WQDay store that contains the Qs. 212 * @param wqdays
213 * The WQDay store that contains the Qs.
287 * @param theme 214 * @param theme
288 */ 215 */
289 protected void doQOut( 216 protected void doQOut(final WQDay wqdays, final ArtifactAndFacet aaf, final ThemeDocument theme, final boolean visible) {
290 WQDay wqdays,
291 ArtifactAndFacet aaf,
292 ThemeDocument theme,
293 boolean visible
294 ) {
295 log.debug("DurationCurveGenerator.doQOut"); 217 log.debug("DurationCurveGenerator.doQOut");
296 218
297 XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme); 219 final XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
298 220
299 int size = wqdays.size(); 221 final int size = wqdays.size();
300 for (int i = 0; i < size; i++) { 222 for (int i = 0; i < size; i++) {
301 int day = wqdays.getDay(i); 223 final int day = wqdays.getDay(i);
302 double q = wqdays.getQ(i); 224 final double q = wqdays.getQ(i);
303 225
304 series.add(day, q); 226 series.add(day, q);
305 } 227 }
306 228
307 addAxisSeries(series, YAXIS.Q.idx, visible); 229 addAxisSeries(series, YAXIS.Q.idx, visible);
308 } 230 }
309
310 231
311 @Override 232 @Override
312 protected YAxisWalker getYAxisWalker() { 233 protected YAxisWalker getYAxisWalker() {
313 return new YAxisWalker() { 234 return new YAxisWalker() {
314 @Override 235 @Override
315 public int length() { 236 public int length() {
316 return YAXIS.values().length; 237 return YAXIS.values().length;
317 } 238 }
318 239
319 @Override 240 @Override
320 public String getId(int idx) { 241 public String getId(final int idx) {
321 YAXIS[] yaxes = YAXIS.values(); 242 final YAXIS[] yaxes = YAXIS.values();
322 return yaxes[idx].toString(); 243 return yaxes[idx].toString();
323 } 244 }
324 }; 245 };
325 } 246 }
326 247

http://dive4elements.wald.intevation.org