histogram/src/main/java/dew/demo/histogram/DataModel.java
branchchess
changeset 87 c2293ae622df
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/histogram/src/main/java/dew/demo/histogram/DataModel.java	Sun Nov 17 09:46:45 2013 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/**
     1.5 + * The MIT License (MIT)
     1.6 + *
     1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.8 + *
     1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.10 + * of this software and associated documentation files (the "Software"), to deal
    1.11 + * in the Software without restriction, including without limitation the rights
    1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.13 + * copies of the Software, and to permit persons to whom the Software is
    1.14 + * furnished to do so, subject to the following conditions:
    1.15 + *
    1.16 + * The above copyright notice and this permission notice shall be included in
    1.17 + * all copies or substantial portions of the Software.
    1.18 + *
    1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    1.25 + * THE SOFTWARE.
    1.26 + */
    1.27 +package dew.demo.histogram;
    1.28 +
    1.29 +import java.util.ArrayList;
    1.30 +import java.util.List;
    1.31 +import net.java.html.json.ComputedProperty;
    1.32 +import net.java.html.json.Model;
    1.33 +import net.java.html.json.Property;
    1.34 +
    1.35 +/** Model annotation generates class Data with 
    1.36 + * one property for list of of numbers and read-only property
    1.37 + * for ten of bars.
    1.38 + */
    1.39 +@Model(className = "Data", properties = {
    1.40 +    @Property(name = "numbers", type = String.class)
    1.41 +})
    1.42 +final class DataModel {
    1.43 +    @ComputedProperty static java.util.List<Bar> bars(String numbers) {
    1.44 +        List<Bar> arr = new ArrayList<Bar>(10);
    1.45 +        for (int i = 0; i < 10; i++) {
    1.46 +            arr.add(new Bar(i, 0));
    1.47 +        }
    1.48 +        String[] words = numbers == null ? new String[0] : numbers.split(" ");
    1.49 +        for (String word : words) {
    1.50 +            try {
    1.51 +                double n = Double.parseDouble(word);
    1.52 +                if (n > 99) {
    1.53 +                    n = 99;
    1.54 +                } else if (n <= 0) {
    1.55 +                    n = 0;
    1.56 +                }
    1.57 +                Bar b = arr.get((int) (n / 10));
    1.58 +                b.setValue(b.getValue() + 1);
    1.59 +            }catch (NumberFormatException ex) {
    1.60 +                // ignore
    1.61 +            }
    1.62 +        }
    1.63 +        return arr;
    1.64 +    }
    1.65 +
    1.66 +    @Model(className = "Bar", properties = {
    1.67 +        @Property(name = "index", type = int.class),
    1.68 +        @Property(name = "value", type = int.class)
    1.69 +    })
    1.70 +    static class BarModel {
    1.71 +        @ComputedProperty static String left(int index) {
    1.72 +            return (index * 50 + 20) + "px";
    1.73 +        }
    1.74 +        
    1.75 +        @ComputedProperty static String top(int value) {
    1.76 +            return (199 - value * 20) + "px";
    1.77 +        }
    1.78 +        
    1.79 +        @ComputedProperty static String height(int value) {
    1.80 +            return 20 * value + "px";
    1.81 +        }
    1.82 +    }
    1.83 +}