2
* Copyright (C) 2007 Camptocamp
4
* This file is part of MapFish Client
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.
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.
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/>.
21
* @requires core/GeoStat.js
22
* @requires core/Color.js
26
* Class: mapfish.GeoStat.Choropleth
27
* Use this class to create choropleths on a map.
32
mapfish.GeoStat.Choropleth = OpenLayers.Class(mapfish.GeoStat, {
36
* {Array(<mapfish.Color>}} Array of 2 colors to be applied to features
37
* We should use styles instead
40
new mapfish.ColorRgb(120, 120, 0),
41
new mapfish.ColorRgb(255, 0, 0)
46
* {Integer} Specifies the distribution method to use. Possible
48
* mapfish.GeoStat.Distribution.CLASSIFY_BY_QUANTILS and
49
* mapfish.GeoStat.Distribution.CLASSIFY_BY_EQUAL_INTERVALS
51
method: mapfish.GeoStat.Distribution.CLASSIFY_BY_QUANTILS,
54
* APIProperty: numClasses
55
* {Integer} Number of classes
60
* Property: defaultSymbolizer
61
* {Object} Overrides defaultSymbolizer in the parent class
63
defaultSymbolizer: {'fillOpacity': 1},
66
* Property: classification
67
* {<mapfish.GeoStat.Classification>} Defines the different classification to use
72
* Property: colorInterpolation
73
* {Array({<mapfish.Color>})} Array of {<mapfish.Color} resulting from the
74
* RGB color interpolation
76
colorInterpolation: null,
79
* Constructor: mapfish.GeoStat.Choropleth
82
* map - {<OpenLayers.Map>} OpenLayers map object
83
* options - {Object} Hashtable of extra options
85
initialize: function(map, options) {
86
mapfish.GeoStat.prototype.initialize.apply(this, arguments);
90
* APIMethod: updateOptions
91
* Method used to update the properties method, indicator,
92
* numClasses and colors.
95
* newOptions - {Object} options object
97
updateOptions: function(newOptions) {
98
var oldOptions = OpenLayers.Util.extend({}, this.options);
99
this.addOptions(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();
114
* Method: createColorInterpolation
115
* Generates color interpolation in regard to classification.
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
127
* Method: setClassification
128
* Creates a classification with the features.
130
setClassification: function() {
132
var features = this.layer.features;
133
for (var i = 0; i < features.length; i++) {
134
values.push(features[i].attributes[this.indicator]);
138
'labelGenerator' : this.options.labelGenerator
140
var dist = new mapfish.GeoStat.Distribution(values, distOptions);
141
this.classification = dist.classify(
146
this.createColorInterpolation();
150
* APIMethod: applyClassification
151
* Style the features based on the classification
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]
172
this.extendStyle(rules);
173
mapfish.GeoStat.prototype.applyClassification.apply(this, arguments);
177
* Method: updateLegend
178
* Update the legendDiv content with new bins label
180
updateLegend: function() {
181
if (!this.legendDiv) {
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);
196
element = document.createElement("div");
197
element.innerHTML = this.classification.bins[i].label;
198
this.legendDiv.appendChild(element);
200
element = document.createElement("div");
201
element.style.clear = "left";
202
this.legendDiv.appendChild(element);
206
CLASS_NAME: "mapfish.GeoStat.Choropleth"