D3WindMap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. (function () {
  2. function A(option) {
  3. var t = this;
  4. t.div = null;
  5. t.map = null;
  6. t.data = [];
  7. t.d3Layer = null;
  8. t._svg = null;
  9. t._tempData=null;
  10. t._stepPointCount = 4;//每一步所走过的点
  11. t._stepPointCountOffset = 5;//每一步所走过的点范围
  12. t._minStepLength = 5;//每一段的长度
  13. t._maxStepLength = 15;
  14. t._stepStatus = {};
  15. t._stepPointMargin =3;
  16. t._stepNum =3;//有多少个小段
  17. t._stepOffset = 30; //点的偏移量
  18. t._maxSpeed = 500;
  19. t._minSpeed = 200;
  20. t._startOpacity = 0.1;
  21. t._endOpacity = 0.9;
  22. t._pointCountMultiple = 2;
  23. t._mindelay = 30;
  24. t._maxdelay = 300;
  25. t._startColor=187;
  26. t._endColor=94;
  27. t._isStop = false;
  28. t._isMove = false;
  29. t._isAddData = false;
  30. t.__isStop = null;
  31. for (var key in option) {
  32. t[key] = option[key];
  33. }
  34. t.createSVG();
  35. t.d3Layer.events.on({moveend: function (evt) {
  36. //console.log("moveend");
  37. if(t._isMove){
  38. t._isMove = false;
  39. //t._isStop = t.__isStop;
  40. //t.__isStop = null;
  41. }
  42. t._refresh(evt);
  43. }});
  44. // t.d3Layer.events.on({movestart: function (evt) {
  45. // console.log("movestart");
  46. // t._isStop = true;
  47. // }});
  48. t.d3Layer.events.on({move: function (evt) {
  49. //console.log("move");
  50. if(!t._isMove){
  51. //t.__isStop = t._isStop;
  52. //t._isStop = true;
  53. t._isMove = true;
  54. //t._svg.selectAll("path").style("display","none");
  55. }
  56. t._svg.selectAll("path").style("display","none");
  57. }});
  58. }
  59. var P = A.prototype;
  60. P.createSVG = function () {
  61. var t = this;
  62. var size = t.map.getSize();
  63. t._svg = d3.select(t.div)
  64. .append("svg")
  65. .attr("width", size.w)
  66. .attr("height", size.h);
  67. //d3.select(t.div).html("<span id='fefe'></span>");
  68. }
  69. P.addData = function(data){
  70. var t = this;
  71. if(t._isAddData){
  72. return;
  73. }
  74. else{
  75. t._isAddData = true;
  76. }
  77. t._isStop = false;
  78. t.data = data;
  79. var defs = d3.select("svg")
  80. .append("defs");
  81. var linearGradient = defs.append("linearGradient").attr({
  82. id:"orange_red",
  83. x1:"0%",
  84. y1:"0%",
  85. x2:"100%",
  86. y2:"0%"
  87. });
  88. linearGradient.append("stop").attr({
  89. offset:"0%"
  90. })
  91. .style({
  92. "stop-color":"rgb("+t._startColor+","+t._startColor+","+t._startColor+")",
  93. "stop-opacity":1
  94. });
  95. linearGradient.append("stop").attr({
  96. offset:"100%"
  97. })
  98. .style({
  99. "stop-color":"rgb("+t._endColor+","+t._endColor+","+t._endColor+")",
  100. "stop-opacity":1
  101. });
  102. var tempData = t._tempData = t.getPosition(data);
  103. var bindData = [];
  104. for(var i=0;i<data.length;i+=2){ //data.length
  105. bindData.push(i);
  106. }
  107. t._svg.selectAll('path')
  108. .data(bindData)
  109. .enter()
  110. .append("path")
  111. .style("stroke", "url(#orange_red)")
  112. .style("stroke-width", 1)
  113. .style("fill", "none")
  114. .style("opacity",0.9)
  115. .call(function(selection){
  116. t.lineAnimate(selection);
  117. });
  118. }
  119. P.getPath = function(index){
  120. var t = this;
  121. var line = t._tempData[index];
  122. var length = line.length;
  123. var status = t._stepStatus[index];
  124. var pointStrArray = [];
  125. for(var i=0;i<status.length;i++){ //status.length
  126. var statusi = status[i];
  127. var start = statusi.start;
  128. var pointCount =statusi.pointCount;
  129. start = start+pointCount;
  130. var end = statusi.end;
  131. var stepLength = statusi.stepLength;
  132. if(start>=length){
  133. start = length;
  134. }
  135. if(start-end<=stepLength){
  136. }
  137. else{
  138. end = end+pointCount;
  139. }
  140. var newStart = start;
  141. var offsetX =statusi.offsetX;
  142. var offsetY = statusi.offsetY;
  143. pointStrArray.push("M"+offset(line[end],offsetX,offsetY).join(" "));
  144. if(stepLength>=4){
  145. for(var j=end+1;j<end+stepLength;j++){
  146. if(j<start){
  147. pointStrArray.push((j==0?"C":"")+offset(line[j],offsetX,offsetY).join(" "));
  148. }
  149. else{
  150. pointStrArray.push((j==0?"C":"")+offset(line[start-1],offsetX,offsetY).join(" "));
  151. }
  152. }
  153. }
  154. else{
  155. for(var j=end+1;j<end+stepLength;j++){
  156. if(j<start){
  157. pointStrArray.push("L"+offset(line[j],offsetX,offsetY).join(" "));
  158. }
  159. else{
  160. pointStrArray.push("L"+offset(line[start-1],offsetX,offsetY).join(" "));
  161. }
  162. }
  163. }
  164. statusi.start = newStart;
  165. statusi.end = end;
  166. }
  167. function offset(point,offsetX,offsetY){
  168. return [point[0]+offsetX,point[1]+offsetY];
  169. }
  170. return pointStrArray.join(" ");// + " Z"
  171. }
  172. P.lineAnimate = function(selection) {
  173. var t = this;
  174. selection.style("opacity", t._startOpacity);
  175. // window.setTimeout(function(selection){
  176. // return function(){
  177. //
  178. // }
  179. // }(selection),Math.round(t._mindelay+Math.random()*(t._maxdelay- t._mindelay)));
  180. selection
  181. .transition()
  182. .duration(function(){
  183. return Math.round(t._mindelay+Math.random()*(t._maxdelay- t._mindelay))
  184. })
  185. .attr("d",function(d){
  186. var line = t._tempData[d];
  187. var length = line.length;
  188. var stepDistance = t._stepPointCount+ t._stepPointMargin;
  189. var count = t._stepNum;
  190. t._stepStatus[d] = [];
  191. for(var i=0;i<count;i++){
  192. var start = stepDistance*i;
  193. if(start>=length){
  194. break;
  195. }
  196. var stepLength = t._minStepLength+Math.round(Math.random()*(t._maxStepLength- t._minStepLength));
  197. if(start+stepLength>=length){
  198. stepLength = length-start;
  199. }
  200. for(var j=0;j<t._pointCountMultiple;j++){
  201. t._stepStatus[d].push({
  202. start:start,
  203. end:start,
  204. offsetX:Math.round(Math.random()*t._stepOffset-t._stepOffset/2),
  205. offsetY:Math.round(Math.random()*t._stepOffset-t._stepOffset/2),
  206. pointCount: t._stepPointCount+Math.round(Math.random()*(t._stepPointCountOffset-t._stepPointCount)),
  207. stepLength:stepLength
  208. });
  209. }
  210. }
  211. t._stepStatus[d].speed = t._minSpeed + Math.round(Math.random()*(t._maxSpeed-t._minSpeed));
  212. var path = t.getPath(d);
  213. return path;
  214. })
  215. .call(function(selection){
  216. t.step(selection);
  217. });
  218. }
  219. P.step = function(selection){
  220. //console.log("step");
  221. var t = this;
  222. selection.transition()
  223. .ease('linear')
  224. .duration(function(d){
  225. return t._stepStatus[d].speed;
  226. })
  227. .attr("d",function(d){
  228. var path = t.getPath(d);
  229. return path;
  230. })
  231. .style("opacity",function(d){
  232. var status = t._stepStatus[d];
  233. var lastStart = status[status.length-1].start;
  234. var line = t._tempData[d];
  235. var length = line.length;
  236. var opacity = t._startOpacity + (lastStart/length)*(t._endOpacity- t._startOpacity);
  237. return opacity;
  238. })
  239. .each('end', function() {
  240. if(t._isStop)return;
  241. var obj = d3.select(this);
  242. var d = obj.data()[0];
  243. var status = t._stepStatus[d];
  244. var lastStart = status[status.length-1].start;
  245. var line = t._tempData[d];
  246. var length = line.length;
  247. if(lastStart>=length){
  248. obj
  249. .transition()
  250. .duration(500)
  251. .style('opacity', t._startOpacity)
  252. .each('end',function(){
  253. d3.select(this).call(function(selection){
  254. t.lineAnimate(selection);
  255. });
  256. });
  257. }
  258. else{
  259. obj.call(function(selection){
  260. t.step(selection);
  261. });
  262. }
  263. });
  264. }
  265. P.getPosition = function(datas){
  266. var tempDatas = [],t = this;
  267. if(datas){
  268. for(var i=0;i<datas.length;i++){
  269. var line = datas[i];
  270. var tempLine = [];
  271. for(var j=0;j<line.length;j++){
  272. var point = line[j];
  273. var tempPoint = this.d3Layer.getLayerPxFromLonLat(new SuperMap.LonLat(point[0],point[1]));
  274. tempLine.push([tempPoint.x,tempPoint.y]);
  275. }
  276. tempDatas.push(tempLine);
  277. }
  278. }
  279. return tempDatas;
  280. }
  281. P._refresh = function(evt){
  282. var t = this;
  283. var tempData = t._tempData = t.getPosition(t.data);
  284. if(tempData){
  285. if(t._isStop){
  286. t._svg.selectAll("path").attr("d",function(d){
  287. var path = t.getPath(d);
  288. return path;
  289. });
  290. t._svg.selectAll("path").style("display","block");
  291. }
  292. else{
  293. //if(evt.zoomChanged){
  294. t._svg.selectAll("path").style("display","none");
  295. //}
  296. t._svg.selectAll("path").attr("d",function(d){
  297. var path = t.getPath(d);
  298. return path;
  299. });
  300. t._isStop = true;
  301. setTimeout(function(){
  302. t._isStop = false;
  303. t._svg.selectAll("path")
  304. .attr("d",function(d){
  305. var path = t.getPath(d);
  306. return path;
  307. })
  308. .style("display","block")
  309. .call(function(selection){
  310. t.step(selection);
  311. });
  312. //if(evt.zoomChanged){
  313. t._svg.selectAll("path").style("display","block");
  314. //}
  315. },300);
  316. }
  317. }
  318. }
  319. P.clear = function(){
  320. var t = this;
  321. t._isStop = true;
  322. t._svg.selectAll("path").remove();
  323. t._svg.selectAll("defs").remove();
  324. t._isAddData = false;
  325. }
  326. P.stop = function(){
  327. this._isStop = true;
  328. }
  329. P.run = function(){
  330. var t = this;
  331. if(t._isStop){
  332. t._isStop = false;
  333. t._svg.selectAll("path").call(function(selection){
  334. t.step(selection);
  335. });
  336. }
  337. }
  338. window.D3WindMap = A;
  339. })()