-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbambu.js
138 lines (109 loc) · 3.13 KB
/
bambu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
Bambu generates thematic carto.js strings
Currently under active development and is being used to style
vector tile json data in modestmaps via Carto.js & VECNIK.js.
MIT License - Copryright 2012
*/
function Bambu() {
// defaults
var colors = 'Reds',
type = 'quantile',
classes = 5,
style = '',
data = [],
id = '#',
field = 'null',
opacity = 1,
default_fill;
// returnable class
var bambu = function(){};
// regenerates the classification
bambu.classify = function(){
var vals = data.sort(function(a, b) {
return a - b;
});
switch (type){
case 'quantile':
var breaks = [];
for (var i = 0; i < classes; i++) {
breaks.push( vals[Math.ceil(i * (vals.length - 1) / classes)])
}
break;
case 'equal_interval':
var breaks = [],
range = vals[vals.length - 1] - vals[0];
for (var i=0; i < classes; i++) {
breaks.push(Math.floor(vals[0] + i * range / classes))
}
breaks[classes] = vals[vals.length - 1];
break;
}
var bins = [];
style = '#'+ id +' { ' + ((default_fill) ? 'polygon-fill: ' + default_fill + '; ' : ' polygon-opacity: '+ opacity+'; ');
for (var b = 0; b < breaks.length-1; b++){
var break_val = breaks[b];
bins.push('['+field+' > '+break_val+'] { polygon-fill: ' + rgb2hex(colorbrewer[colors][classes][b]) + '; }');
}
style = style + bins.join(' ') + '}';
return style;
}
bambu.id = function(x, gen){
if (!arguments.length) return id;
id = x;
if (gen) bambu.classify();
return bambu;
};
bambu.data = function(x, gen){
if (!arguments.length) return data;
data = x;
if (gen) bambu.classify();
return bambu;
};
bambu.field = function(x, gen){
if (!arguments.length) return field;
field = x;
if (gen) bambu.classify();
return bambu;
};
bambu.colors = function(x, gen){
if (!arguments.length) return colors;
colors = x;
if (gen) bambu.classify();
return bambu;
};
bambu.classes = function(x, gen){
if (!arguments.length) return classes;
classes = Math.min(x,9);
classes = Math.max(classes,3);
if (gen) bambu.classify();
return bambu;
};
bambu.type = function(x, gen){
if (!arguments.length) return type;
type = x;
if (gen) bambu.classify();
return bambu;
};
bambu.opacity = function(x, gen){
if (!arguments.length) return opacity;
opacity = x;
if (gen) bambu.classify();
return bambu;
};
bambu.style = function(){
return style;
};
function rgb2hex(rgb){
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}
return bambu;
}
if (typeof module !== 'undefined' && module.exports) {
//_ = require('underscore');
colorbrewer = require('./colorbrewer.js');
module.exports.Bambu = Bambu;
}