forked from huiyan-fe/mapv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterDrawer.js
More file actions
203 lines (171 loc) · 5.84 KB
/
Copy pathClusterDrawer.js
File metadata and controls
203 lines (171 loc) · 5.84 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
/**
* @file draw grad on the map
* @author Mofei Zhu <zhuwenlong@baidu.com>
*/
/* globals Drawer mercatorProjection BMap util */
var min;
var max;
function ClusterDrawer() {
Drawer.apply(this, arguments);
}
util.inherits(ClusterDrawer, Drawer);
ClusterDrawer.prototype.drawMap = function () {
// console.log('ClusterDrawer');
window.console.time('computerMapData');
var ctx = this.getCtx();
// TODO: ser workder
max = min = undefined;
var data = this.getLayer().getData();
var map = this.getMapv().getMap();
var zoom = map.getZoom();
var zoomUnit = this.zoomUnit = Math.pow(2, 18 - zoom);
// setMapStyle(map);
var param = this.formatParam();
// console.log(param)
// console.log(param.gridWidth)
var gridWidth = param.gridWidth;
var fillColors = param.colors;
var mercatorProjection = map.getMapType().getProjection();
var mcCenter = mercatorProjection.lngLatToPoint(map.getCenter());
var nwMcX = mcCenter.x - (map.getSize().width / 2) * zoomUnit;
var nwMc = new BMap.Pixel(nwMcX, mcCenter.y + (map.getSize().height / 2) * zoomUnit);
// 左上角墨卡托坐标
var gridStep = gridWidth / zoomUnit;
var startXMc = parseInt(nwMc.x / gridWidth, 10) * gridWidth;
var startX = (startXMc - nwMc.x) / zoomUnit;
var stockXA = [];
var stickXAIndex = 0;
while ((startX + stickXAIndex * gridStep) < map.getSize().width) {
var value = startX + stickXAIndex * gridStep;
stockXA.push(value.toFixed(2));
stickXAIndex++;
}
var startYMc = parseInt(nwMc.y / gridWidth, 10) * gridWidth + gridWidth;
var startY = (nwMc.y - startYMc) / zoomUnit;
var stockYA = [];
var stickYAIndex = 0;
while ((startY + stickYAIndex * gridStep) < map.getSize().height) {
value = startY + stickYAIndex * gridStep;
stockYA.push(value.toFixed(2));
stickYAIndex++;
}
var grids = {};
for (var i = 0; i < stockXA.length; i++) {
for (var j = 0; j < stockYA.length; j++) {
var name = stockXA[i] + '_' + stockYA[j];
grids[name] = 0;
}
}
for (var i = 0; i < data.length; i++) {
var x = data[i].px;
var y = data[i].py;
var val = parseInt(data[i].count, 10);
var isSmallX = x < stockXA[0];
var isSmallY = y < stockYA[0];
var isBigX = x > (Number(stockXA[stockXA.length - 1]) + Number(gridStep));
var isBigY = y > (Number(stockYA[stockYA.length - 1]) + Number(gridStep));
if (isSmallX || isSmallY || isBigX || isBigY) {
continue;
}
for (var j = 0; j < stockXA.length; j++) {
var dataX = Number(stockXA[j]);
if ((x >= dataX) && (x < dataX + gridStep)) {
for (var k = 0; k < stockYA.length; k++) {
var dataY = Number(stockYA[k]);
if ((y >= dataY) && (y < dataY + gridStep)) {
// grids[stockXA[j] + '_' + stockYA[k]] += 1;
grids[stockXA[j] + '_' + stockYA[k]] += val;
val = grids[stockXA[j] + '_' + stockYA[k]];
}
}
}
}
min = min || val;
max = max || val;
min = min > val ? val : min;
max = max < val ? val : max;
}
var step = (max - min + 1) / 10;
window.console.timeEnd('computerMapData');
window.console.time('drawMap');
for (var i in grids) {
var sp = i.split('_');
x = Number(sp[0]);
y = Number(sp[1]);
var v = (grids[i] - min) / step;
v = v < 0 ? 0 : v;
var color = fillColors[v | 0];
// if (grids[i] === 0) {
// ctx.fillStyle = 'rgba(255,255,255,0.1)';
// } else {
// ctx.fillStyle = 'rgba(' + color[0] + ',' + color[1] + ',' + color[2] + ',0.4)';
// }
var cx = x + gridStep / 2;
var cy = y + gridStep / 2;
ctx.fillStyle = '#fa8b2e';
// ctx.fillRect(x, y, 2, 2);
ctx.beginPath();
ctx.arc(cx, cy, v * 5, 0, 2 * Math.PI);
ctx.fill();
ctx.lineWidth = 8 * v / 10;
ctx.strokeStyle = '#fff';
ctx.stroke();
// if (this.drawOptions.showNum) {
ctx.save();
// ctx.fillStyle = 'black';
ctx.font = 30 * v / 10 + 'px serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
if (grids[i] !== 0) {
ctx.fillStyle = '#fff';
ctx.fillText(grids[i], cx, cy);
}
ctx.restore();
// }
}
window.console.timeEnd('drawMap');
};
// ClusterDrawer.prototype.drawDataRange = function (canvas, data, drawOptions) {
// };
/**
* format param
* @return {[type]} [description]
*/
ClusterDrawer.prototype.formatParam = function () {
// console.log('AAA')
var options = this.getDrawOptions();
// console.log(options)
var fillColors = this.fillColors = [
[73, 174, 34],
[119, 191, 26],
[160, 205, 18],
[202, 221, 10],
[248, 237, 1],
[225, 222, 3],
[254, 182, 10],
[254, 126, 19],
[254, 84, 27],
[253, 54, 32]
];
this.colorBar = {};
for (var i = 0; i < fillColors.length; i++) {
var pos = (i + 1) / fillColors.length;
var r = fillColors[i][0];
var g = fillColors[i][1];
var b = fillColors[i][2];
this.colorBar[pos] = 'rgb(' + r + ',' + g + ',' + b + ')';
}
var gridWidth = 60 || options.gridWidth || '50';
// console.log(gridWidth, '@@@@@@')
gridWidth = gridWidth + (options.gridUnit || 'px');
if (/px$/.test(gridWidth)) {
gridWidth = parseInt(gridWidth, 10) * this.zoomUnit;
} else {
gridWidth = parseInt(gridWidth, 10);
}
// console.log(gridWidth, options.gridWidth)
return {
gridWidth: gridWidth,
colors: fillColors
};
};