当前位置:   article > 正文

vue echart地图划分大区_vue实现echarts的全国地图点击某个区域地图切换到这个区域的地图

vue实现echarts的全国地图点击某个区域地图切换到这个区域的地图

首先上图:

引入china.json文件: https://download.csdn.net/download/liming1016/12487994

首先要通过npm安装echart,.vue文件同目录引入china.json

  1. <template>
  2. <div class="app-container">
  3. <div class="chart-wrapper">
  4. <div id="chinaMap" style="width: 100%;height: 500px"></div>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import 'echarts/map/js/china.js'
  10. import echarts from 'echarts'
  11. import chinaJson from './china.json' // 这个是json引用
  12. export default {
  13. name: '',
  14. data () {
  15. return {
  16. chinaMapData: [
  17. {name: '东北', value: this.randomValue()},
  18. {name: '华北', value: this.randomValue()},
  19. {name: '西北', value: this.randomValue()},
  20. {name: '西南', value: this.randomValue()},
  21. {name: '华中', value: this.randomValue()},
  22. {name: '华南', value: this.randomValue()},
  23. {name: '华东', value: this.randomValue()}
  24. ],
  25. chinaDaquGeo: {}
  26. }
  27. },
  28. methods:{
  29. progressFormat(percentage){
  30. return ""
  31. },
  32. randomValue() {
  33. return Math.round(Math.random()*1000);
  34. },
  35. drawChinaMap(id){
  36. this.mapCharts = echarts.init(document.getElementById(id));
  37. var option = {
  38. tooltip: {
  39. formatter:function(params,ticket, callback){
  40. return params.seriesName+'<br />'+params.name+':'+params.value
  41. }//数据格式化
  42. },
  43. visualMap: {
  44. min: 0,
  45. max: 1500,
  46. left: 'left',
  47. top: 'bottom',
  48. type:"piecewise",
  49. //text: ['高','低'],//取值范围的文字
  50. inRange: {
  51. color: ['#e0ffff', '#006edd']//取值范围的颜色
  52. },
  53. show:true//图注
  54. },
  55. geo: {
  56. map: 'china',
  57. roam: true,//不开启缩放和平移
  58. zoom: 1,//视角缩放比例
  59. center: [108.5525, 34.3227],
  60. label: {
  61. normal: {
  62. show: true,
  63. fontSize:'10',
  64. color: 'rgba(0,0,0,0.7)'
  65. }
  66. },
  67. itemStyle: {
  68. normal:{
  69. borderColor: 'rgba(0, 0, 0, 0.2)'
  70. },
  71. emphasis:{
  72. areaColor: '#F3B329',//鼠标选择区域颜色
  73. shadowOffsetX: 0,
  74. shadowOffsetY: 0,
  75. shadowBlur: 20,
  76. borderWidth: 0,
  77. shadowColor: 'rgba(0, 0, 0, 0.5)'
  78. }
  79. }
  80. },
  81. series : [
  82. {
  83. name: '事业部',
  84. type: 'map',
  85. geoIndex: 0,
  86. data:this.chinaMapData
  87. }
  88. ]
  89. };
  90. this.mapCharts.setOption(option);
  91. /*this.mapCharts.on('click', function (params) {
  92. alert(params.name);
  93. });*/
  94. },
  95. //划分大区
  96. mergeProvinces(chinaJson, names, properties) {//合并大区里省份的coordinates
  97. var features = chinaJson.features;
  98. var polygons = [];
  99. for (var i = 0; i < names.length; i++) {
  100. var polygonsCoordinates = [];
  101. for (var z = 0; z < names[i].length; z++) {
  102. for (var j = 0; j < features.length; j++) {
  103. if (features[j].properties.name == names[i][z]) {
  104. if (features[j].geometry.coordinates[0].constructor == String) {//合并coordinates
  105. for (var l = 0; l < features[j].geometry.coordinates.length; l++) {
  106. polygonsCoordinates.push(features[j].geometry.coordinates[l]);
  107. }
  108. } else if (features[j].geometry.coordinates[0].constructor == Array) {
  109. for (var k = 0; k < features[j].geometry.coordinates.length; k++) {
  110. for (var d = 0; d < features[j].geometry.coordinates[k].length; d++) {
  111. polygonsCoordinates.push(features[j].geometry.coordinates[k][d]);
  112. }
  113. }
  114. }
  115. break;
  116. }
  117. }
  118. }
  119. polygons.push(polygonsCoordinates);
  120. }
  121. // 构建新的合并区域
  122. var features = [];
  123. for(var a = 0;a<names.length;a++){
  124. var feature = {
  125. id: features.length.toString(),
  126. type: "FeatureCollection",
  127. geometry: {
  128. type: "Polygon",
  129. coordinates: polygons[a]
  130. },
  131. properties: {
  132. name: properties.name[a] || "",
  133. childNum:polygons[a].length
  134. }
  135. };
  136. if(properties.cp[a]) {
  137. feature.properties.cp = properties.cp[a];
  138. }
  139. // 将新的合并区域添加到地图中
  140. features.push(feature);
  141. }
  142. this.chinaDaquGeo.type = "FeatureCollection";
  143. this.chinaDaquGeo.features = features;
  144. },
  145. repRegionStrategy(){
  146. var params = {
  147. names: [//把各个大区的省份用二维数组分开
  148. ['北京','天津','河北','山西','内蒙古'],
  149. ["黑龙江","吉林","辽宁"],
  150. ['山东','江苏','安徽','江西','浙江','福建','上海','台湾'],
  151. ['河南','湖北','湖南'],
  152. ['广东','广西','海南','香港','澳门'],
  153. ['重庆','四川','云南','西藏','贵州'],
  154. ['陕西','甘肃','青海','宁夏','新疆']
  155. ],
  156. properties: {//自定义大区的名字,要和上面的大区省份一一对应
  157. name: ['华北','东北','华东','华中','华南','西南','西北'],
  158. cp: [//经纬度可以自己随意定义
  159. [116.24,39.54],
  160. [126.32,43.50],
  161. [121.28,31.13],
  162. [114.20,30.32],
  163. [113.15,23.08],
  164. [104.04,30.39],
  165. [103.49,36.03]
  166. ]
  167. }
  168. };
  169. this.mergeProvinces(chinaJson, params.names, params.properties);
  170. }
  171. },
  172. //调用
  173. created() {
  174. this.repRegionStrategy()
  175. // 注册地图
  176. this.echarts.registerMap('china', this.chinaDaquGeo) // 如果是js引入就不需要这一行了
  177. },
  178. mounted(){
  179. this.$nextTick(function() {
  180. this.drawChinaMap('chinaMap')
  181. })
  182. }
  183. }
  184. </script>
  185. <style>
  186. </style>

 

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