当前位置:   article > 正文

echarts中绘制3D三维地球_echarts3d地图数据

echarts3d地图数据

简介

echarts中的三维地球,需要用到世界地图json数据,我把json文件放到我的资源中,有需要的自行下载。

安装插件

  1. // 安装echats
  2. npm install echarts --save
  3. npm install echarts-gl --save

项目中引用

1,引入安装的echarts插件

  1. import * as echarts from 'echarts';
  2. import 'echarts-gl';

2,引入世界地图json文件

import WorldJSON from './world.json'

3,echarts 注册世界地图

  1. //注册世界地图
  2. echarts.registerMap('world', WorldJSON);

绘制3d地球代码

  1. // 绘制3d echarts图表
  2. function draw3Dcharts() {
  3. let myChart = echarts.init(map.value);
  4. let geoCoordMap = {
  5. "海门": [121.15, 31.89],
  6. "鄂尔多斯": [109.781327, 39.608266],
  7. "菏泽": [115.480656, 35.23375],
  8. "合肥": [117.27, 31.86],
  9. "武汉": [114.31, 30.52],
  10. "大庆": [125.03, 46.58]
  11. };
  12. var alirl = [
  13. [[121.15, 31.89], [121.48, 31.22]],
  14. [[120.38, 37.35], [121.48, 31.22]],
  15. [[123.97, 47.33], [121.48, 31.22]],
  16. [[118.87, 42.28], [121.48, 31.22]],
  17. [[121.52, 36.89], [121.48, 31.22]],
  18. [[102.18, 38.52], [121.48, 31.22]],
  19. [[118.58, 24.93], [121.48, 31.22]],
  20. [[120.53, 36.86], [121.48, 31.22]],
  21. [[119.46, 35.42], [121.48, 31.22]],
  22. [[119.97, 35.88], [121.48, 31.22]],
  23. [[121.05, 32.08], [121.48, 31.22]],
  24. [[ 91.11, 29.97], [121.48, 31.22]]
  25. ]
  26. var convertData = function (data) {
  27. var res = [];
  28. for (var i = 0; i < data.length; i++) {
  29. var geoCoord = geoCoordMap[data[i].name];
  30. if (geoCoord) {
  31. res.push({
  32. name: data[i].name,
  33. value: geoCoord.concat(data[i].value)
  34. });
  35. }
  36. }
  37. return res;
  38. };
  39. var baseTexture = null
  40. // 生成球面纹理
  41. function getBaseTexture() {
  42. let canvas = document.createElement('canvas');
  43. baseTexture = echarts.init(canvas, null , {
  44. width: 4096,
  45. height: 2048,
  46. });
  47. baseTexture.setOption({
  48. backgroundColor: '#001213',
  49. series: [
  50. {
  51. type: 'map',
  52. map: 'world',
  53. left: 0,
  54. top: 0,
  55. right: 0,
  56. bottom: 0,
  57. roam:true,
  58. boundingCoords: [
  59. [-180, 90],
  60. [180, -90],
  61. ],
  62. label: {
  63. show: false,
  64. color: '#fff',
  65. fontSize: 20,
  66. },
  67. itemStyle: {
  68. areaColor: '#004444',
  69. borderColor: '#00cccc',
  70. borderWidth: 2,
  71. },
  72. },
  73. ],
  74. });
  75. drawEarth();
  76. }
  77. function drawEarth() {
  78. let option = {
  79. tooltip: {
  80. show: true,
  81. },
  82. globe: {
  83. silent: true,
  84. shading: 'color',
  85. environment: '#000',
  86. baseTexture: baseTexture,
  87. viewControl: {
  88. rotateSensitivity: 3, //鼠标旋转灵敏度,设置为0后无法旋转。
  89. zoomSensitivity: 0,//鼠标缩放灵敏度
  90. autoRotate: true,//自动旋转
  91. autoRotateAfterStill: 1,//鼠标停止后多久恢复旋转(为0时暂停后不恢复旋转)
  92. //alpha:160,//视角绕 x 轴,即上下旋转的角度
  93. //beta:-20,//视角绕 y 轴,即左右旋转的角度。
  94. // targetCoord: [75.508268, 18.247872] //定位到哪里
  95. // 定位到北京
  96. targetCoord: [116.46, 39.92],
  97. }
  98. },
  99. series: [
  100. //柱状图
  101. {
  102. type: "bar3D",
  103. coordinateSystem: 'globe',
  104. barSize: 0.5, //柱子粗细
  105. shading: 'lambert',
  106. opacity: 1,
  107. bevelSize: 0.2,
  108. itemStyle: {
  109. color: '#EBE806',
  110. opacity: 0.1
  111. },
  112. label: {
  113. show: false,
  114. formatter: '{b}'
  115. },
  116. data: convertData([
  117. {
  118. name: "海门",
  119. value: (Math.random() * 300).toFixed(2)
  120. }, {
  121. name: "鄂尔多斯",
  122. value: (Math.random() * 300).toFixed(2)
  123. }, {
  124. name: "招远",
  125. value: (Math.random() * 300).toFixed(2)
  126. }, {
  127. name: "舟山",
  128. value: (Math.random() * 300).toFixed(2)
  129. }, {
  130. name: "齐齐哈尔",
  131. value: (Math.random() * 300).toFixed(2)
  132. }, {
  133. name: "盐城",
  134. value: (Math.random() * 300).toFixed(2)
  135. }, {
  136. name: "赤峰",
  137. value: (Math.random() * 300).toFixed(2)
  138. }
  139. ]),
  140. },
  141. {
  142. name: 'lines3D',
  143. type: 'lines3D',
  144. coordinateSystem: 'globe',
  145. effect: {
  146. show: true,
  147. period: 2,
  148. trailWidth: 3,
  149. trailLength: 0.5,
  150. trailOpacity: 1,
  151. trailColor: '#0087f4'
  152. },
  153. blendMode: 'lighter',
  154. lineStyle: {
  155. // width: 2
  156. width: 1,
  157. color: '#0087f4',
  158. opacity: 0
  159. },
  160. data: [],
  161. silent: false,
  162. },
  163. {
  164. type: 'lines3D',
  165. coordinateSystem: 'globe',
  166. effect: {
  167. show: true,
  168. trailWidth: 5,
  169. trailOpacity: 1,
  170. trailLength: 0.2,
  171. constantSpeed: 5
  172. },
  173. blendMode: 'lighter',
  174. lineStyle: { //航线的视图效果
  175. color: '#EBE806',
  176. width: 1,
  177. opacity: 1
  178. },
  179. data: alirl
  180. }
  181. ]
  182. };
  183. for (let i = 0; i < 50; i++) {
  184. option.series[1].data = option.series[1].data.concat(rodamData())
  185. }
  186. myChart.clear();
  187. myChart.setOption(option, true);
  188. window.addEventListener('resize', () => {
  189. myChart.resize();
  190. });
  191. }
  192. function rodamData() {
  193. let longitude = 105.18
  194. let longitude2 = Math.random() * 360 - 180
  195. let latitude = 37.51
  196. let latitude2 = Math.random() * 180 - 90
  197. return {
  198. coords: [
  199. [longitude2, latitude2],
  200. [longitude, latitude]
  201. ],
  202. value: (Math.random() * 3000).toFixed(2)
  203. }
  204. }
  205. getBaseTexture();
  206. }

效果图展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/803326
推荐阅读
相关标签
  

闽ICP备14008679号