comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java @ 8465:2772a0fc7400

(issue1767) Fix closet next value selection. This simplyfies the code and thus fixes the overly complicated old version.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 04 Nov 2014 17:09:04 +0100
parents 8a425ee702e3
children 5fa1c8be43db
comparison
equal deleted inserted replaced
8464:5dd6cc9fec1e 8465:2772a0fc7400
42 import org.dive4elements.river.client.shared.model.OutputMode; 42 import org.dive4elements.river.client.shared.model.OutputMode;
43 import org.dive4elements.river.client.shared.model.Theme; 43 import org.dive4elements.river.client.shared.model.Theme;
44 import org.dive4elements.river.client.shared.model.ThemeList; 44 import org.dive4elements.river.client.shared.model.ThemeList;
45 45
46 import java.util.ArrayList; 46 import java.util.ArrayList;
47 import java.util.Arrays;
47 import java.util.HashMap; 48 import java.util.HashMap;
48 import java.util.HashSet; 49 import java.util.HashSet;
49 import java.util.LinkedHashMap; 50 import java.util.LinkedHashMap;
50 import java.util.List; 51 import java.util.List;
51 import java.util.Map; 52 import java.util.Map;
282 */ 283 */
283 public static double closest(Double[] in, double to, boolean up) { 284 public static double closest(Double[] in, double to, boolean up) {
284 if (in == null || in.length == 0) { 285 if (in == null || in.length == 0) {
285 return -1; 286 return -1;
286 } 287 }
287 if (in[0] == to) { 288 Arrays.sort(in);
288 return to; 289 GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " +
289 } 290 to + " candidates: " + Arrays.toString(in));
290 for (int i = 0; i < in.length; i++) { 291 if (up) {
291 GWT.log ("Close? " + in[i]); 292 // take the first one that is larger
292 } 293 for (int i = 0; i < in.length; i++) {
293 294 if (in[i] > to) {
294 double minDiff = Math.abs(to - in[0]); 295 return in[i];
295 double bestMatch = in[0]; 296 }
296 for (int i = 1; i < in.length; i++) { 297 }
297 if (in[i] == to) { 298 } else {
298 return to; 299 for (int i = in.length - 1; i >= 0; i--) {
299 } 300 if (in[i] < to) {
300 if ((in[i] < to && up) 301 return in[i];
301 || (in[i] > to && !up)) { 302 }
302 continue; 303 }
303 } 304 }
304 double diff = Math.abs(to - in[i]); 305 GWT.log("Failed to find closest match");
305 if (diff <= minDiff) { 306 return 0;
306 minDiff = diff;
307 bestMatch = in[i];
308 }
309 }
310 return bestMatch;
311 } 307 }
312 308
313 309
314 /** 310 /**
315 * Feed artifacts with the km of the crosssection to display. 311 * Feed artifacts with the km of the crosssection to display.

http://dive4elements.wald.intevation.org