comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java @ 8467:5fa1c8be43db

(issue1767) Fix lookup of closest value. If no match can be found stay on the same value. If an equal match can be found use it.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 05 Nov 2014 15:44:04 +0100
parents 2772a0fc7400
children 67ae70259011
comparison
equal deleted inserted replaced
8466:cc53aae06303 8467:5fa1c8be43db
279 * @param to the value to be as close to as possible. 279 * @param to the value to be as close to as possible.
280 * @param up if true, prefer numerically higher values in case of two 280 * @param up if true, prefer numerically higher values in case of two
281 * values with equal distance to \param to. 281 * values with equal distance to \param to.
282 * @return value from in that is closest to to, -1 if none. 282 * @return value from in that is closest to to, -1 if none.
283 */ 283 */
284 public static double closest(Double[] in, double to, boolean up) { 284 public static Double closest(Double[] in, double to, boolean up) {
285 if (in == null || in.length == 0) { 285 if (in == null || in.length == 0) {
286 return -1; 286 return Double.NaN;
287 } 287 }
288 Arrays.sort(in); 288 Arrays.sort(in);
289 GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " + 289 GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " +
290 to + " candidates: " + Arrays.toString(in)); 290 to + " candidates: " + Arrays.toString(in));
291 if (up) { 291 if (up) {
292 // take the first one that is larger 292 // take the first one that is larger
293 for (int i = 0; i < in.length; i++) { 293 for (int i = 0; i < in.length; i++) {
294 if (in[i] > to) { 294 if (in[i] >= to) {
295 return in[i]; 295 return in[i];
296 } 296 }
297 } 297 }
298 } else { 298 } else {
299 for (int i = in.length - 1; i >= 0; i--) { 299 for (int i = in.length - 1; i >= 0; i--) {
300 if (in[i] < to) { 300 if (in[i] <= to) {
301 return in[i]; 301 return in[i];
302 } 302 }
303 } 303 }
304 } 304 }
305 GWT.log("Failed to find closest match"); 305 GWT.log("Failed to find closest match");
306 return 0; 306 return Double.NaN;
307 } 307 }
308 308
309 309
310 /** 310 /**
311 * Feed artifacts with the km of the crosssection to display. 311 * Feed artifacts with the km of the crosssection to display.
445 } 445 }
446 446
447 @Override 447 @Override
448 public void onSuccess(Map<Integer, Double[]> obj) { 448 public void onSuccess(Map<Integer, Double[]> obj) {
449 Double[] kms = obj.get(dbid); 449 Double[] kms = obj.get(dbid);
450 double closest = 450 Double closest =
451 CrossSectionChartThemePanel.closest(kms, enteredKm, up); 451 CrossSectionChartThemePanel.closest(kms, enteredKm, up);
452 if (Double.isNaN(closest)) {
453 GWT.log("Failed to find a closest km. Staying on current km.");
454 updateCollection();
455 enable();
456 return;
457 }
458
452 GWT.log("Got single km close to " + enteredKm + " for " + dbid + ", it is " 459 GWT.log("Got single km close to " + enteredKm + " for " + dbid + ", it is "
453 + closest); 460 + closest);
454 461
455 // Do not set value, as it will trigger strange 462 // Do not set value, as it will trigger strange
456 // "javascript" bugs. /*item.setValue(closest);*/ 463 // "javascript" bugs. /*item.setValue(closest);*/

http://dive4elements.wald.intevation.org