comparison gnv-artifacts/src/main/java/de/intevation/gnv/histogram/HistogramHelper.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.histogram;
10
11 import de.intevation.artifacts.CallContext;
12
13 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
14
15 import de.intevation.gnv.chart.ChartLabels;
16
17 import de.intevation.gnv.geobackend.base.Result;
18 import de.intevation.gnv.geobackend.base.ResultDescriptor;
19
20 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.log4j.Logger;
29
30 /**
31 * This class supports some helper methods for histogram charts.
32 *
33 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
34 */
35 public class HistogramHelper {
36
37 /**
38 * Logger used for logging with log4j.
39 */
40 private static Logger logger = Logger.getLogger(HistogramHelper.class);
41
42
43 /**
44 * Disabled HistogramHelper constructor. This is a helper class and no
45 * instance should be instantiated from this class.
46 */
47 private HistogramHelper() {
48 }
49
50
51 /**
52 * This function prepare some input data and turns it into an array which is
53 * taken by {@link de.intevation.gnv.chart.DefaultHistogram}.
54 *
55 * @param input A collection with the data used to be displayed in a
56 * histogram
57 * @param parameters A collection with a bunch of parameters
58 * @param measurements A collection with a bunch of measurements
59 * @param dates A collection with a bunch of dates
60 *
61 * @return Object[][] containing raw data which can be used to create
62 * histograms
63 */
64 public static Object[][] prepareHistogramData(
65 Collection input,
66 Collection parameters,
67 Collection measurements,
68 Collection dates
69 ) {
70 List names = new ArrayList<String>();
71 List data = new ArrayList<Double[]>();
72
73 if (logger.isDebugEnabled()) {
74 logger.debug("############ prepare histogram data ##########");
75 logger.debug("Input data size: " + input.size());
76 }
77
78 if (input == null) {
79 return new Object[0][0];
80 }
81
82 String break1, break2, break3;
83 int b1Idx = -1;
84 int b2Idx = -1;
85 int b3Idx = -1;
86 int yIdx = -1;
87 try {
88 Iterator iter = input.iterator();
89
90 if (iter.hasNext()) {
91 Result row = (Result) iter.next();
92 Result previousRow = row;
93
94 if (b1Idx == -1) {
95 ResultDescriptor rd = row.getResultDescriptor();
96 b1Idx = rd.getColumnIndex("GROUP1");
97 b2Idx = rd.getColumnIndex("GROUP2");
98 b3Idx = rd.getColumnIndex("GROUP3");
99 yIdx = rd.getColumnIndex("YORDINATE");
100
101 if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
102 return new Object[0][0];
103 }
104 }
105 break1 = row.getString(b1Idx);
106 break2 = row.getString(b2Idx);
107 break3 = row.getString(b3Idx);
108
109 List values = new ArrayList<Double>();
110 while (iter.hasNext()) {
111
112 // found new series
113 if (!break1.equals(row.getString(b1Idx))
114 || !break2.equals(row.getString(b2Idx))
115 || !break3.equals(row.getString(b3Idx))
116 ) {
117
118 // get parameter name
119 String name = generateName(
120 break1, break2, break3,
121 parameters, measurements, dates
122 );
123
124 // add values and parameter name
125 data.add((Double[]) values.toArray(new Double[values.size()]));
126 names.add(name);
127
128 if (logger.isDebugEnabled()) {
129 logger.debug(" --- series name: " + name);
130 logger.debug(" --- series items: " + values.size());
131 }
132
133 values.clear();
134
135 Double yValue = row.getDouble(yIdx);
136 if (yValue != null)
137 values.add(yValue);
138
139 // set new conditions to find new series
140 break1 = row.getString(b1Idx);
141 break2 = row.getString(b2Idx);
142 break3 = row.getString(b3Idx);
143
144 previousRow = row;
145 row = (Result) iter.next();
146 } else {
147
148 Double value = row.getDouble(yIdx);
149 if (value != null)
150 values.add(value);
151
152 row = (Result) iter.next();
153 }
154 }
155
156 Double yValue = row.getDouble(yIdx);
157 if (yValue != null)
158 values.add(yValue);
159
160 String name = generateName(
161 break1, break2, break3, parameters, measurements, dates);
162
163 if (logger.isDebugEnabled()) {
164 logger.debug(" --- series name: " + name);
165 logger.debug(" --- series items: " + values.size());
166 }
167
168 data.add((Double[]) values.toArray(new Double[values.size()]));
169 names.add(name);
170 }
171 }
172 catch (Exception e) {
173 logger.error(e.getMessage(), e);
174 }
175
176 int series = data.size();
177 logger.debug(" === Found total: " + series);
178 Object[][] obj = new Object[series][2];
179 for (int i = 0; i < series; i++) {
180 obj[i][0] = names.get(i);
181 obj[i][1] = (Double[]) data.get(i);
182 }
183
184 return obj;
185 }
186
187
188 /**
189 * This method generates a string made up of parameter name and a
190 * measurement.
191 *
192 * @param break1 Id of a parameter.
193 * @param break2 Id of a measurement.
194 * @param break3 Id of a date.
195 * @param parameters A collection with a bunch of parameters.
196 * @param measurements A collection with a bunch of measurements.
197 * @param dates A collection with a bunch of dates.
198 *
199 * @return Concatenated string (${parametername} + ${measurement} + m).
200 */
201 protected static String generateName(
202 String break1, String break2, String break3,
203 Collection parameters, Collection measurements, Collection dates)
204 {
205 return findValueTitle(parameters,break1) + " " +
206 findValueTitle(measurements,break2) + "m";
207 }
208
209
210 /**
211 * Find a value with the given <code>id</code> and return its description.
212 *
213 * @param values A collection which contains the value we are searching for
214 * @param id Id of the value
215 *
216 * @return String representation of the value. An empty string is returned
217 * if no value have been found with the given id.
218 */
219 protected static String findValueTitle(Collection values, String id) {
220 if (values != null) {
221 Iterator it = values.iterator();
222
223 while (it.hasNext()) {
224 KeyValueDescibeData data = (KeyValueDescibeData) it.next();
225
226 if (id.equals(data.getKey())) {
227 return data.getValue();
228 }
229 }
230 }
231 return "";
232 }
233
234
235 /**
236 * Creates and returns labels to decorate histograms.
237 *
238 * @param uuid The UUID of the current artifact.
239 * @param context The CallContext object.
240 * @param data An array storing strings.
241 * @return A ChartLabels object with the 1st string in <i>data</i> as title.
242 */
243 public static ChartLabels createHistogramLabels(
244 String uuid, CallContext context, Locale locale, Object[] data)
245 {
246 RessourceFactory fac = RessourceFactory.getInstance();
247
248 return new ChartLabels(
249 (String) data[0],
250 "",
251 "",
252 fac.getRessource(locale, "histogram.axis.range.title", ""));
253 }
254
255
256 public static Object[][] prepareVectorialHistogramData(Collection input) {
257 List names = new ArrayList<String>();
258 List data = new ArrayList<Double[]>();
259
260 if (logger.isDebugEnabled()) {
261 logger.debug("######### prepare vectorial histogram data #######");
262 logger.debug("Input data size: " + input.size());
263 }
264
265 if (input == null) {
266 return new Object[0][0];
267 }
268
269 int sIdx = -1;
270 int yIdx = -1;
271
272 String series = null;
273
274 try {
275 Iterator iter = input.iterator();
276
277 if (iter.hasNext()) {
278 Result row = (Result) iter.next();
279 Result previousRow = row;
280
281 if (sIdx == -1 || yIdx == -1) {
282 ResultDescriptor rd = row.getResultDescriptor();
283 sIdx = rd.getColumnIndex("SERIES");
284 yIdx = rd.getColumnIndex("YORDINATE");
285
286 if (sIdx == -1 || yIdx == -1) {
287 return new Object[0][0];
288 }
289 }
290
291 List values = new ArrayList<Double>();
292 while (iter.hasNext()) {
293 row = (Result) iter.next();
294
295 // found new series
296 if (series != null && !series.equals(row.getString(sIdx))) {
297
298 // add values and parameter name
299 data.add((Double[])
300 values.toArray(new Double[values.size()]));
301 names.add(series);
302
303 if (logger.isDebugEnabled()) {
304 logger.debug(" --- series name: " + series);
305 logger.debug(" --- series items: " + values.size());
306 }
307
308 values.clear();
309 }
310
311 Double value = row.getDouble(yIdx);
312 if (value != null)
313 values.add(value);
314
315 series = row.getString(sIdx);
316 }
317
318 if (logger.isDebugEnabled()) {
319 logger.debug(" --- series name: " + series);
320 logger.debug(" --- series items: " + values.size());
321 }
322
323 data.add((Double[]) values.toArray(new Double[values.size()]));
324 names.add(series);
325 }
326 }
327 catch (Exception e) {
328 logger.error(e.getMessage(), e);
329 }
330
331 int count = data.size();
332 logger.debug(" === Found total: " + count);
333 Object[][] obj = new Object[count][2];
334 for (int i = 0; i < count; i++) {
335 obj[i][0] = names.get(i);
336 obj[i][1] = (Double[]) data.get(i);
337 }
338
339 return obj;
340 }
341 }
342 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org