components_chart_isvr.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en-US">
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n='resources.title_chart_iServer'></title>
  9. <script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
  10. <script type="text/javascript" include="iclient-mapboxgl-css,echarts" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
  11. <style>
  12. .chart-setting {
  13. position: absolute;
  14. top: 50px;
  15. right: 10px;
  16. width: 450px;
  17. height: 50px;
  18. z-index: 800;
  19. background-color: #fff;
  20. }
  21. .chart-type-btn {
  22. position: absolute;
  23. top: 8px;
  24. right: 20px;
  25. width: 112px;
  26. height: 36px;
  27. float: right;
  28. z-index: 800;
  29. }
  30. .chart-setting .input-group {
  31. left: 16px;
  32. top: 8px;
  33. width: 260px;
  34. }
  35. .graph {
  36. margin: 5px;
  37. width: 26px;
  38. height: 26px;
  39. border: none;
  40. border-radius: 4px;
  41. background-size: 100%;
  42. outline: none;
  43. }
  44. button {
  45. float: right;
  46. }
  47. #bar {
  48. background-image: url("../img/bar.png");
  49. }
  50. #line {
  51. background-image: url("../img/ling.png");
  52. }
  53. #scatter {
  54. background-image: url("../img/scatter.png");
  55. }
  56. #chart {
  57. position: absolute;
  58. top: 100px;
  59. right: 10px;
  60. width: 450px;
  61. height: 350px;
  62. z-index: 800;
  63. }
  64. </style>
  65. </head>
  66. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  67. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  68. <div class="chart-setting"></div>
  69. <div id="chart"></div>
  70. <script>
  71. init();
  72. function init() {
  73. var container = document.getElementsByClassName("chart-setting")[0];
  74. container.innerHTML = "<div class='input-group'><span class='input-group-addon'>" +
  75. resources.text_dataset + "</span>" +
  76. "<select class='form-control' id='rule' name='rule'>" +
  77. "<option value='https://iserver.supermap.io/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/Landuse_R' selected>Landuse_R@Jingjin</option>" +
  78. "<option value='https://iserver.supermap.io/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/BaseMap_P'>BaseMap_P@Jingjin</option>" +
  79. "<option value='https://iserver.supermap.io/iserver/services/map-world/rest/maps/World/layers/Rivers@World@@World'>Rivers@World</option>" +
  80. "</select></div>" +
  81. "<div class='chart-type-btn'>" +
  82. "<button type='button' class='btn btn-default graph' id='scatter' title='" + resources.title_Scatter +
  83. "'></button>" +
  84. "<button type='button' class='btn btn-default graph' id='line' title='" + resources.title_GraphLine +
  85. "'></button>" +
  86. "<button type='button' class='btn btn-default graph active' id='bar' title='" + resources.title_GraphBar +
  87. "''></button></div>"
  88. }
  89. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  90. var map, resultLayer, url = host + "/iserver/services/map-world/rest/maps/World";
  91. var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
  92. " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
  93. " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
  94. map = new mapboxgl.Map({
  95. container: 'map',
  96. style: {
  97. "version": 8,
  98. "sources": {
  99. "raster-tiles": {
  100. "attribution": attribution,
  101. "type": "raster",
  102. "tiles": [url + "/zxyTileImage.png?z={z}&x={x}&y={y}"],
  103. "tileSize": 256
  104. }
  105. },
  106. "layers": [{
  107. "id": "simple-tiles",
  108. "type": "raster",
  109. "source": "raster-tiles",
  110. }]
  111. },
  112. center: [118, 40],
  113. zoom: 6
  114. });
  115. //图表组件
  116. var options = {
  117. type: 'bar',
  118. datasets: {
  119. type: 'iServer', //iServer iPortal
  120. url: host + "/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/Landuse_R",
  121. withCredentials: false, //默认值是false
  122. queryInfo: {
  123. attributeFilter: "SmID > 0"
  124. }
  125. },
  126. chartOptions: [{
  127. xAxis: {
  128. field: "LANDTYPE",
  129. name: "type"
  130. },
  131. yAxis: {
  132. field: "AREA",
  133. name: "Area"
  134. }
  135. }]
  136. }
  137. var barChart = new SuperMap.Components.Chart("chart", options);
  138. resultLayer = {
  139. "id": 'jinjin',
  140. "type": 'fill',
  141. // "layout": {
  142. // 'visibility': 'visible',
  143. // },
  144. "paint": {
  145. 'fill-color': 'blue', // 返回的是id的值
  146. }
  147. };
  148. //加载图表之后,将要素添加到地图上
  149. barChart.onAdd(addDataToMap);
  150. function addDataToMap() {
  151. var features = barChart.getFeatures();
  152. // resultLayer.source = {
  153. // "type": "geojson",
  154. // "data": features
  155. // }
  156. map.addSource(resultLayer.id,{
  157. "type": "geojson",
  158. "data": features
  159. })
  160. resultLayer.source = resultLayer.id;
  161. map.addLayer(resultLayer);
  162. }
  163. //为图表类型按钮绑定事件
  164. bindEvent();
  165. function bindEvent() {
  166. $(".graph").on("click", function () {
  167. $(".graph").removeClass("active");
  168. });
  169. $("#bar").on("click", function () {
  170. $("#bar").addClass("active");
  171. barChart.changeType('bar');
  172. });
  173. $("#line").on("click", function () {
  174. $("#line").addClass("active");
  175. barChart.changeType('line');
  176. });
  177. $("#scatter").on("click", function () {
  178. $("#scatter").addClass("active");
  179. barChart.changeType('scatter');
  180. });
  181. }
  182. //切换数据集
  183. $("#rule").change(function () {
  184. var selectedIndex = ($(this).get(0).selectedIndex);
  185. var url = $(this).val();
  186. var chartOption, serviceType, withCredentials;
  187. // 如果已上图,删除当前地图上的图层和数据集
  188. map.getLayer(resultLayer.id) && map.removeLayer(resultLayer.id) && map.removeSource(resultLayer.id);
  189. if (selectedIndex === 0) {
  190. chartOption = [{
  191. xAxis: {
  192. field: "LANDTYPE",
  193. name: "type"
  194. },
  195. yAxis: {
  196. field: "AREA",
  197. name: "Area"
  198. }
  199. }];
  200. // 重新设置上图的id, 图层的样式,图层ID
  201. resultLayer = {
  202. "id": 'jinjin',
  203. "type": 'fill',
  204. "paint": {
  205. 'fill-color': 'blue', // 返回的是id的值
  206. }
  207. };
  208. } else if (selectedIndex === 1) {
  209. chartOption = [{
  210. xAxis: {
  211. field: "NAME",
  212. name: "NAME"
  213. },
  214. yAxis: {
  215. field: "ADCLASS",
  216. name: "ADCLASS"
  217. },
  218. }];
  219. resultLayer = {
  220. "id": 'ADCLASS',
  221. "type": 'circle',
  222. "paint": {
  223. "circle-radius": 8,
  224. /* 圆的直径,单位像素 */
  225. "circle-color": 'blue'
  226. }
  227. };
  228. } else {
  229. chartOption = [{
  230. xAxis: {
  231. field: "NAME",
  232. name: "name"
  233. },
  234. yAxis: {
  235. field: "KILOMETERS",
  236. name: "Kilometers"
  237. }
  238. }];
  239. resultLayer = {
  240. "id": 'river',
  241. "type": 'line',
  242. "paint": {
  243. "line-width": 3,
  244. 'line-color': '#78c4ec',
  245. }
  246. };
  247. }
  248. serviceType = 'iServer';
  249. datasets = {
  250. type: serviceType, //iServer iPortal
  251. url,
  252. withCredentials, //默认值是false
  253. queryInfo: {
  254. attributeFilter: "SmID > 0"
  255. }
  256. };
  257. barChart.onAdd(addDataToMap);
  258. barChart.updateData(datasets, chartOption);
  259. });
  260. </script>
  261. </body>
  262. </html>