others_d3_zoomablePacking.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title data-i18n="resources.title_zoomablePacking"></title>
  9. <style>
  10. #box {
  11. background-color: #ffffff;
  12. font-family: Arial;
  13. border: 1px solid #379082;
  14. border-radius: 10px;
  15. padding: 10px 10px;
  16. width: 310px;
  17. }
  18. #box > div {
  19. overflow: hidden;
  20. overflow: hidden;;
  21. text-align: center;
  22. opacity: 0.7;
  23. }
  24. #popwin_contentDiv {
  25. overflow: hidden;
  26. }
  27. .node {
  28. cursor: pointer;
  29. }
  30. .node:hover {
  31. stroke: #000;
  32. stroke-width: 1.5px;
  33. }
  34. .node--leaf {
  35. fill: white;
  36. }
  37. .label {
  38. font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
  39. text-anchor: middle;
  40. text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
  41. }
  42. .label,
  43. .node--root,
  44. .node--leaf {
  45. pointer-events: none;
  46. }
  47. </style>
  48. </head>
  49. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  50. <div id="map" style="margin:0 auto;width: 100%;height: 100%; position: relative;"></div>
  51. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  52. <script type="text/javascript" exclude="iclient-classic" include="d3" src="../../dist/classic/include-classic.js"></script>
  53. <script>
  54. var worldCountryInfo = [
  55. ['新西兰', '29.174235956394114', '174.8072164108394', '-41.33484005165742'],
  56. ['澳大利亚', '695.5399346214508', '149.04200743036952', '-35.350002057001724'],
  57. ['拉脱维亚', '9.516091977409815', '24.04999831855966', '56.88000003428641'],
  58. ['爱沙尼亚', '7.107818374643102', '24.75000031924904', '59.416667932981184'],
  59. ['乌克兰', '73.48295930493987', '30.50210739128974', '50.44815906914981'],
  60. ['白俄罗斯', '28.15780526434537', '27.575559329931593', '53.89993803274737'],
  61. ['俄罗斯', '2931.554593770361', '37.70000133930645', '55.74999602682895'],
  62. ['意大利', '33.05506087308913', '12.519999338142895', '41.87999704393327'],
  63. ['西班牙', '53.610781509409136', '-3.690971698244965', '40.442220073506434'],
  64. ['土耳其', '81.19930219469461', '32.85299737691997', '39.92900394684892'],
  65. ['葡萄牙', '9.641563211488574', '-9.12999560218097', '38.72999705969055'],
  66. ['保加利亚', '12.210432929783565', '23.33187039624326', '42.70726805874146'],
  67. ['罗马尼亚', '27.498306337311078', '26.12296842782999', '44.43048001493676'],
  68. ['斯洛文尼亚', '2.376986789131479', '14.639607376076782', '46.068299920805856'],
  69. ['匈牙利', '11.033293646723905', '19.094004336066007', '47.51499596400467'],
  70. ['捷克', '9.824207258823662', '14.456997316697482', '50.10599694699265'],
  71. ['波兰', '40.91073109364697', '21.011877368143843', '52.24494605306201'],
  72. ['斯洛伐克', '5.978073764857982', '17.129997438550106', '48.14999995502585'],
  73. ['德国', '45.87205763302518', '13.327569357857186', '52.51626892335375'],
  74. ['瑞士', '4.889274569378358', '7.445997303541986', '46.94799599331836'],
  75. ['丹麦', '6.142943083979844', '12.549996395535771', '55.71999896943609'],
  76. ['芬兰', '62.65498121370547', '24.97670134829545', '60.196418991223055'],
  77. ['挪威', '59.93964994602538', '10.711998354074979', '59.9380020260172'],
  78. ['比利时', '3.8955219766376104', '4.367997439032962', '50.83700396604216'],
  79. ['英国', '33.740018575668834', '-0.17800167655576615', '51.48791093669834'],
  80. ['法国', '64.34832264506642', '2.4329973446941153', '48.8819970628075'],
  81. ['荷兰', '4.677572945859083', '4.894839324693237', '52.373042981927085'],
  82. ['阿根廷', '278.3089524010138', '-58.44983369684459', '-34.622496010243125'],
  83. ['巴西', '707.0506786671417', '-47.8977476573595', '-15.792110943058866'],
  84. ['古巴', '9.52728555372596', '-82.41645556215907', '23.048955006622577'],
  85. ['牙买加', '0.9424347225140082', '-76.79951967992427', '17.966925028753693'],
  86. ['多米尼加', '4.147566520878172', '-69.8990036733499', '18.48899701342542'],
  87. ['巴拿马', '6.088299470397033', '-79.51707859374812', '9.00287992114162'],
  88. ['墨西哥', '174.11766745569184', '-99.1275746461327', '19.4270490779827'],
  89. ['加拿大', '1694.0251615091693', '-75.65100268456125', '45.374003962164295'],
  90. ['美国', '1116.6706382613484', '-76.95383368844213', '38.89090807427654'],
  91. ['津巴布韦', '33.46313087468934', '31.020003395825285', '-17.829999013060934'],
  92. ['喀麦隆', '37.972713199220834', '11.513997366407239', '3.864996064152848'],
  93. ['肯尼亚', '47.3068528143186', '36.803997348645254', '-1.269998944599834'],
  94. ['埃塞俄比亚', '92.7227504971338', '38.70000039130997', '9.029997066415987'],
  95. ['突尼斯', '15.188934756238268', '10.16600438261787', '36.81900003913003'],
  96. ['乌兹别克斯坦', '48.370006065262714', '69.34986930626746', '41.247936014304685'],
  97. ['哈萨克斯坦', '343.1600648165404', '71.43200141832125', '51.189002933344426'],
  98. ['阿塞拜疆', ' 0.086600', '49.815999303821741', '40.323996065224122'],
  99. ['格鲁吉亚', '7.623239405831555', '44.78312740526016', '41.7218130345648'],
  100. ['伊朗', '161.76988961015377', '51.44799633617973', '35.774001015720245'],
  101. ['印度', '272.3191563077348', '77.21675130049576', '28.568726992960762'],
  102. ['印度尼西亚', '153.00252996300375', '106.80000339972676', '-6.166665049519089'],
  103. ['泰国', '43.14321767192996', '100.51667144183207', '13.750001918531567'],
  104. ['日本', '37.94131046523398', '139.80900636524117', '35.683002048058015'],
  105. ['蒙古', '184.43204108648933', '106.91199943050063', '47.92899603190966'],
  106. ['韩国', '9.766875186847756', '126.93524432884405', '37.542357079588854'],
  107. ['朝鲜', '12.918861162514077', '125.75700034541975', '39.029004022272574'],
  108. ['中华人民共和国', '960.4537031350538', '116.38803641666053', '39.90618904574261']
  109. ];
  110. var map, baseLayer, d3Layer, popup,
  111. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  112. url = host + "/iserver/services/map-world/rest/maps/World";
  113. function init() {
  114. /*
  115. * 不支持 SVG 的浏览器不能运行该范例
  116. * android 设备也不能运行该范例*/
  117. var broz = SuperMap.Util.getBrowser();
  118. if (!hasSVG()) {
  119. widgets.alert.showAlert(resources.msg_supportSVG, false);
  120. return;
  121. }
  122. else if (broz.device === 'android' || broz.device === 'apple') {
  123. widgets.alert.showAlert(resources.msg_supportEquipment, false);
  124. return;
  125. }
  126. map = new SuperMap.Map("map", {
  127. controls: [
  128. new SuperMap.Control.LayerSwitcher(),
  129. new SuperMap.Control.ScaleLine(),
  130. new SuperMap.Control.Zoom(),
  131. new SuperMap.Control.Navigation({
  132. dragPanOptions: {
  133. enableKinetic: true
  134. }
  135. })
  136. ]
  137. });
  138. baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("Word", url, {
  139. transparent: true,
  140. cacheEnabled: true
  141. }, {maxResolution: "auto"});
  142. baseLayer.events.on({"layerInitialized": addLayer});
  143. }
  144. window.onload = function () {
  145. init();
  146. };
  147. function hasSVG() {
  148. var doc = document;
  149. var SVG_NS = 'http://www.w3.org/2000/svg';
  150. return !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect;
  151. }
  152. function addLayer() {
  153. //创建Elements的实例
  154. d3Layer = new SuperMap.Layer.Elements("D3Layer");
  155. map.addLayers([baseLayer, d3Layer]);
  156. map.setCenter(new SuperMap.LonLat(0, 0), 0);
  157. //获得Elements div
  158. var layerDiv = d3Layer.getDiv();
  159. var contentHTML = "<div id = 'box'>";
  160. contentHTML += "<div>" + resources.text_picker + "</div>";
  161. contentHTML += "<div id= 'vis'></div>";
  162. contentHTML += "</div>";
  163. layerDiv.innerHTML = contentHTML;
  164. //添加D3 拾取器
  165. addZoomableCirclePacking();
  166. }
  167. function addZoomableCirclePacking() {
  168. var margin = 10,
  169. diameter = 300;
  170. var color = d3.scale.linear()
  171. .domain([-1, 5])
  172. .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
  173. .interpolate(d3.interpolateHcl);
  174. var pack = d3.layout.pack()
  175. .padding(2)
  176. .size([diameter - margin, diameter - margin])
  177. .value(function (d) {
  178. return d.size;
  179. })
  180. var svg = d3.select("#vis").append("svg")
  181. .attr("width", diameter)
  182. .attr("height", diameter)
  183. .append("g")
  184. .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
  185. d3.json("../data/goldMedalData.json", function (error, root) {
  186. if (error) return console.error(error);
  187. var focus = root,
  188. nodes = pack.nodes(root),
  189. view;
  190. var circle = svg.selectAll("circle")
  191. .data(nodes)
  192. .enter().append("circle")
  193. .attr("class", function (d) {
  194. return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root";
  195. })
  196. .style("fill", function (d) {
  197. return d.children ? color(d.depth) : null;
  198. })
  199. .on("click", function (d) {
  200. if (focus !== d) zoom(d), d3.event.stopPropagation();
  201. })
  202. .on("mouseup", function (d) {
  203. showGoldMedal(d);
  204. });
  205. var text = svg.selectAll("text")
  206. .data(nodes)
  207. .enter().append("text")
  208. .attr("class", "label")
  209. .style("fill-opacity", function (d) {
  210. return d.parent === root ? 1 : 0;
  211. })
  212. .style("display", function (d) {
  213. return d.parent === root ? null : "none";
  214. })
  215. .text(function (d) {
  216. return d.name;
  217. });
  218. var node = svg.selectAll("circle,text");
  219. d3.select("#vis")
  220. .style("background", color(-1))
  221. .style("opacity", 0.8)
  222. .on("click", function () {
  223. zoom(root);
  224. });
  225. zoomTo([root.x, root.y, root.r * 2 + margin]);
  226. function zoom(d) {
  227. var focus = d;
  228. var transition = d3.transition()
  229. .duration(d3.event.altKey ? 7500 : 750)
  230. .tween("zoom", function (d) {
  231. var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
  232. return function (t) {
  233. zoomTo(i(t));
  234. };
  235. });
  236. transition.selectAll("text")
  237. .filter(function (d) {
  238. return d.parent === focus || this.style.display === "inline";
  239. })
  240. .style("fill-opacity", function (d) {
  241. return d.parent === focus ? 1 : 0;
  242. })
  243. .each("start", function (d) {
  244. if (d.parent === focus) this.style.display = "inline";
  245. })
  246. .each("end", function (d) {
  247. if (d.parent !== focus) this.style.display = "none";
  248. });
  249. }
  250. function zoomTo(v) {
  251. var k = diameter / v[2];
  252. view = v;
  253. node.attr("transform", function (d) {
  254. return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")";
  255. });
  256. circle.attr("r", function (d) {
  257. return d.r * k;
  258. });
  259. }
  260. });
  261. //显示金牌
  262. function showGoldMedal(d) {
  263. var word = d.name;
  264. var indexWord = -1;
  265. for (var i = 0; i < worldCountryInfo.length; i++) {
  266. if (word == worldCountryInfo[i][0])
  267. indexWord = i;
  268. }
  269. if (indexWord == -1) {
  270. closeInfoWin();
  271. if (d.depth) {
  272. map.zoomTo(d.depth);
  273. } else {
  274. map.zoomTo(0);
  275. }
  276. return;
  277. }
  278. if (!d.children || !d.children[0].size) return;
  279. var goldCount = d.children[0].size;
  280. var lon = worldCountryInfo[indexWord][2];
  281. var lat = worldCountryInfo[indexWord][3];
  282. var lonlat = new SuperMap.LonLat(lon, lat);
  283. map.setCenter(new SuperMap.LonLat(lon + 2, lat), 5);
  284. openInfoWin(lonlat, word, goldCount);
  285. }
  286. //添加弹出窗
  287. function openInfoWin(lonlat, name, count) {
  288. closeInfoWin();
  289. var contentHTML = "<div style='width:160px; opacity: 0.8; overflow: hidden; text-align: center'>";
  290. contentHTML += "<div>" + name + "</div>";
  291. contentHTML += "<div>";
  292. contentHTML += "<img src = 'images/aoyunjinpai08.png'/>";
  293. contentHTML += "<div ><strong style='font-size:1.2em;color: blue'>" + count + "</strong><span> 枚</span></div>";
  294. contentHTML += "</div>";
  295. popup = new SuperMap.Popup.FramedCloud("popwin", new SuperMap.LonLat(lonlat.lon, lonlat.lat), null, contentHTML, null, true, function () {
  296. closeInfoWin();
  297. }, false);
  298. map.addPopup(popup);
  299. }
  300. //关闭弹出窗
  301. function closeInfoWin() {
  302. if (popup) {
  303. try {
  304. map.removePopup(popup);
  305. }
  306. catch (e) {
  307. }
  308. }
  309. }
  310. }
  311. </script>
  312. </body>
  313. </html>