comparison gnv-artifacts/src/main/java/de/intevation/gnv/histogram/HistogramHelper.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents b1f5f2a8840f
children 01e26528bb39
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.histogram;
2
3 import de.intevation.gnv.geobackend.base.Result;
4 import de.intevation.gnv.geobackend.base.ResultDescriptor;
5
6 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
7
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.apache.log4j.Logger;
14
15 /**
16 * This class supports some helper methods for histogram charts.
17 *
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
19 */
20 public class HistogramHelper {
21
22 /**
23 * Logger used for logging with log4j.
24 */
25 private static Logger logger = Logger.getLogger(HistogramHelper.class);
26
27
28 /**
29 * Disabled HistogramHelper constructor. This is a helper class and no
30 * instance should be instantiated from this class.
31 */
32 private HistogramHelper() {
33 }
34
35
36 /**
37 * This function prepare some input data and turns it into an array which is
38 * taken by {@link de.intevation.gnv.chart.DefaultHistogram}.
39 *
40 * @param input A collection with the data used to be displayed in a
41 * histogram
42 * @param parameters A collection with a bunch of parameters
43 * @param measurements A collection with a bunch of measurements
44 * @param dates A collection with a bunch of dates
45 *
46 * @return Object[][] containing raw data which can be used to create
47 * histograms
48 */
49 public static Object[][] prepareHistogramData(
50 Collection input,
51 Collection parameters,
52 Collection measurements,
53 Collection dates
54 ) {
55 List names = new ArrayList<String>();
56 List data = new ArrayList<Double[]>();
57
58 if (logger.isDebugEnabled()) {
59 logger.debug("############ prepare histogram data ##########");
60 logger.debug("Input data size: " + input.size());
61 }
62
63 if (input == null) {
64 return new Object[0][0];
65 }
66
67 String break1, break2, break3;
68 int b1Idx = -1;
69 int b2Idx = -1;
70 int b3Idx = -1;
71 int yIdx = -1;
72 try {
73 Iterator iter = input.iterator();
74
75 if (iter.hasNext()) {
76 Result row = (Result) iter.next();
77 Result previousRow = row;
78
79 if (b1Idx == -1) {
80 ResultDescriptor rd = row.getResultDescriptor();
81 b1Idx = rd.getColumnIndex("GROUP1");
82 b2Idx = rd.getColumnIndex("GROUP2");
83 b3Idx = rd.getColumnIndex("GROUP3");
84 yIdx = rd.getColumnIndex("YORDINATE");
85
86 if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
87 return new Object[0][0];
88 }
89 }
90 break1 = row.getString(b1Idx);
91 break2 = row.getString(b2Idx);
92 break3 = row.getString(b3Idx);
93
94 List values = new ArrayList<Double>();
95 while (iter.hasNext()) {
96
97 // found new series
98 if (!break1.equals(row.getString(b1Idx))
99 || !break2.equals(row.getString(b2Idx))
100 || !break3.equals(row.getString(b3Idx))
101 ) {
102
103 // get parameter name
104 String name = generateName(
105 break1, break2, break3,
106 parameters, measurements, dates
107 );
108
109 // add values and parameter name
110 data.add((Double[]) values.toArray(new Double[values.size()]));
111 names.add(name);
112
113 if (logger.isDebugEnabled()) {
114 logger.debug(" --- series name: " + name);
115 logger.debug(" --- series items: " + values.size());
116 }
117
118 values.clear();
119
120 Double yValue = row.getDouble(yIdx);
121 if (yValue != null)
122 values.add(yValue);
123
124 // set new conditions to find new series
125 break1 = row.getString(b1Idx);
126 break2 = row.getString(b2Idx);
127 break3 = row.getString(b3Idx);
128
129 previousRow = row;
130 row = (Result) iter.next();
131 } else {
132
133 Double value = row.getDouble(yIdx);
134 if (value != null)
135 values.add(value);
136
137 row = (Result) iter.next();
138 }
139 }
140
141 Double yValue = row.getDouble(yIdx);
142 if (yValue != null)
143 values.add(yValue);
144
145 String name = generateName(
146 break1, break2, break3, parameters, measurements, dates);
147
148 if (logger.isDebugEnabled()) {
149 logger.debug(" --- series name: " + name);
150 logger.debug(" --- series items: " + values.size());
151 }
152
153 data.add((Double[]) values.toArray(new Double[values.size()]));
154 names.add(name);
155 }
156 }
157 catch (Exception e) {
158 logger.error(e.getMessage(), e);
159 }
160
161 int series = data.size();
162 logger.debug(" === Found total: " + series);
163 Object[][] obj = new Object[series][2];
164 for (int i = 0; i < series; i++) {
165 obj[i][0] = names.get(i);
166 obj[i][1] = (Double[]) data.get(i);
167 }
168
169 return obj;
170 }
171
172
173 /**
174 * This method generates a string made up of parameter name and a
175 * measurement.
176 *
177 * @param break1 Id of a parameter.
178 * @param break2 Id of a measurement.
179 * @param break3 Id of a date.
180 * @param parameters A collection with a bunch of parameters.
181 * @param measurements A collection with a bunch of measurements.
182 * @param dates A collection with a bunch of dates.
183 *
184 * @return Concatenated string (${parametername} + ${measurement} + m).
185 */
186 protected static String generateName(
187 String break1, String break2, String break3,
188 Collection parameters, Collection measurements, Collection dates)
189 {
190 return findValueTitle(parameters,break1) + " " +
191 findValueTitle(measurements,break2) + "m";
192 }
193
194
195 /**
196 * Find a value with the given <code>id</code> and return its description.
197 *
198 * @param values A collection which contains the value we are searching for
199 * @param id Id of the value
200 *
201 * @return String representation of the value. An empty string is returned
202 * if no value have been found with the given id.
203 */
204 protected static String findValueTitle(Collection values, String id) {
205 if (values != null) {
206 Iterator it = values.iterator();
207
208 while (it.hasNext()) {
209 KeyValueDescibeData data = (KeyValueDescibeData) it.next();
210
211 if (id.equals(data.getKey())) {
212 return data.getValue();
213 }
214 }
215 }
216 return "";
217 }
218 }
219 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org