Skip to content
Navigation Menu
{{ message }}
forked from woliqiang/iClient-for-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD3WindMap.js
More file actions
357 lines (339 loc) · 11.7 KB
/
Copy pathD3WindMap.js
File metadata and controls
357 lines (339 loc) · 11.7 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
(function () {
function A(option) {
var t = this;
t.div = null;
t.map = null;
t.data = [];
t.d3Layer = null;
t._svg = null;
t._tempData=null;
t._stepPointCount = 4;//每一步所走过的点
t._stepPointCountOffset = 5;//每一步所走过的点范围
t._minStepLength = 5;//每一段的长度
t._maxStepLength = 15;
t._stepStatus = {};
t._stepPointMargin =3;
t._stepNum =3;//有多少个小段
t._stepOffset = 30; //点的偏移量
t._maxSpeed = 500;
t._minSpeed = 200;
t._startOpacity = 0.1;
t._endOpacity = 0.9;
t._pointCountMultiple = 2;
t._mindelay = 30;
t._maxdelay = 300;
t._startColor=187;
t._endColor=94;
t._isStop = false;
t._isMove = false;
t._isAddData = false;
t.__isStop = null;
for (var key in option) {
t[key] = option[key];
}
t.createSVG();
t.d3Layer.events.on({moveend: function (evt) {
//console.log("moveend");
if(t._isMove){
t._isMove = false;
//t._isStop = t.__isStop;
//t.__isStop = null;
}
t._refresh(evt);
}});
// t.d3Layer.events.on({movestart: function (evt) {
// console.log("movestart");
// t._isStop = true;
// }});
t.d3Layer.events.on({move: function (evt) {
//console.log("move");
if(!t._isMove){
//t.__isStop = t._isStop;
//t._isStop = true;
t._isMove = true;
//t._svg.selectAll("path").style("display","none");
}
t._svg.selectAll("path").style("display","none");
}});
}
var P = A.prototype;
P.createSVG = function () {
var t = this;
var size = t.map.getSize();
t._svg = d3.select(t.div)
.append("svg")
.attr("width", size.w)
.attr("height", size.h);
//d3.select(t.div).html("<span id='fefe'></span>");
}
P.addData = function(data){
var t = this;
if(t._isAddData){
return;
}
else{
t._isAddData = true;
}
t._isStop = false;
t.data = data;
var defs = d3.select("svg")
.append("defs");
var linearGradient = defs.append("linearGradient").attr({
id:"orange_red",
x1:"0%",
y1:"0%",
x2:"100%",
y2:"0%"
});
linearGradient.append("stop").attr({
offset:"0%"
})
.style({
"stop-color":"rgb("+t._startColor+","+t._startColor+","+t._startColor+")",
"stop-opacity":1
});
linearGradient.append("stop").attr({
offset:"100%"
})
.style({
"stop-color":"rgb("+t._endColor+","+t._endColor+","+t._endColor+")",
"stop-opacity":1
});
var tempData = t._tempData = t.getPosition(data);
var bindData = [];
for(var i=0;i<data.length;i+=2){ //data.length
bindData.push(i);
}
t._svg.selectAll('path')
.data(bindData)
.enter()
.append("path")
.style("stroke", "url(#orange_red)")
.style("stroke-width", 1)
.style("fill", "none")
.style("opacity",0.9)
.call(function(selection){
t.lineAnimate(selection);
});
}
P.getPath = function(index){
var t = this;
var line = t._tempData[index];
var length = line.length;
var status = t._stepStatus[index];
var pointStrArray = [];
for(var i=0;i<status.length;i++){ //status.length
var statusi = status[i];
var start = statusi.start;
var pointCount =statusi.pointCount;
start = start+pointCount;
var end = statusi.end;
var stepLength = statusi.stepLength;
if(start>=length){
start = length;
}
if(start-end<=stepLength){
}
else{
end = end+pointCount;
}
var newStart = start;
var offsetX =statusi.offsetX;
var offsetY = statusi.offsetY;
pointStrArray.push("M"+offset(line[end],offsetX,offsetY).join(" "));
if(stepLength>=4){
for(var j=end+1;j<end+stepLength;j++){
if(j<start){
pointStrArray.push((j==0?"C":"")+offset(line[j],offsetX,offsetY).join(" "));
}
else{
pointStrArray.push((j==0?"C":"")+offset(line[start-1],offsetX,offsetY).join(" "));
}
}
}
else{
for(var j=end+1;j<end+stepLength;j++){
if(j<start){
pointStrArray.push("L"+offset(line[j],offsetX,offsetY).join(" "));
}
else{
pointStrArray.push("L"+offset(line[start-1],offsetX,offsetY).join(" "));
}
}
}
statusi.start = newStart;
statusi.end = end;
}
function offset(point,offsetX,offsetY){
return [point[0]+offsetX,point[1]+offsetY];
}
return pointStrArray.join(" ");// + " Z"
}
P.lineAnimate = function(selection) {
var t = this;
selection.style("opacity", t._startOpacity);
// window.setTimeout(function(selection){
// return function(){
//
// }
// }(selection),Math.round(t._mindelay+Math.random()*(t._maxdelay- t._mindelay)));
selection
.transition()
.duration(function(){
return Math.round(t._mindelay+Math.random()*(t._maxdelay- t._mindelay))
})
.attr("d",function(d){
var line = t._tempData[d];
var length = line.length;
var stepDistance = t._stepPointCount+ t._stepPointMargin;
var count = t._stepNum;
t._stepStatus[d] = [];
for(var i=0;i<count;i++){
var start = stepDistance*i;
if(start>=length){
break;
}
var stepLength = t._minStepLength+Math.round(Math.random()*(t._maxStepLength- t._minStepLength));
if(start+stepLength>=length){
stepLength = length-start;
}
for(var j=0;j<t._pointCountMultiple;j++){
t._stepStatus[d].push({
start:start,
end:start,
offsetX:Math.round(Math.random()*t._stepOffset-t._stepOffset/2),
offsetY:Math.round(Math.random()*t._stepOffset-t._stepOffset/2),
pointCount: t._stepPointCount+Math.round(Math.random()*(t._stepPointCountOffset-t._stepPointCount)),
stepLength:stepLength
});
}
}
t._stepStatus[d].speed = t._minSpeed + Math.round(Math.random()*(t._maxSpeed-t._minSpeed));
var path = t.getPath(d);
return path;
})
.call(function(selection){
t.step(selection);
});
}
P.step = function(selection){
//console.log("step");
var t = this;
selection.transition()
.ease('linear')
.duration(function(d){
return t._stepStatus[d].speed;
})
.attr("d",function(d){
var path = t.getPath(d);
return path;
})
.style("opacity",function(d){
var status = t._stepStatus[d];
var lastStart = status[status.length-1].start;
var line = t._tempData[d];
var length = line.length;
var opacity = t._startOpacity + (lastStart/length)*(t._endOpacity- t._startOpacity);
return opacity;
})
.each('end', function() {
if(t._isStop)return;
var obj = d3.select(this);
var d = obj.data()[0];
var status = t._stepStatus[d];
var lastStart = status[status.length-1].start;
var line = t._tempData[d];
var length = line.length;
if(lastStart>=length){
obj
.transition()
.duration(500)
.style('opacity', t._startOpacity)
.each('end',function(){
d3.select(this).call(function(selection){
t.lineAnimate(selection);
});
});
}
else{
obj.call(function(selection){
t.step(selection);
});
}
});
}
P.getPosition = function(datas){
var tempDatas = [],t = this;
if(datas){
for(var i=0;i<datas.length;i++){
var line = datas[i];
var tempLine = [];
for(var j=0;j<line.length;j++){
var point = line[j];
var tempPoint = this.d3Layer.getLayerPxFromLonLat(new SuperMap.LonLat(point[0],point[1]));
tempLine.push([tempPoint.x,tempPoint.y]);
}
tempDatas.push(tempLine);
}
}
return tempDatas;
}
P._refresh = function(evt){
var t = this;
var tempData = t._tempData = t.getPosition(t.data);
if(tempData){
if(t._isStop){
t._svg.selectAll("path").attr("d",function(d){
var path = t.getPath(d);
return path;
});
t._svg.selectAll("path").style("display","block");
}
else{
//if(evt.zoomChanged){
t._svg.selectAll("path").style("display","none");
//}
t._svg.selectAll("path").attr("d",function(d){
var path = t.getPath(d);
return path;
});
t._isStop = true;
setTimeout(function(){
t._isStop = false;
t._svg.selectAll("path")
.attr("d",function(d){
var path = t.getPath(d);
return path;
})
.style("display","block")
.call(function(selection){
t.step(selection);
});
//if(evt.zoomChanged){
t._svg.selectAll("path").style("display","block");
//}
},300);
}
}
}
P.clear = function(){
var t = this;
t._isStop = true;
t._svg.selectAll("path").remove();
t._svg.selectAll("defs").remove();
t._isAddData = false;
}
P.stop = function(){
this._isStop = true;
}
P.run = function(){
var t = this;
if(t._isStop){
t._isStop = false;
t._svg.selectAll("path").call(function(selection){
t.step(selection);
});
}
}
window.D3WindMap = A;
})()
You can’t perform that action at this time.
