f40_cylinder_gdp.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>统计柱状图 </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,bootstrap,layer,haoutil,echarts-gl,turf,mars3d"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. <style>
  30. .gdpView {
  31. min-width: 90px;
  32. min-height: 35px;
  33. position: absolute;
  34. left: 16px;
  35. bottom: 31px;
  36. cursor: default;
  37. border-radius: 4px;
  38. opacity: 0.96;
  39. border: 1px solid rgba(35, 40, 44, 0.226);
  40. box-shadow: 0px 2px 21px 0px rgba(35, 40, 44, 0.226);
  41. border-radius: 4px;
  42. box-sizing: border-box;
  43. background: rgba(2, 79, 128, 0.425);
  44. }
  45. .gdpView::before {
  46. content: "";
  47. width: calc(100% + 22px);
  48. height: 39px;
  49. position: absolute;
  50. bottom: -39px;
  51. left: -22px;
  52. background: url(./img/marker/popupLbl.png) 0px 0px no-repeat;
  53. background-position: 0px 0px;
  54. }
  55. .gdpCharts {
  56. width: 470px;
  57. height: 275px;
  58. text-align: center;
  59. padding: 1px;
  60. font-size: 14px;
  61. font-family: "MicrosoftYaHei";
  62. font-weight: 400;
  63. color: #ffffff;
  64. line-height: 33px;
  65. /* border: 1px solid #ffffff4f; */
  66. -webkit-box-sizing: border-box;
  67. box-sizing: border-box;
  68. white-space: nowrap;
  69. }
  70. .gdpCharts::before {
  71. content: "";
  72. width: 100%;
  73. height: 40px;
  74. position: absolute;
  75. left: 0;
  76. background-color: #1f3f8e;
  77. }
  78. .btnClosePopup {
  79. position: absolute;
  80. right: 5px;
  81. top: 2px;
  82. width: 30px;
  83. height: 40px;
  84. font-size: 25px;
  85. color: white;
  86. border: none;
  87. background-color: #1f3f8e !important;
  88. }
  89. </style>
  90. </head>
  91. <body class="dark">
  92. <!--加载前进行操作提示,优化用户体验-->
  93. <div id="mask" class="signmask" onclick="removeMask()"></div>
  94. <div id="mars3dContainer" class="mars3d-container"></div>
  95. <script src="./js/common.js"></script>
  96. <script type="text/javascript">
  97. "use script"; //开发环境建议开启严格模式
  98. var map;
  99. var graphicLayer;
  100. var barColors = ["#00fdcf", "#63AEFF", "#FFB861", "#FF6D5D"];
  101. function initMap(options) {
  102. //合并属性参数,可覆盖config.json中的对应配置
  103. var mapOptions = mars3d.Util.merge(options, {
  104. scene: {
  105. center: { lat: 30.286465, lng: 117.620524, alt: 510892, heading: 358, pitch: -50 },
  106. },
  107. layers: [
  108. {
  109. id: 1987,
  110. type: "geojson",
  111. name: "淮海经济区11市",
  112. url: "//data.mars3d.cn/file/geojson/huaihai.json",
  113. symbol: {
  114. styleOptions: {
  115. materialType: mars3d.MaterialType.PolyGradient,
  116. color: "#3388cc",
  117. opacity: 0.7,
  118. alphaPower: 1.3,
  119. length: "{gdp}",
  120. },
  121. styleField: "Name",
  122. styleFieldOptions: {
  123. 济宁市: { color: "#D4AACE" },
  124. 临沂市: { color: "#8DC763" },
  125. 菏泽市: { color: "#F7F39A" },
  126. 枣庄市: { color: "#F7F39A" },
  127. 徐州市: { color: "#96F0F1" },
  128. 宿迁市: { color: "#EAC9A8" },
  129. 连云港市: { color: "#F7F39A" },
  130. 商丘市: { color: "#D4AACE" },
  131. 宿州市: { color: "#8DC763" },
  132. 亳州市: { color: "#96F0F1" },
  133. 淮北市: { color: "#EAC9A8" },
  134. },
  135. },
  136. show: true,
  137. },
  138. ],
  139. });
  140. //创建三维地球场景
  141. map = new mars3d.Map("mars3dContainer", mapOptions);
  142. map.basemap = 2017; //蓝色底图
  143. //创建Graphic图层
  144. graphicLayer = new mars3d.layer.GraphicLayer();
  145. map.addLayer(graphicLayer);
  146. queryHuaiHaiApiData()
  147. .then(function (res) {
  148. conventChartsData(res.data); //单击显示的popup
  149. showYearZT(res.data); //柱状图
  150. bindHaihuaiPopup();
  151. })
  152. .catch(function () {
  153. haoutil.msg("获取信息失败,请稍候再试");
  154. });
  155. map.on(mars3d.EventType.load, function (event) {
  156. console.log("矢量数据对象加载完成", event);
  157. });
  158. }
  159. // 访问后端接口,取数据
  160. function queryHuaiHaiApiData() {
  161. return new Promise(function (resolve, reject) {
  162. $.ajax({
  163. url: "//data.mars3d.cn/file/apidemo/huaihai-jj.json",
  164. type: "get",
  165. dataType: "json",
  166. success: function (result) {
  167. resolve(result);
  168. },
  169. error: function (data) {
  170. reject(data);
  171. },
  172. });
  173. });
  174. }
  175. /**
  176. * 展示某年的椎体
  177. */
  178. function showYearZT(data) {
  179. const yearArr = Object.keys(data);
  180. let arr = data[yearArr[0]];
  181. for (let i = 0; i < arr.length; i += 1) {
  182. const attr = arr[i];
  183. const jwd = getJWDByName(attr["name"]);
  184. const num1 = attr["第一产业"];
  185. const num2 = attr["第二产业"];
  186. const num3 = attr["第三产业"];
  187. const numall = Number(num1 + num2 + num3).toFixed(2);
  188. const html = `${attr["name"]}<br/>
  189. <span style="color:#63AEFF">第一产业:${num1}</span><br/>
  190. <span style="color:#FFB861">第二产业:${num2}</span><br/>
  191. <span style="color:#FF6D5D">第三产业:${num3}</span>`;
  192. var height1 = Math.floor(num1 * 10);
  193. var height2 = Math.floor(num2 * 10);
  194. var height3 = Math.floor(num3 * 10);
  195. var p1 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 / 2);
  196. var p2 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 + height2 / 2);
  197. var p3 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 + height2 + height1 / 2);
  198. // 添加柱体
  199. createZT(p1, height3, "#63AEFF", html);
  200. createZT(p2, height2, "#FFB861", html);
  201. createZT(p3, height1, "#FF6D5D", html);
  202. // 添加文字
  203. var primitive = new mars3d.graphic.LabelPrimitive({
  204. position: Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height1 + height2 + height3),
  205. style: {
  206. text: numall,
  207. font_size: 18,
  208. font_family: "楷体",
  209. color: "#00ff00",
  210. outline: true,
  211. outlineColor: "#000000",
  212. outlineWidth: 1,
  213. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  214. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  215. pixelOffset: new Cesium.Cartesian2(0, -20),
  216. },
  217. });
  218. graphicLayer.addGraphic(primitive);
  219. primitive.bindTooltip(html);
  220. }
  221. }
  222. /** 创建柱体 */
  223. function createZT(position, len, color, html) {
  224. var graphic = new mars3d.graphic.CylinderEntity({
  225. position: position,
  226. style: {
  227. length: len,
  228. topRadius: 6000.0,
  229. bottomRadius: 6000.0,
  230. color: color,
  231. },
  232. });
  233. graphicLayer.addGraphic(graphic);
  234. graphic.bindTooltip(html);
  235. // graphic._position_show = position
  236. // graphic._length_show = len
  237. return graphic;
  238. }
  239. var cityPosition = [
  240. { name: "亳州", jwd: [116.203602, 33.496075] },
  241. { name: "商丘", jwd: [115.871509, 34.297084] },
  242. { name: "淮北", jwd: [116.688413, 33.689214] },
  243. { name: "宿州", jwd: [117.234682, 33.740035] },
  244. { name: "徐州", jwd: [117.70509, 34.350708] },
  245. { name: "宿迁", jwd: [118.559349, 33.807355] },
  246. { name: "连云港", jwd: [118.875445, 34.619808] },
  247. { name: "临沂", jwd: [118.026908, 35.262767] },
  248. { name: "枣庄", jwd: [117.320268, 35.072555] },
  249. { name: "济宁", jwd: [116.856599, 35.500232] },
  250. { name: "菏泽", jwd: [115.716086, 35.05629] },
  251. ];
  252. /**
  253. * 根据名称获取坐标
  254. */
  255. function getJWDByName(name) {
  256. for (let i = 0; i < cityPosition.length; i += 1) {
  257. const item = cityPosition[i];
  258. if (item.name === name) {
  259. return item.jwd;
  260. }
  261. }
  262. return [];
  263. }
  264. //================以下是单击显示的echarst图表的相关代码===============
  265. let arrYear;
  266. let objCity = {};
  267. // 转换值
  268. function conventChartsData(arrOld) {
  269. console.log("转换前数据=>", arrOld);
  270. arrYear = Object.keys(arrOld); //[年份]
  271. objCity = {}; //十一个城市对应的各年度数据
  272. for (let a = 0; a < arrYear.length; a++) {
  273. let arrCity = arrOld[arrYear[a]]; //指定某年的11个城市对应数据
  274. // 循环十次
  275. for (let b = 0; b < arrCity.length; b++) {
  276. let item = arrCity[b];
  277. if (!objCity[item.code]) {
  278. objCity[item.code] = [];
  279. }
  280. objCity[item.code].push(item.GDP);
  281. }
  282. }
  283. console.log("转换完成的数据=>", objCity);
  284. }
  285. function bindHaihuaiPopup() {
  286. var layerHuaihai = map.getLayer(1987, "id"); // 获取config.json中对应图层
  287. //在layer上绑定Popup单击弹窗
  288. layerHuaihai.bindPopup(
  289. `<div class="gdpView">
  290. <div class="gdpCharts" id="gdpCharts"></div>
  291. <input type="button" class="btnClosePopup closeButton" value="×" />
  292. </div>`,
  293. {
  294. template: false,
  295. horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
  296. verticalOrigin: Cesium.VerticalOrigin.CENTER,
  297. }
  298. );
  299. let gdpCharts;
  300. layerHuaihai.on(mars3d.EventType.popupOpen, function (event) {
  301. let container = event.container; //popup对应的DOM
  302. console.log("图层上打开了popup", container);
  303. let option = getCityChartsOptions(event.graphic?.attr);
  304. if (!option) {
  305. return;
  306. }
  307. gdpCharts = echarts.init(container.querySelector("#gdpCharts"));
  308. gdpCharts.setOption(option);
  309. });
  310. layerHuaihai.on(mars3d.EventType.popupClose, function (event) {
  311. let container = event.container; //popup对应的DOM
  312. console.log("图层上移除了popup", container);
  313. gdpCharts.dispose();
  314. gdpCharts = null;
  315. });
  316. }
  317. function getCityChartsOptions(attr) {
  318. let arrGDPvalues = objCity[attr.code];
  319. if (!arrGDPvalues) {
  320. haoutil.msg(attr.Name + " 无经济数据");
  321. return;
  322. }
  323. // arrGDPvalues 是点击的城市的数值,需要以[b,0,value]的方式重新排列
  324. let arrData = [];
  325. for (let b = 0; b < arrGDPvalues.length; b++) {
  326. arrData[b] = [b, 0, arrGDPvalues[b]];
  327. }
  328. var option = {
  329. visualMap: {
  330. max: 4500,
  331. show: false,
  332. inRange: {
  333. color: ["#32C5E9", "#67E0E3", "#FFDB5C", "#37A2DA", "#9FE6B8"],
  334. },
  335. },
  336. title: {
  337. text: attr.Name + " 近五年GDP(亿元)",
  338. top: "10",
  339. left: "5",
  340. textStyle: {
  341. color: "white",
  342. fontSize: "17",
  343. fontWidth: "normal",
  344. },
  345. },
  346. tooltip: {
  347. show: "true",
  348. trigger: "item",
  349. showContent: "true",
  350. position: "top",
  351. textStyle: {
  352. fontSize: "12",
  353. color: "black",
  354. },
  355. formatter: function formatter(params) {
  356. return "GDP:" + params.data[2];
  357. },
  358. },
  359. // x轴是横向,是时间
  360. xAxis3D: {
  361. type: "category",
  362. data: arrYear,
  363. nameTextStyle: {
  364. color: "rgb(0, 0, 0, 0.1)",
  365. },
  366. // splitLine不可见时仅仅线不可见
  367. splitLine: {
  368. show: false,
  369. },
  370. },
  371. // y轴被缩小
  372. yAxis3D: {
  373. type: "category",
  374. data: [" "],
  375. nameTextStyle: {
  376. color: "rgb(0, 0, 0, 0.1)",
  377. },
  378. splitLine: {
  379. show: false,
  380. },
  381. },
  382. // z轴是gdp的值
  383. zAxis3D: {
  384. type: "value",
  385. name: "GDP",
  386. axisLine: {
  387. lineStyle: {
  388. color: "rgb(0, 0, 0, 0.1)",
  389. },
  390. },
  391. nameTextStyle: {
  392. color: "white",
  393. fontSize: "18",
  394. },
  395. nameGap: "50",
  396. },
  397. grid3D: {
  398. boxWidth: 180, //缩大放小x轴
  399. boxDepth: 10, // 缩大放小y轴
  400. top: "20",
  401. // left: '50',
  402. // 視角的設置
  403. viewControl: {
  404. alpha: 8,
  405. beta: 0,
  406. distance: 162,
  407. center: [-20, 0, 0],
  408. },
  409. axisLabel: {
  410. textStyle: {
  411. color: "white",
  412. fontSize: 15,
  413. },
  414. },
  415. axisPointer: {
  416. //坐标轴指示线,就是鼠标移入时,指向x轴,y轴的线
  417. show: false,
  418. },
  419. },
  420. series: [
  421. {
  422. type: "bar3D",
  423. data: arrData,
  424. shading: "lambert",
  425. label: {
  426. position: "top",
  427. show: true,
  428. color: "white",
  429. },
  430. emphasis: {
  431. label: {
  432. textStyle: {
  433. color: "white",
  434. fontSize: "18",
  435. },
  436. },
  437. },
  438. },
  439. ],
  440. };
  441. return option;
  442. }
  443. </script>
  444. </body>
  445. </html>