histogram/src/main/java/dew/demo/histogram/DataModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 17 Nov 2013 09:46:45 +0100
branchchess
changeset 87 c2293ae622df
permissions -rw-r--r--
Adding histogram demo
jtulach@87
     1
/**
jtulach@87
     2
 * The MIT License (MIT)
jtulach@87
     3
 *
jtulach@87
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@87
     5
 *
jtulach@87
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@87
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@87
     8
 * in the Software without restriction, including without limitation the rights
jtulach@87
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@87
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@87
    11
 * furnished to do so, subject to the following conditions:
jtulach@87
    12
 *
jtulach@87
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@87
    14
 * all copies or substantial portions of the Software.
jtulach@87
    15
 *
jtulach@87
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@87
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@87
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@87
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@87
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@87
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@87
    22
 * THE SOFTWARE.
jtulach@87
    23
 */
jtulach@87
    24
package dew.demo.histogram;
jtulach@87
    25
jtulach@87
    26
import java.util.ArrayList;
jtulach@87
    27
import java.util.List;
jtulach@87
    28
import net.java.html.json.ComputedProperty;
jtulach@87
    29
import net.java.html.json.Model;
jtulach@87
    30
import net.java.html.json.Property;
jtulach@87
    31
jtulach@87
    32
/** Model annotation generates class Data with 
jtulach@87
    33
 * one property for list of of numbers and read-only property
jtulach@87
    34
 * for ten of bars.
jtulach@87
    35
 */
jtulach@87
    36
@Model(className = "Data", properties = {
jtulach@87
    37
    @Property(name = "numbers", type = String.class)
jtulach@87
    38
})
jtulach@87
    39
final class DataModel {
jtulach@87
    40
    @ComputedProperty static java.util.List<Bar> bars(String numbers) {
jtulach@87
    41
        List<Bar> arr = new ArrayList<Bar>(10);
jtulach@87
    42
        for (int i = 0; i < 10; i++) {
jtulach@87
    43
            arr.add(new Bar(i, 0));
jtulach@87
    44
        }
jtulach@87
    45
        String[] words = numbers == null ? new String[0] : numbers.split(" ");
jtulach@87
    46
        for (String word : words) {
jtulach@87
    47
            try {
jtulach@87
    48
                double n = Double.parseDouble(word);
jtulach@87
    49
                if (n > 99) {
jtulach@87
    50
                    n = 99;
jtulach@87
    51
                } else if (n <= 0) {
jtulach@87
    52
                    n = 0;
jtulach@87
    53
                }
jtulach@87
    54
                Bar b = arr.get((int) (n / 10));
jtulach@87
    55
                b.setValue(b.getValue() + 1);
jtulach@87
    56
            }catch (NumberFormatException ex) {
jtulach@87
    57
                // ignore
jtulach@87
    58
            }
jtulach@87
    59
        }
jtulach@87
    60
        return arr;
jtulach@87
    61
    }
jtulach@87
    62
jtulach@87
    63
    @Model(className = "Bar", properties = {
jtulach@87
    64
        @Property(name = "index", type = int.class),
jtulach@87
    65
        @Property(name = "value", type = int.class)
jtulach@87
    66
    })
jtulach@87
    67
    static class BarModel {
jtulach@87
    68
        @ComputedProperty static String left(int index) {
jtulach@87
    69
            return (index * 50 + 20) + "px";
jtulach@87
    70
        }
jtulach@87
    71
        
jtulach@87
    72
        @ComputedProperty static String top(int value) {
jtulach@87
    73
            return (199 - value * 20) + "px";
jtulach@87
    74
        }
jtulach@87
    75
        
jtulach@87
    76
        @ComputedProperty static String height(int value) {
jtulach@87
    77
            return 20 * value + "px";
jtulach@87
    78
        }
jtulach@87
    79
    }
jtulach@87
    80
}