当前位置:   article > 正文

Vue中使用Echarts构建3D地球_echarts 3d地球科技蓝

echarts 3d地球科技蓝

在main.js引入echarts

  1. //引入echarts
  2. import * as echarts from 'echarts'
  3. //vue全局注入echarts
  4. Vue.prototype.$echarts = echarts;
使用echart的3d功能需要另外安装echarts-gl  
安装指令: 
npm install echarts-gl

直接上代码:

  1. <template>
  2. <div class="chart-container" style="width: 100%;">
  3. <el-row >
  4. <el-col :span="24">
  5. <div id="chartMaine3" style="width:100%; height:560px;"></div>
  6. </el-col>
  7. </el-row>
  8. </div>
  9. </template>
  10. <script>
  11. // 使用echart的3d功能需要另外安装echarts-gl 安装指令: cnpm install echarts-gl
  12. import 'echarts-gl'
  13. export default {
  14. name: "Demo2",
  15. data() {
  16. return {
  17. chartMaine3:null,
  18. }
  19. },
  20. methods: {
  21. drawMaine3Chart(){
  22. this.chartMaine3 = this.$echarts.init(document.getElementById('chartMaine3'));
  23. var ROOT_PATH = 'https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/examples';
  24. this.$axios({
  25. url: 'https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/examples/data-gl/asset/data/flights.json',
  26. headers:'',
  27. data: {},
  28. method: 'get',
  29. success: (data) => {
  30. var airports = data.airports.map(function (item) {
  31. return {
  32. coord: [item[3], item[4]]
  33. };
  34. });
  35. function getAirportCoord(idx) {
  36. return [data.airports[idx][3], data.airports[idx][4]];
  37. }
  38. // Route: [airlineIndex, sourceAirportIndex, destinationAirportIndex]
  39. var routesGroupByAirline = {};
  40. data.routes.forEach(function (route) {
  41. var airline = data.airlines[route[0]];
  42. var airlineName = airline[0];
  43. if (!routesGroupByAirline[airlineName]) {
  44. routesGroupByAirline[airlineName] = [];
  45. }
  46. routesGroupByAirline[airlineName].push(route);
  47. });
  48. var pointsData = [];
  49. data.routes.forEach(function (airline) {
  50. pointsData.push(getAirportCoord(airline[1]));
  51. pointsData.push(getAirportCoord(airline[2]));
  52. });
  53. var series = data.airlines
  54. .map(function (airline) {
  55. var airlineName = airline[0];
  56. var routes = routesGroupByAirline[airlineName];
  57. if (!routes) {
  58. return null;
  59. }
  60. return {
  61. type: 'lines3D',
  62. name: airlineName,
  63. effect: {
  64. show: true,
  65. trailWidth: 2,
  66. trailLength: 0.15,
  67. trailOpacity: 1,
  68. trailColor: 'rgb(30, 30, 60)'
  69. },
  70. lineStyle: {
  71. width: 1,
  72. color: 'rgb(50, 50, 150)',
  73. // color: 'rgb(118, 233, 241)',
  74. opacity: 0.1
  75. },
  76. blendMode: 'lighter',
  77. data: routes.map(function (item) {
  78. return [airports[item[1]].coord, airports[item[2]].coord];
  79. })
  80. };
  81. })
  82. .filter(function (series) {
  83. return !!series;
  84. });
  85. series.push({
  86. type: 'scatter3D',
  87. coordinateSystem: 'globe',
  88. blendMode: 'lighter',
  89. symbolSize: 2,
  90. itemStyle: {
  91. color: 'rgb(50, 50, 150)',
  92. opacity: 0.2
  93. },
  94. data: pointsData
  95. });
  96. this.chartMaine3.setOption({
  97. legend: {
  98. selectedMode: 'single',
  99. left: 'left',
  100. data: Object.keys(routesGroupByAirline),
  101. orient: 'vertical',
  102. textStyle: {
  103. color: '#fff'
  104. }
  105. },
  106. globe: {
  107. environment: ROOT_PATH + '/data-gl/asset/starfield.jpg',
  108. heightTexture:
  109. ROOT_PATH + '/data-gl/asset/bathymetry_bw_composite_4k.jpg',
  110. displacementScale: 0.1,
  111. displacementQuality: 'high',
  112. baseColor: '#000',
  113. shading: 'realistic',
  114. realisticMaterial: {
  115. roughness: 0.2,
  116. metalness: 0
  117. },
  118. postEffect: {
  119. enable: true,
  120. depthOfField: {
  121. enable: false,
  122. focalDistance: 150
  123. }
  124. },
  125. temporalSuperSampling: {
  126. enable: true
  127. },
  128. light: {
  129. ambient: {
  130. intensity: 0
  131. },
  132. main: {
  133. intensity: 0.1,
  134. shadow: false
  135. },
  136. ambientCubemap: {
  137. texture: ROOT_PATH + '/data-gl/asset/lake.hdr',
  138. exposure: 1,
  139. diffuseIntensity: 0.5,
  140. specularIntensity: 2
  141. }
  142. },
  143. viewControl: {
  144. autoRotate: false
  145. },
  146. silent: true
  147. },
  148. series: series
  149. });
  150. window.addEventListener('keydown', function () {
  151. series.forEach(function (series, idx) {
  152. this.chartMaine3.dispatchAction({
  153. type: 'lines3DToggleEffect',
  154. seriesIndex: idx
  155. });
  156. });
  157. });
  158. },
  159. error:(e)=>{
  160. }
  161. })
  162. },
  163. drawCharts() {
  164. this.drawMaine3Chart()
  165. },
  166. },
  167. mounted: function () {
  168. this.drawCharts()
  169. },
  170. updated: function () {
  171. this.drawCharts()
  172. }
  173. }
  174. </script>
  175. <style scoped>
  176. .chart-container {
  177. background-color:white;
  178. height: 100%;
  179. width: 100%;
  180. }
  181. .el-col {
  182. padding: 30px;
  183. }
  184. </style>

效果图:

 

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

闽ICP备14008679号