others_echart_graphBar.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <title data-i18n="resources.title_eChartGraphBar"></title>
  9. <style type="text/css">
  10. .arc path {
  11. stroke: #fff;
  12. }
  13. </style>
  14. </head>
  15. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  16. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  17. <script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
  18. <script type="text/javascript" exclude="iclient-classic" include="echarts-all"
  19. src="../../dist/classic/include-classic.js"></script>
  20. <script type="text/javascript">
  21. var map, layer, utfgrid, control, chartLayer,
  22. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  23. url = host + "/iserver/services/map-china400/rest/maps/China";
  24. var pieChartDom;
  25. var elementsDiv;
  26. var isMapMoving = false;
  27. function init() {
  28. map = new SuperMap.Map("map", {
  29. controls: [
  30. new SuperMap.Control.ScaleLine(),
  31. new SuperMap.Control.Zoom(),
  32. new SuperMap.Control.LayerSwitcher(),
  33. new SuperMap.Control.Navigation({
  34. dragPanOptions: {
  35. enableKinetic: true
  36. }
  37. })],
  38. projection: "EPSG:3857"
  39. });
  40. //地图移动过程中不显示图表
  41. map.events.on({
  42. "movestart": function () {
  43. document.getElementById("piechart").innerHTML = "";
  44. pieChartDom.style.display = "none";
  45. isMapMoving = true;
  46. }
  47. });
  48. map.events.on({
  49. "moveend": function () {
  50. isMapMoving = false;
  51. }
  52. });
  53. //基础图层
  54. layer = new SuperMap.Layer.TiledDynamicRESTLayer("China", url, {transparent: true}, {
  55. useCanvas: true,
  56. maxResolution: "auto"
  57. });
  58. //创建 Elements 图层,用于显示饼图
  59. chartLayer = new SuperMap.Layer.Elements("eCharts");
  60. //获取 Elements 图层 div
  61. elementsDiv = chartLayer.getDiv();
  62. //设置Elements实例的div为地图大小
  63. var mapsize = map.getSize();
  64. elementsDiv.style.width = mapsize.w;
  65. elementsDiv.style.height = mapsize.h;
  66. //创建图表 div, 设置其基本属性, 并添加到 Elements 图层
  67. pieChartDom = document.createElement("div");
  68. pieChartDom.id = "piechart";
  69. pieChartDom.style.width = "400px";
  70. pieChartDom.style.height = "240px";
  71. pieChartDom.style.position = "absolute";
  72. pieChartDom.style.opacity = 0.8;
  73. elementsDiv.appendChild(pieChartDom);
  74. //创建 UTFGrid 图层,用于获取饼图所需信息
  75. utfgrid = new SuperMap.Layer.UTFGrid("UTFGridLayer", url,
  76. {
  77. layerName: "China_Province_pl@China",
  78. utfTileSize: 256,
  79. pixcell: 8,
  80. isUseCache: true
  81. },
  82. {
  83. utfgridResolution: 8
  84. });
  85. layer.events.on({"layerInitialized": addLayer});
  86. control = new SuperMap.Control.UTFGrid({
  87. layers: [utfgrid],
  88. callback: callback,
  89. handlerMode: "move"
  90. });
  91. map.addControl(control);
  92. }
  93. $(document).ready(function () {
  94. init();
  95. });
  96. // 鼠标移动中仍保持在同一个数据上
  97. var isOneData = "";
  98. var theDataCache;
  99. // 相同数据检测
  100. var callback = function (infoLookup, loc, pixel) {
  101. if (infoLookup && isMapMoving === false) {
  102. var info;
  103. for (var idx in infoLookup) {
  104. info = infoLookup[idx];
  105. if (info && info.data) {
  106. document.getElementById("piechart").innerHTML = "";
  107. pieChartDom.style.display = "block";
  108. // 基于准备好的dom,初始化echarts图表
  109. var myChart = echarts.init(pieChartDom);
  110. var thedata;
  111. if (isOneData != info.data.SMID) {
  112. // 数据(随机生成)
  113. thedata = [
  114. getRandomNumber(0, 1000, 0),
  115. getRandomNumber(0, 1000, 0),
  116. getRandomNumber(0, 1000, 0),
  117. getRandomNumber(0, 1000, 0),
  118. getRandomNumber(0, 1000, 0),
  119. getRandomNumber(0, 1000, 0)
  120. ];
  121. isOneData = info.data.SMID
  122. }
  123. else {
  124. thedata = theDataCache;
  125. }
  126. theDataCache = thedata;
  127. // echart 图表配置参数
  128. var option = {
  129. tooltip: {
  130. show: true
  131. },
  132. title: {
  133. x: "center",
  134. text: resources.text_salesVolume1 + info.data.NAME + resources.text_salesVolume2
  135. },
  136. xAxis: [
  137. {
  138. type: 'category',
  139. data: [resources.text_productA, resources.text_productB, resources.text_productC, resources.text_productD, resources.text_productE, resources.text_productF]
  140. }
  141. ],
  142. yAxis: [
  143. {
  144. type: 'value'
  145. }
  146. ],
  147. series: [
  148. {
  149. "name": "销量",
  150. "type": "bar",
  151. "data": thedata
  152. }
  153. ]
  154. };
  155. // 为echarts对象加载数据
  156. myChart.setOption(option);
  157. // 图表位置
  158. pieChartDom.style.left = (pixel.x + 20) + "px";
  159. pieChartDom.style.top = (pixel.y - 20) + "px";
  160. // 图表背景颜色
  161. pieChartDom.style.backgroundColor = "#F4F3F0";
  162. }
  163. else {
  164. //清除饼图
  165. document.getElementById("piechart").innerHTML = "";
  166. pieChartDom.style.display = "none";
  167. }
  168. }
  169. }
  170. };
  171. // 获取指定范围内的随机数
  172. // min - 范围下限
  173. // max - 范围上限
  174. // decimalNum - 返回结果的小数位数。如果为 0,返回整数。
  175. function getRandomNumber(min, max, decimalNum) {
  176. var rNum = min + Math.random() * (max - min);
  177. if (decimalNum) {
  178. if (!isNaN(decimalNum)) {
  179. return rNum;
  180. }
  181. else {
  182. decimalNum = parseInt(decimalNum);
  183. }
  184. if (decimalNum === 0) {
  185. return Math.round(rNum);
  186. }
  187. else {
  188. return parseFloat(rNum).toFixed(decimalNum);
  189. }
  190. }
  191. else {
  192. return rNum;
  193. }
  194. }
  195. //添加图层
  196. function addLayer() {
  197. var center = new SuperMap.LonLat(9733502.481499, 4614406.969325);
  198. map.addLayers([layer, chartLayer, utfgrid]);
  199. map.setCenter(center, 4);
  200. }
  201. </script>
  202. </body>
  203. </html>