forked from huiyan-fe/mapv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataRange.js
More file actions
238 lines (196 loc) · 6.76 KB
/
DataRange.js
File metadata and controls
238 lines (196 loc) · 6.76 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @file 控制值域的类
* @author nikai (@胖嘟嘟的骨头, nikai@baidu.com)
*/
function DataRange(layer) {
Class.call(this);
this.initOptions({
min: 0,
max: 0,
});
this.set('layer', layer);
this.bindTo('data', layer)
this.bindTo('drawOptions', layer)
this.bindTo('drawType', layer)
var me = this;
}
util.inherits(DataRange, Class);
util.extend(DataRange.prototype, {
defaultGradient: {
'0.4': 'blue',
'0.6': 'cyan',
'0.7': 'lime',
'0.8': 'yellow',
'1.0': 'red'
},
colors: [
'rgba(17, 102, 252, 0.8)',
'rgba(52, 139, 251, 0.8)',
'rgba(110, 176, 253, 0.8)',
'rgba(255, 241, 193, 0.8)',
'rgba(255, 146, 149, 0.8)',
'rgba(253, 98, 104, 0.8)',
'rgba(255, 0, 0, 0.8)',
'rgba(255, 51, 61, 0.8)'
],
// 根据count值获取对应的大小,在bubble绘制中用到
getSize: function (count) {
var size = 1;
var splitList = this.splitList;
for (var i = 0; i < splitList.length; i++) {
if ((splitList[i].start === undefined
|| splitList[i].start !== undefined
&& count >= splitList[i].start)
&& (splitList[i].end === undefined
|| splitList[i].end !== undefined && count < splitList[i].end)) {
size = splitList[i].size;
break;
}
}
return size;
},
// 根据count值获取对应的颜色,在choropleth中使用
getColorByRange: function (count) {
var color = 'rgba(50, 50, 255, 1)';
var splitList = this.splitList;
for (var i = 0; i < splitList.length; i++) {
if ((splitList[i].start === undefined
|| splitList[i].start !== undefined
&& count >= splitList[i].start)
&& (splitList[i].end === undefined
|| splitList[i].end !== undefined && count < splitList[i].end)) {
color = splitList[i].color;
break;
}
}
return color;
},
data_changed: function () {
var data = this.get('data');
if (data && data.length > 0) {
this._min = data[0].count;
this._max = data[0].count;
for (var i = 0; i < data.length; i++) {
this._max = Math.max(this._max, data[i].count);
this._min = Math.min(this._min, data[i].count);
}
}
},
drawType_changed: function () {
this.update();
},
drawOptions_changed: function () {
this.update();
},
update: function () {
var drawOptions = this.get("drawOptions");
if (drawOptions && drawOptions.splitList) {
this.splitList = drawOptions.splitList;
} else {
this.generalSplitList();
}
if (this.get("layer").getDrawType() === 'category') {
if (drawOptions && drawOptions.splitList) {
this.categorySplitList = drawOptions.splitList;
} else {
this.generalCategorySplitList();
}
}
if (this.get("layer").getDrawType() === 'heatmap' || this.get("layer").getDrawType() === 'density' || this.get("layer").getDrawType() === 'intensity') {
this.generalGradient(drawOptions.gradient || this.defaultGradient);
}
this.draw();
},
draw: function () {
if (this.get("layer").getDataRangeControl()) {
this.get("layer").dataRangeControl.show();
}
if (this.get("layer").getDrawType() === 'bubble') {
this.get("layer").dataRangeControl.drawSizeSplit(this.splitList, this.get('drawOptions'));
} else if (this.get("layer").getDrawType() === 'category') {
this.get("layer").dataRangeControl.drawCategorySplit(this.categorySplitList, this.get('drawOptions'));
} else if (this.get("layer").getDrawType() === 'choropleth') {
this.get("layer").dataRangeControl.drawChoroplethSplit(this.splitList, this.get('drawOptions'));
} else {
this.get("layer").dataRangeControl.hide();
}
},
generalSplitList: function () {
var splitNum = Math.ceil((this._max - this._min) / 7);
var index = this._min;
this.splitList = [];
var radius = 1;
while (index < this._max) {
this.splitList.push({
start: index,
end: index + splitNum,
size: radius,
color: this.colors[radius - 1]
});
index += splitNum;
radius++;
}
},
generalCategorySplitList: function () {
var colors = ['rgba(255, 255, 0, 0.8)',
'rgba(253, 98, 104, 0.8)',
'rgba(255, 146, 149, 0.8)',
'rgba(255, 241, 193, 0.8)',
'rgba(110, 176, 253, 0.8)',
'rgba(52, 139, 251, 0.8)',
'rgba(17, 102, 252, 0.8)'];
var data = this.get("data");
this.categorySplitList = {};
var count = 0;
for (var i = 0; i < data.length; i++) {
if (this.categorySplitList[data[i].count] === undefined) {
this.categorySplitList[data[i].count] = colors[count];
count++;
}
if (count >= colors.length - 1) {
break;
}
}
this.categorySplitList['other'] = colors[colors.length - 1];
},
getCategoryColor: function (count) {
var splitList = this.categorySplitList;
var color = splitList['other'];
for (var i in splitList) {
if (count == i) {
color = splitList[i];
break;
}
}
return color;
},
generalGradient: function (grad) {
// create a 256x1 gradient that we'll use to turn a grayscale heatmap into a colored one
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var gradient = ctx.createLinearGradient(0, 0, 0, 256);
canvas.width = 1;
canvas.height = 256;
for (var i in grad) {
gradient.addColorStop(i, grad[i]);
}
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 1, 256);
this._grad = ctx.getImageData(0, 0, 1, 256).data;
},
getGradient: function () {
return this._grad;
},
getColorByGradient: function (count) {
var max = this.get("max") || 10;
var index = count / max;
if (index > 1) {
index = 1;
}
index *= 255;
index = parseInt(index, 10);
index *= 4;
var color = 'rgba(' + this._grad[index] + ', ' + this._grad[index + 1] + ', ' + this._grad[index + 2] + ',0.8)';
return color;
}
}); // end extend