diff -r 000000000000 -r c2293ae622df histogram/src/main/java/dew/demo/histogram/DataModel.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/histogram/src/main/java/dew/demo/histogram/DataModel.java Sun Nov 17 09:46:45 2013 +0100 @@ -0,0 +1,80 @@ +/** + * The MIT License (MIT) + * + * Copyright (C) 2013 Jaroslav Tulach + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package dew.demo.histogram; + +import java.util.ArrayList; +import java.util.List; +import net.java.html.json.ComputedProperty; +import net.java.html.json.Model; +import net.java.html.json.Property; + +/** Model annotation generates class Data with + * one property for list of of numbers and read-only property + * for ten of bars. + */ +@Model(className = "Data", properties = { + @Property(name = "numbers", type = String.class) +}) +final class DataModel { + @ComputedProperty static java.util.List bars(String numbers) { + List arr = new ArrayList(10); + for (int i = 0; i < 10; i++) { + arr.add(new Bar(i, 0)); + } + String[] words = numbers == null ? new String[0] : numbers.split(" "); + for (String word : words) { + try { + double n = Double.parseDouble(word); + if (n > 99) { + n = 99; + } else if (n <= 0) { + n = 0; + } + Bar b = arr.get((int) (n / 10)); + b.setValue(b.getValue() + 1); + }catch (NumberFormatException ex) { + // ignore + } + } + return arr; + } + + @Model(className = "Bar", properties = { + @Property(name = "index", type = int.class), + @Property(name = "value", type = int.class) + }) + static class BarModel { + @ComputedProperty static String left(int index) { + return (index * 50 + 20) + "px"; + } + + @ComputedProperty static String top(int value) { + return (199 - value * 20) + "px"; + } + + @ComputedProperty static String height(int value) { + return 20 * value + "px"; + } + } +}