~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/gis/mapfish/core/GeoStat/Choropleth.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
6
 * MapFish Client is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * MapFish Client is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/**
 
21
 * @requires core/GeoStat.js
 
22
 * @requires core/Color.js
 
23
 */
 
24
 
 
25
/**
 
26
 * Class: mapfish.GeoStat.Choropleth
 
27
 * Use this class to create choropleths on a map.
 
28
 *
 
29
 * Inherits from:
 
30
 * - <mapfish.GeoStat>
 
31
 */
 
32
mapfish.GeoStat.Choropleth = OpenLayers.Class(mapfish.GeoStat, {
 
33
 
 
34
    /**
 
35
     * APIProperty: colors
 
36
     * {Array(<mapfish.Color>}} Array of 2 colors to be applied to features
 
37
     *     We should use styles instead
 
38
     */
 
39
    colors: [
 
40
        new mapfish.ColorRgb(120, 120, 0),
 
41
        new mapfish.ColorRgb(255, 0, 0)
 
42
    ],
 
43
 
 
44
    /**
 
45
     * APIProperty: method
 
46
     * {Integer} Specifies the distribution method to use. Possible
 
47
     *      values are:
 
48
     *      mapfish.GeoStat.Distribution.CLASSIFY_BY_QUANTILS and
 
49
     *      mapfish.GeoStat.Distribution.CLASSIFY_BY_EQUAL_INTERVALS
 
50
     */
 
51
    method: mapfish.GeoStat.Distribution.CLASSIFY_BY_QUANTILS,
 
52
 
 
53
    /**
 
54
     * APIProperty: numClasses
 
55
     * {Integer} Number of classes
 
56
     */
 
57
    numClasses: 5,
 
58
 
 
59
    /**
 
60
     * Property: defaultSymbolizer
 
61
     * {Object} Overrides defaultSymbolizer in the parent class
 
62
     */
 
63
    defaultSymbolizer: {'fillOpacity': 1},
 
64
 
 
65
    /**
 
66
     * Property: classification
 
67
     * {<mapfish.GeoStat.Classification>} Defines the different classification to use
 
68
     */
 
69
    classification: null,
 
70
 
 
71
    /**
 
72
     * Property: colorInterpolation
 
73
     * {Array({<mapfish.Color>})} Array of {<mapfish.Color} resulting from the
 
74
     *      RGB color interpolation
 
75
     */
 
76
    colorInterpolation: null,
 
77
 
 
78
    /**
 
79
     * Constructor: mapfish.GeoStat.Choropleth
 
80
     *
 
81
     * Parameters:
 
82
     * map - {<OpenLayers.Map>} OpenLayers map object
 
83
     * options - {Object} Hashtable of extra options
 
84
     */
 
85
    initialize: function(map, options) {
 
86
        mapfish.GeoStat.prototype.initialize.apply(this, arguments);
 
87
    },
 
88
 
 
89
    /**
 
90
     * APIMethod: updateOptions
 
91
     *      Method used to update the properties method, indicator,
 
92
     *      numClasses and colors.
 
93
     *
 
94
     * Parameters:
 
95
     * newOptions - {Object} options object
 
96
     */
 
97
    updateOptions: function(newOptions) {
 
98
        var oldOptions = OpenLayers.Util.extend({}, this.options);
 
99
        this.addOptions(newOptions);
 
100
        if (newOptions) {
 
101
            if (newOptions.method != oldOptions.method ||
 
102
                newOptions.indicator != oldOptions.indicator ||
 
103
                newOptions.numClasses != oldOptions.numClasses) {
 
104
                this.setClassification();
 
105
            } else if (newOptions.colors && (
 
106
                       !newOptions.colors[0].equals(oldOptions.colors[0]) ||
 
107
                       !newOptions.colors[1].equals(oldOptions.colors[1]))) {
 
108
                this.createColorInterpolation();
 
109
            }
 
110
        }
 
111
    },
 
112
 
 
113
    /**
 
114
     * Method: createColorInterpolation
 
115
     *      Generates color interpolation in regard to classification.
 
116
     */
 
117
    createColorInterpolation: function() {
 
118
        var initialColors = this.colors;
 
119
        var numColors = this.classification.bins.length;
 
120
        this.colorInterpolation =
 
121
            mapfish.ColorRgb.getColorsArrayByRgbInterpolation(
 
122
                initialColors[0], initialColors[1], numColors
 
123
            );
 
124
    },
 
125
 
 
126
    /**
 
127
     * Method: setClassification
 
128
     *      Creates a classification with the features.
 
129
     */
 
130
    setClassification: function() {
 
131
        var values = [];
 
132
        var features = this.layer.features;
 
133
        for (var i = 0; i < features.length; i++) {
 
134
            values.push(features[i].attributes[this.indicator]);
 
135
        }
 
136
 
 
137
        var distOptions = {
 
138
            'labelGenerator' : this.options.labelGenerator
 
139
        };
 
140
        var dist = new mapfish.GeoStat.Distribution(values, distOptions);
 
141
        this.classification = dist.classify(
 
142
            this.method,
 
143
            this.numClasses,
 
144
            null
 
145
        );
 
146
        this.createColorInterpolation();
 
147
    },
 
148
 
 
149
    /**
 
150
     * APIMethod: applyClassification
 
151
     *      Style the features based on the classification
 
152
     *
 
153
     * Parameters:
 
154
     * options - {Object}
 
155
     */
 
156
    applyClassification: function(options) {
 
157
        this.updateOptions(options);
 
158
        var boundsArray = this.classification.getBoundsArray();
 
159
        var rules = new Array(boundsArray.length - 1);
 
160
        for (var i = 0; i < boundsArray.length -1; i++) {
 
161
            var rule = new OpenLayers.Rule({
 
162
                symbolizer: {fillColor: this.colorInterpolation[i].toHexString()},
 
163
                filter: new OpenLayers.Filter.Comparison({
 
164
                    type: OpenLayers.Filter.Comparison.BETWEEN,
 
165
                    property: this.indicator,
 
166
                    lowerBoundary: boundsArray[i],
 
167
                    upperBoundary: boundsArray[i + 1]
 
168
                })
 
169
            });
 
170
            rules[i] = rule;
 
171
        }
 
172
        this.extendStyle(rules);
 
173
        mapfish.GeoStat.prototype.applyClassification.apply(this, arguments);
 
174
    },
 
175
 
 
176
    /**
 
177
     * Method: updateLegend
 
178
     *    Update the legendDiv content with new bins label
 
179
     */
 
180
    updateLegend: function() {
 
181
        if (!this.legendDiv) {
 
182
            return;
 
183
        }
 
184
 
 
185
        // TODO use css classes instead
 
186
        this.legendDiv.update("");
 
187
        for (var i = 0; i < this.classification.bins.length; i++) {
 
188
            var element = document.createElement("div");
 
189
            element.style.backgroundColor = this.colorInterpolation[i].toHexString();
 
190
            element.style.width = "30px";
 
191
            element.style.height = "15px";
 
192
            element.style.cssFloat = "left";
 
193
            element.style.marginRight = "10px";
 
194
            this.legendDiv.appendChild(element);
 
195
 
 
196
            element = document.createElement("div");
 
197
            element.innerHTML = this.classification.bins[i].label;
 
198
            this.legendDiv.appendChild(element);
 
199
 
 
200
            element = document.createElement("div");
 
201
            element.style.clear = "left";
 
202
            this.legendDiv.appendChild(element);
 
203
        }
 
204
    },
 
205
 
 
206
    CLASS_NAME: "mapfish.GeoStat.Choropleth"
 
207
});