theme_ctl_jingjinPopGraph.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_jingjinPopGraph"></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_jingjinPopGraph"></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_switchBar" onclick="toBar()"/>
  30. <input type='button' id='btn3' class='btn btn-primary' data-i18n="[value]resources.btn_switchLine" onclick="toLine()"/>
  31. <input type='button' id='btn4' class='btn btn-primary' data-i18n="[value]resources.btn_switchPie" onclick="toPie()"/>
  32. <input type='button' id='btn5' class='btn btn-primary' data-i18n="[value]resources.btn_updateData" onclick="updateData()"/>
  33. <input type='button' id='btn6' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear" onclick="clearThemeLayer()"/>
  34. </div>
  35. </div>
  36. </div>
  37. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  38. <script type="text/javascript" exclude="iclient-classic" include="Bar,Line,Pie" src="../../dist/classic/include-classic.js"></script>
  39. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.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. url1 = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
  44. url2 = host + "/iserver/services/data-jingjin/rest/data";
  45. // 柱状图表配置
  46. var settingForBar = {
  47. // width,height,codomain 分别表示图表宽、高、数据值域;此三项参数为必设参数
  48. width: 180,
  49. height: 100,
  50. // 允许图表展示的值域范围,此范围外的数据将不制作图表
  51. codomain: [0, 1000],
  52. // 自动处理小数位
  53. decimalNumber: 2,
  54. barStyle: {fillOpacity: 0.7}, // 柱状图中柱条的(表示字段值的图形)样式
  55. // 按字段设置柱条样式 (与 themeLayer.themeFields 中的字段一一对应) // '#ff9277', ' #dddd00', ' #ffc877', ' #bbe3ff', ' #d5ffbb',
  56. barStyleByFields: [{fillColor: "#ff9277"}, {fillColor: "#dddd00"}, {fillColor: "#ffc877"}],
  57. barHoverStyle: {fillOpacity: 1}, // 柱条 hover 样式
  58. xShapeBlank: [10, 10, 10], // 水平方向上的空白间距参数
  59. axisYTick: 4, // y 轴刻度数量
  60. // y 轴标签及其样式
  61. axisYLabels: ["1000", "750", "500", "250", "0"],
  62. axisYLabelsStyle: {fillColor: "#008ACD"},
  63. // x 轴标签及其样式
  64. axisXLabels: ["92年", "95年", "99年"],
  65. axisXLabelsStyle: {fillColor: "#008ACD"},
  66. backgroundRadius: [5, 5, 5, 5] // 背景框圆角参数
  67. };
  68. // 折线图表配置
  69. var settingForLine = {
  70. width: 180,
  71. height: 100,
  72. codomain: [0, 1000],
  73. decimalNumber: 2,
  74. // 折线 style
  75. lineStyle: {strokeColor: "#D8361B", strokeOpacity: 0.7},
  76. // 折线节点(表示字段值的图形)样式
  77. pointStyle: {
  78. stroke: true,
  79. strokeColor: "#D8361B",
  80. pointRadius: 3,
  81. strokeWidth: 1,
  82. fillColor: "#F3F3F3",
  83. fillOpacity: 1
  84. },
  85. // 折线节点 hover 样式
  86. pointHoverStyle: {
  87. fillColor: "#D8361B"
  88. },
  89. xShapeBlank: [10, 10],
  90. axisYTick: 4,
  91. axisYLabels: ["1000", "750", "500", "250", "0"],
  92. axisXLabels: ["92年", "95年", "99年"],
  93. backgroundRadius: [5, 5, 5, 5]
  94. };
  95. // 饼图配置
  96. var settingForPie = {
  97. width: 100,
  98. height: 100,
  99. codomain: [0, 1000],
  100. decimalNumber: 2,
  101. // 饼图扇形(表示字段值的图形)样式
  102. sectorStyle: {fillOpacity: 0.8},
  103. // 按字段设置饼图扇形 (样式与 themeLayer.themeFields 数组中的字段名称一一对应)
  104. sectorStyleByFields: [{fillColor: "#ff9277"}, {fillColor: "#dddd00"}, {fillColor: "#ffc877"}],
  105. // 饼图扇形 hover 样式
  106. sectorHoverStyle: {fillOpacity: 1}
  107. };
  108. // 统计图模块要求浏览器支持 Canvas 渲染
  109. if (!document.createElement('canvas').getContext) {
  110. widgets.alert.showAlert(resources.msg_supportCanvas, false);
  111. }
  112. // 创建地图
  113. map = new SuperMap.Map("map", {
  114. controls: [
  115. new SuperMap.Control.LayerSwitcher(),
  116. new SuperMap.Control.ScaleLine(),
  117. new SuperMap.Control.Zoom(),
  118. new SuperMap.Control.Navigation({
  119. dragPanOptions: {
  120. enableKinetic: true
  121. }
  122. })]
  123. });
  124. layer = new SuperMap.Layer.TiledDynamicRESTLayer("Jingjin", url1, {
  125. transparent: true,
  126. cacheEnabled: true
  127. }, {maxResolution: "auto"});
  128. layer.events.on({"layerInitialized": addLayer});
  129. // 创建一个柱状(Bar)统计专题图
  130. themeLayer = new SuperMap.Layer.Graph("ThemeLayer", "Bar");
  131. // 配置图表参数
  132. themeLayer.chartsSetting = settingForBar;
  133. themeLayer.setOpacity(0.9);
  134. // 注册专题图 mousemove, mouseout 事件 (注意:专题图图层对象自带 on 函数注册事件,专题图对象没有 events 属性)
  135. themeLayer.on("mousemove", showInfoWin);
  136. themeLayer.on("mouseout", closeInfoWin);
  137. // 注册地图 mousemove,用于获取当前鼠标的地理位置
  138. map.events.on({
  139. "mousemove": function (e) {
  140. infowinPosition = e.xy.clone();
  141. // 偏移显示弹出窗
  142. infowinPosition.x += 40;
  143. infowinPosition.y -= 20;
  144. }
  145. });
  146. function addLayer() {
  147. map.addLayers([layer, themeLayer]);
  148. map.setCenter(new SuperMap.LonLat(117, 40.004), 0);
  149. }
  150. //获取 feature 数据, 专题图的数据必须是 SuperMap.Feature.Vector
  151. function addThemeLayer() {
  152. clearThemeLayer();
  153. // 使用 GetFeaturesBySQLService 获取数据
  154. var getFeatureParam, getFeatureBySQLService, getFeatureBySQLParams;
  155. getFeatureParam = new SuperMap.REST.FilterParameter({
  156. name: "Jingjin",
  157. attributeFilter: "SMID > -1"
  158. });
  159. getFeatureBySQLParams = new SuperMap.REST.GetFeaturesBySQLParameters({
  160. queryParameter: getFeatureParam,
  161. toIndex: 500,
  162. datasetNames: ["Jingjin:BaseMap_R"]
  163. });
  164. getFeatureBySQLService = new SuperMap.REST.GetFeaturesBySQLService(url2, {
  165. eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}
  166. });
  167. getFeatureBySQLService.processAsync(getFeatureBySQLParams);
  168. }
  169. function processCompleted(getFeaturesEventArgs) {
  170. // 指定用于专题图制作的属性字段
  171. themeLayer.themeFields = ["POP_1992", "POP_1995", "POP_1999"];
  172. themeLayer.overlayWeightField = "POP_1999";
  173. // 改变x轴上的标签
  174. settingForBar.axisXLabels = settingForLine.axisXLabels = settingForPie.axisXLabels = ["92年", "95年", "99年"];
  175. // 图标宽度 180
  176. settingForBar.width = settingForLine.width = 180;
  177. var result = getFeaturesEventArgs.result;
  178. if (result && result.features) {
  179. // 向专题图层添加数据
  180. themeLayer.addFeatures(result.features);
  181. }
  182. }
  183. function processFailed(e) {
  184. widgets.alert.showAlert(e.error.errorMsg,false);
  185. }
  186. // 清除专题图层中的内容
  187. function clearThemeLayer() {
  188. themeLayer.clear();
  189. closeInfoWin();
  190. }
  191. // 显示地图弹窗
  192. function showInfoWin(e) {
  193. // e.target 是图形对象,即数据的可视化对象;
  194. // 图形对象的 refDataID 属性是数据(feature)的 id 属性,它指明图形对象是由那个数据制作而来;
  195. // 图形对象的 dataInfo 属性是图形对象表示的具体数据,他有两个属性,field 和 value;
  196. if (e.target && e.target.refDataID && e.target.dataInfo) {
  197. closeInfoWin();
  198. // 获取图形对应的数据 (feature)
  199. var fea = themeLayer.getFeatureById(e.target.refDataID);
  200. var info = e.target.dataInfo;
  201. // 弹窗内容
  202. var contentHTML = "<div style='color: yellow;'>";
  203. contentHTML += "城市:" + fea.attributes.NAME;
  204. contentHTML += "<hr style='margin: 3px'>";
  205. switch (info.field) {
  206. case "POP_1992":
  207. contentHTML += "92年人口 <br/>" + info.value + "(万)";
  208. break;
  209. case "POP_1995":
  210. contentHTML += "95年人口 <br/>" + info.value + "(万)";
  211. break;
  212. case "POP_1999":
  213. contentHTML += "99年人口 <br/>" + info.value + "(万)";
  214. break;
  215. case "POP_2003":
  216. contentHTML += "03年人口 <br/>" + info.value + "(万)";
  217. break;
  218. case "POP_2008":
  219. contentHTML += "08年人口 <br/>" + info.value + "(万)";
  220. break;
  221. default:
  222. contentHTML += "No Data";
  223. }
  224. contentHTML += "</div>";
  225. // 弹出框大小
  226. var infowinSize = (SuperMap.Browser.name == "firefox") ? new SuperMap.Size(110, 85) : new SuperMap.Size(100, 75);
  227. // 弹出窗地理位置
  228. var lonLat = map.getLonLatFromPixel(infowinPosition);
  229. infowin = new SuperMap.Popup(
  230. "infowin",
  231. lonLat,
  232. infowinSize,
  233. contentHTML,
  234. false,
  235. false,
  236. null);
  237. infowin.setBackgroundColor("#000000");
  238. infowin.setOpacity(0.6);
  239. if (infowin) map.removePopup(infowin);
  240. map.addPopup(infowin);
  241. }
  242. }
  243. // 移除和销毁地图弹窗
  244. function closeInfoWin() {
  245. if (infowin) {
  246. try {
  247. map.removePopup(infowin);
  248. }
  249. catch (e) {
  250. widgets.alert.showAlert(e.message,false);
  251. }
  252. }
  253. }
  254. // 更新数据
  255. function updateData() {
  256. var feas = themeLayer.features;
  257. for (var i = 0, len = feas.length; i < len; i++) {
  258. var fea = feas[i];
  259. fea.attributes.POP_1992 = getRandomNumber(50, 950, 2);
  260. fea.attributes.POP_1995 = getRandomNumber(50, 950, 2);
  261. fea.attributes.POP_1999 = getRandomNumber(50, 950, 2);
  262. fea.attributes.POP_2003 = getRandomNumber(50, 950, 2);
  263. fea.attributes.POP_2008 = getRandomNumber(50, 950, 2);
  264. }
  265. // 重新指定用于专题图制作的属性字段
  266. themeLayer.themeFields = ["POP_1992", "POP_1995", "POP_1999", "POP_2003", "POP_2008"];
  267. // 改变 x 轴上的标签
  268. settingForBar.axisXLabels = settingForLine.axisXLabels = settingForPie.axisXLabels = ["92年", "95年", "99年", "03年", "08年"];
  269. // 图标宽度改为 220
  270. settingForBar.width = settingForLine.width = 220;
  271. // 为 03、08年 数据增加 style
  272. settingForBar.barStyleByFields.push({fillColor: "#bbe3ff"});
  273. settingForBar.barStyleByFields.push({fillColor: "#d5ffbb"});
  274. settingForPie.sectorStyleByFields.push({fillColor: "#bbe3ff"});
  275. settingForPie.sectorStyleByFields.push({fillColor: "#d5ffbb"});
  276. // 重绘图层(图表)
  277. themeLayer.redraw();
  278. }
  279. // 获取指定范围内的随机数
  280. // min - 范围下限
  281. // max - 范围上限
  282. // decimalNum - 返回结果的小数位数。如果为 0,返回整数。
  283. function getRandomNumber(min, max, decimalNum) {
  284. var rNum = min + Math.random() * (max - min);
  285. if (decimalNum) {
  286. if (!isNaN(decimalNum)) {
  287. return rNum;
  288. }
  289. else {
  290. decimalNum = parseInt(decimalNum);
  291. }
  292. if (decimalNum === 0) {
  293. return Math.round(rNum);
  294. }
  295. else {
  296. return parseFloat(rNum).toFixed(decimalNum);
  297. }
  298. }
  299. else {
  300. return rNum;
  301. }
  302. }
  303. // 切换图表为柱状图
  304. function toBar() {
  305. themeLayer.chartsSetting = settingForBar;
  306. themeLayer.setOpacity(0.9);
  307. themeLayer.setChartsType("Bar");
  308. }
  309. // 切换图表为折线图
  310. function toLine() {
  311. themeLayer.chartsSetting = settingForLine;
  312. themeLayer.setOpacity(0.9);
  313. themeLayer.setChartsType("Line");
  314. }
  315. // 切换图表为饼图
  316. function toPie() {
  317. themeLayer.chartsSetting = settingForPie;
  318. themeLayer.setOpacity(1);
  319. themeLayer.setChartsType("Pie");
  320. }
  321. </script>
  322. </body>
  323. </html>