theme_ctl_GraphPie.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_GraphPie"></title>
  9. <style type="text/css">
  10. .editPane {
  11. position: absolute;
  12. right: 60px;
  13. top: 50px;
  14. text-align: center;
  15. background: #FFF;
  16. z-index: 1000;
  17. }
  18. </style>
  19. </head>
  20. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  21. <div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
  22. <div class='panel-heading'>
  23. <h5 class='panel-title text-center' data-i18n="resources.title_GraphPie"></h5>
  24. </div>
  25. <div class='panel-body' id='params'>
  26. <p></p>
  27. <div align='right' class='button-group'>
  28. <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_addThemeLayer" onclick="addThemeLayer()"/>
  29. <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.btn_highlightFill" onclick="resetStyleB()"/>
  30. <input type='button' id='btn3' class='btn btn-primary' data-i18n="[value]resources.btn_highlightStroke" onclick="resetStyleC()"/>
  31. <input type='button' id='btn4' class='btn btn-primary' data-i18n="[value]resources.btn_initialStyle" onclick="resetStyleA()"/>
  32. <input type='button' id='btn5' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear" onclick="clearThemeLayer()"/>
  33. </div>
  34. </div>
  35. </div>
  36. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  37. <script type="text/javascript" exclude="iclient-classic" include="Pie" src="../../dist/classic/include-classic.js"></script>
  38. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  39. <script src='../data/chinaConsumptionLevel.js'></script>
  40. <script type="text/javascript">
  41. var map, layer, themeLayer, infowin, infowinPosition;
  42. var host = window.isLocal ? window.server : "https://iserver.supermap.io",
  43. url = host + "/iserver/services/map-china400/rest/maps/China_4326";
  44. // 统计图模块要求浏览器支持 Canvas 渲染
  45. if (!document.createElement('canvas').getContext) {
  46. widgets.alert.showAlert(resources.msg_supportCanvas,false);
  47. }
  48. map = new SuperMap.Map("map", {
  49. controls: [
  50. new SuperMap.Control.LayerSwitcher(),
  51. new SuperMap.Control.ScaleLine(),
  52. new SuperMap.Control.Zoom(),
  53. new SuperMap.Control.Navigation({
  54. dragPanOptions: {
  55. enableKinetic: true
  56. }
  57. })]
  58. });
  59. layer = new SuperMap.Layer.TiledDynamicRESTLayer("Jingjin", url, {
  60. transparent: true,
  61. cacheEnabled: true
  62. }, {maxResolution: "auto"});
  63. layer.events.on({"layerInitialized": addLayer});
  64. // 创建一个饼状图(Pie)统计专题图图层
  65. themeLayer = new SuperMap.Layer.Graph("ThemeLayer", "Pie");
  66. // 指定用于专题图制作的属性字段
  67. themeLayer.themeFields = ["CON2009", "CON2010", "CON2011", "CON2012", "CON2013"];
  68. // 配置图表参数
  69. themeLayer.chartsSetting = {
  70. // width,height,codomain 分别表示图表宽、高、数据值域;此三项参数为必设参数
  71. width: 100,
  72. height: 100,
  73. codomain: [0, 40000], // 允许图表展示的值域范围,此范围外的数据将不制作图表
  74. // 饼图扇形(表示字段值的图形)样式
  75. sectorStyle: {fillOpacity: 0.8},
  76. // 按字段设置饼图扇形 (样式与 themeLayer.themeFields 数组中的字段名称一一对应)
  77. sectorStyleByFields: [{fillColor: "#FFB980"}, {fillColor: "#5AB1EF"}, {fillColor: "#B6A2DE"}, {fillColor: "#2EC7C9"}, {fillColor: "#D87A80"}],
  78. // 饼图扇形 hover 样式
  79. sectorHoverStyle: {fillOpacity: 1}
  80. };
  81. // 注册专题图 mousemove, mouseout事件(注意:专题图图层对象自带 on 函数,没有 events 对象)
  82. themeLayer.on("mousemove", showInfoWin);
  83. themeLayer.on("mouseout", closeInfoWin);
  84. // 注册地图 mousemove,用于获取当前鼠标在地图中的像素位置
  85. map.events.on({
  86. "mousemove": function (e) {
  87. infowinPosition = e.xy.clone();
  88. // 偏移
  89. infowinPosition.x += 40;
  90. infowinPosition.y -= 25;
  91. }
  92. });
  93. function addLayer() {
  94. map.addLayers([layer, themeLayer]);
  95. map.setCenter(new SuperMap.LonLat(104.067923, 34.679943), 2);
  96. }
  97. //构建 feature 数据, 专题图的数据必须是 SuperMap.Feature.Vector
  98. function addThemeLayer() {
  99. clearThemeLayer();
  100. var features = [];
  101. for (var i = 0, len = chinaConsumptionLevel.length; i < len; i++) {
  102. // 省居民消费水平(单位:元)信息
  103. var provinceInfo = chinaConsumptionLevel[i];
  104. var geo = new SuperMap.Geometry.Point(provinceInfo[1], provinceInfo[2]);
  105. var attrs = {};
  106. attrs.NAME = provinceInfo[0];
  107. attrs.CON2009 = provinceInfo[3];
  108. attrs.CON2010 = provinceInfo[4];
  109. attrs.CON2011 = provinceInfo[5];
  110. attrs.CON2012 = provinceInfo[6];
  111. attrs.CON2013 = provinceInfo[7];
  112. var fea = new SuperMap.Feature.Vector(geo, attrs);
  113. features.push(fea);
  114. }
  115. themeLayer.addFeatures(features);
  116. }
  117. // 清除专题图层中的内容
  118. function clearThemeLayer() {
  119. themeLayer.clear();
  120. closeInfoWin();
  121. }
  122. // 显示地图弹窗
  123. function showInfoWin(e) {
  124. // e.target 是图形对象,即数据的可视化对象,饼状图中是扇形。
  125. // 图形对象的 refDataID 属性是数据(feature)的 id 属性,它指明图形对象是由那个数据制作而来;
  126. // 图形对象的 dataInfo 属性是图形对象表示的具体数据,他有两个属性,field 和 value;
  127. if (e.target && e.target.refDataID && e.target.dataInfo) {
  128. closeInfoWin();
  129. // 获取图形对应的数据 (feature)
  130. var fea = themeLayer.getFeatureById(e.target.refDataID);
  131. var info = e.target.dataInfo;
  132. // 弹窗内容
  133. var contentHTML = "<div style='color: #000; background-color: #fff'>";
  134. contentHTML += "省级行政区名称:<br><strong>" + fea.attributes.NAME + "</strong>";
  135. contentHTML += "<hr style='margin: 3px'>";
  136. switch (info.field) {
  137. case "CON2009":
  138. contentHTML += "09年居民消费水平 <br/><strong>" + info.value + "</strong>(元)";
  139. break;
  140. case "CON2010":
  141. contentHTML += "10年居民消费水平 <br/><strong>" + info.value + "</strong>(元)";
  142. break;
  143. case "CON2011":
  144. contentHTML += "11年居民消费水平 <br/><strong>" + info.value + "</strong>(元)";
  145. break;
  146. case "CON2012":
  147. contentHTML += "12年居民消费水平 <br/><strong>" + info.value + "</strong>(元)";
  148. break;
  149. case "CON2013":
  150. contentHTML += "13年居民消费水平 <br/><strong>" + info.value + "</strong>(元)";
  151. break;
  152. default:
  153. contentHTML += "No Data";
  154. }
  155. contentHTML += "</div>";
  156. // 弹出框大小
  157. var infowinSize = (SuperMap.Browser.name == "firefox") ? new SuperMap.Size(150, 105) : new SuperMap.Size(140, 90);
  158. // 弹出窗地理位置
  159. var lonLat = map.getLonLatFromPixel(infowinPosition);
  160. infowin = new SuperMap.Popup(
  161. "infowin",
  162. lonLat,
  163. infowinSize,
  164. contentHTML,
  165. false,
  166. false,
  167. null);
  168. infowin.setBackgroundColor("#fff");
  169. infowin.setOpacity(0.8);
  170. if (infowin) map.removePopup(infowin);
  171. map.addPopup(infowin);
  172. }
  173. }
  174. // 高亮纯色样式
  175. function resetStyleB() {
  176. themeLayer.chartsSetting.sectorStyleByFields = [{fillColor: "#86B379"}, {fillColor: "#68A54A"}, {fillColor: "#408829"}, {fillColor: "#7CCD7C"}, {fillColor: "#228B22"}];
  177. themeLayer.chartsSetting.sectorHoverStyle = {
  178. fillColor: "#397B29",
  179. fillOpacity: 1
  180. };
  181. themeLayer.redraw();
  182. }
  183. // 高亮描边样式
  184. function resetStyleC() {
  185. themeLayer.chartsSetting.sectorStyleByFields = [{fillColor: "#C8E49C"}, {fillColor: "#ED9678"}, {fillColor: "#E7DAC9"}, {fillColor: "#CB8E85"}, {fillColor: "#F3F39D"}];
  186. themeLayer.chartsSetting.sectorHoverStyle = {
  187. stroke: true,
  188. strokeColor: "#D8361B",
  189. strokeWidth: 2,
  190. fillOpacity: 1
  191. };
  192. themeLayer.redraw();
  193. }
  194. // 初始样式
  195. function resetStyleA() {
  196. themeLayer.chartsSetting.sectorStyleByFields = [{fillColor: "#FFB980"}, {fillColor: "#5AB1EF"}, {fillColor: "#B6A2DE"}, {fillColor: "#2EC7C9"}, {fillColor: "#D87A80"}];
  197. themeLayer.chartsSetting.sectorHoverStyle = {fillOpacity: 1};
  198. themeLayer.redraw();
  199. }
  200. // 移除和销毁地图弹窗
  201. function closeInfoWin() {
  202. if (infowin) {
  203. try {
  204. map.removePopup(infowin);
  205. }
  206. catch (e) {
  207. widgets.alert.showAlert(e.message,false);
  208. }
  209. }
  210. }
  211. </script>
  212. </body>
  213. </html>