当前位置:   article > 正文

vue 使用echarts地图实现全国地图_echarts.getmap

echarts.getmap

代码参考来自 <https://www.cnblogs.com/lude1994/p/12482355.html>

json数据自己收集保存,包含世界坐标、全国坐标及各省份内的坐标

附代码及json数据下载:https://download.csdn.net/download/loa_loa/19339991?spm=1001.2014.3001.5503

 

地图json数据:

http://datav.aliyun.com/tools/atlas/#&lat=30.332329214580188&lng=106.72278672066881&zoom=3.5

https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json

 

1.安装echarts

npm install echarts --save

 

2.main.js中引入

  1. import echarts from 'echarts';
  2. //引入遇到报错 "export ‘default’ (imported as ‘echarts’) was not found in ‘echarts’
  3. //新版本需要使用该代码引入 :const echarts = require('echarts');
  4. import 'echarts/map/js/china.js'; //node_modules中 官方依赖已不提供map文件 需要自行导入其中
  5. Vue.prototype.$echarts = echarts

3.在页面中使用:

<div ref="map" style="height:300px;" ></div>

4.将函数写在methods中,然后再mounted中调用

js函数方法:

  1. getmap() {
  2. // 基于准备好的dom(myEchartPillar),初始化echarts实例
  3. let myChart = this.$echarts.init(this.$refs.map);
  4. this.$echarts.registerMap('china',china);
  5. var mapName = 'china'
  6. var data = [
  7. {
  8. code: "10",
  9. name: "其他",
  10. value: 5
  11. },
  12. {
  13. code: "11",
  14. name: "北京市",
  15. value: 26
  16. },
  17. {
  18. code: "12",
  19. name: "天津市",
  20. value: 1
  21. },
  22. {
  23. code: "13",
  24. name: "河北省",
  25. value: 3
  26. },
  27. {
  28. code: "31",
  29. name: "上海市",
  30. value: 6
  31. },
  32. {
  33. code: "32",
  34. name: "江苏省",
  35. value: 8
  36. },
  37. {
  38. code: "33",
  39. name: "浙江省",
  40. value: 3
  41. },
  42. {
  43. code: "34",
  44. name: "安徽省",
  45. value: 2
  46. },
  47. {
  48. code: "35",
  49. name: "福建省",
  50. value: 1
  51. },
  52. {
  53. code: "37",
  54. name: "山东省",
  55. value: 4
  56. },
  57. {
  58. code: "41",
  59. name: "河南省",
  60. value: 3
  61. },
  62. {
  63. code: "42",
  64. name: "湖北省",
  65. value: 3
  66. },
  67. {
  68. code: "44",
  69. name: "广东省",
  70. value: 25
  71. },
  72. {
  73. code: "61",
  74. name: "陕西省",
  75. value: 3
  76. }
  77. ];
  78. var geoCoordMap = {};
  79. var toolTipData= [
  80. {
  81. code: "10",
  82. name: "其他",
  83. value: 5
  84. },
  85. {
  86. code: "11",
  87. name: "北京市",
  88. value: 26
  89. },
  90. {
  91. code: "12",
  92. name: "天津市",
  93. value: 1
  94. },
  95. {
  96. code: "13",
  97. name: "河北省",
  98. value: 3
  99. },
  100. {
  101. code: "31",
  102. name: "上海市",
  103. value: 6
  104. },
  105. {
  106. code: "32",
  107. name: "江苏省",
  108. value: 8
  109. },
  110. {
  111. code: "33",
  112. name: "浙江省",
  113. value: 3
  114. },
  115. {
  116. code: "34",
  117. name: "安徽省",
  118. value: 2
  119. },
  120. {
  121. code: "35",
  122. name: "福建省",
  123. value: 1
  124. },
  125. {
  126. code: "37",
  127. name: "山东省",
  128. value: 4
  129. },
  130. {
  131. code: "41",
  132. name: "河南省",
  133. value: 3
  134. },
  135. {
  136. code: "42",
  137. name: "湖北省",
  138. value: 3
  139. },
  140. {
  141. code: "44",
  142. name: "广东省",
  143. value: 25
  144. },
  145. {
  146. code: "61",
  147. name: "陕西省",
  148. value: 3
  149. }
  150. ];
  151. /*获取地图数据*/
  152. myChart.showLoading();
  153. var mapFeatures = this.$echarts.getMap(mapName).geoJson.features;
  154. myChart.hideLoading();
  155. mapFeatures.forEach(function(v) {
  156. // 地区名称
  157. var name = v.properties.name;
  158. // 地区经纬度
  159. geoCoordMap[name] = v.properties.cp;
  160. });
  161. var max = 480,
  162. min = 9; // todo
  163. var maxSize4Pin = 100,
  164. minSize4Pin = 20;
  165. var convertData = function(data) {
  166. var res = [];
  167. for (var i = 0; i < data.length; i++) {
  168. var geoCoord = geoCoordMap[data[i].name];
  169. if (geoCoord) {
  170. res.push({
  171. name: data[i].name,
  172. value: geoCoord.concat(data[i].value),
  173. });
  174. }
  175. }
  176. return res;
  177. };
  178. //指定图表的配置项和数据,绘制图表
  179. myChart.setOption({
  180. tooltip: {
  181. trigger: 'item',
  182. formatter: function(params) {
  183. var toolTiphtml = ''
  184. if (typeof(params.value)[2] == "undefined") {
  185. for (let i = 0; i < toolTipData.length; i++) {
  186. if (params.name == toolTipData[i].name) {
  187. toolTiphtml += toolTipData[i].name + ':'+toolTipData[i].value
  188. }
  189. }
  190. console.log(toolTiphtml)
  191. // console.log(convertData(data))
  192. return toolTiphtml;
  193. } else {
  194. for (let i = 0; i < toolTipData.length; i++) {
  195. if (params.name == toolTipData[i].name) {
  196. toolTiphtml += toolTipData[i].name + ':'+toolTipData[i].value
  197. }
  198. }
  199. console.log(toolTiphtml)
  200. // console.log(convertData(data))
  201. return toolTiphtml;
  202. }
  203. }
  204. },
  205. // legend: {
  206. // orient: 'vertical',
  207. // y: 'bottom',
  208. // x: 'right',
  209. // data: ['credit_pm2.5'],
  210. // textStyle: {
  211. // color: '#fff'
  212. // }
  213. // },
  214. visualMap: {
  215. show: true,
  216. min: 0,
  217. max: 25,
  218. left: 'left',
  219. top: 'bottom',
  220. text: ['高', '低'], // 文本,默认为数值文本
  221. calculable: true,
  222. seriesIndex: [1],
  223. inRange: {
  224. color: ['#83c5ff', '#c6eb68'] // 蓝绿
  225. }
  226. },
  227. geo: {
  228. show: true,
  229. map: mapName,
  230. label: {
  231. normal: {
  232. show: false
  233. },
  234. emphasis: {
  235. show: false,
  236. }
  237. },
  238. roam: true,
  239. itemStyle: {
  240. normal: {
  241. areaColor: '#f8f8f8',
  242. borderColor: '#3B5077',
  243. },
  244. emphasis: {
  245. areaColor: '#2B91B7',
  246. }
  247. }
  248. },
  249. series: [{
  250. name: '散点',
  251. type: 'scatter',
  252. coordinateSystem: 'geo',
  253. data: convertData(data),
  254. symbolSize: function(val) {
  255. return val[2] / 10;
  256. },
  257. label: {
  258. normal: {
  259. formatter: '{b}',
  260. position: 'right',
  261. show: true
  262. },
  263. emphasis: {
  264. show: true
  265. }
  266. },
  267. itemStyle: {
  268. normal: {
  269. color: '#05C3F9'
  270. }
  271. }
  272. },
  273. {
  274. type: 'map',
  275. map: mapName,
  276. geoIndex: 0,
  277. aspectScale: 0.75, //长宽比
  278. showLegendSymbol: false, // 存在legend时显示
  279. label: {
  280. normal: {
  281. show: true
  282. },
  283. emphasis: {
  284. show: false,
  285. textStyle: {
  286. color: '#fff'
  287. }
  288. }
  289. },
  290. roam: true,
  291. itemStyle: {
  292. normal: {
  293. areaColor: '#031525',
  294. borderColor: '#3B5077',
  295. },
  296. emphasis: {
  297. areaColor: '#2B91B7'
  298. }
  299. },
  300. animation: false,
  301. data: data
  302. },
  303. {
  304. name: '点',
  305. type: 'scatter',
  306. coordinateSystem: 'geo',
  307. symbol: 'pin', //气泡
  308. symbolSize: function(val) {
  309. var a = (maxSize4Pin - minSize4Pin) / (max - min);
  310. var b = minSize4Pin - a * min;
  311. b = maxSize4Pin - a * max;
  312. return a * val[2] + b;
  313. },
  314. label: {
  315. normal: {
  316. show: true,
  317. textStyle: {
  318. color: '#fff',
  319. fontSize: 9,
  320. }
  321. }
  322. },
  323. itemStyle: {
  324. normal: {
  325. color: '#9c0a16', //标志颜色
  326. }
  327. },
  328. zlevel: 6,
  329. data: convertData(data),
  330. },
  331. {
  332. name: 'Top 5',
  333. type: 'effectScatter',
  334. coordinateSystem: 'geo',
  335. data: convertData(data.sort(function(a, b) {
  336. return b.value - a.value;
  337. }).slice(0, 5)),
  338. symbolSize: function(val) {
  339. return val[2] / 10;
  340. },
  341. showEffectOn: 'render',
  342. rippleEffect: {
  343. brushType: 'stroke'
  344. },
  345. hoverAnimation: true,
  346. label: {
  347. normal: {
  348. formatter: '{b}',
  349. position: 'right',
  350. show: true
  351. }
  352. },
  353. itemStyle: {
  354. normal: {
  355. color: 'yellow',
  356. shadowBlur: 10,
  357. shadowColor: 'yellow'
  358. }
  359. },
  360. zlevel: 1
  361. },
  362. ]
  363. });
  364. //解决自适应
  365. setTimeout(function() {
  366. window.addEventListener("resize", () => {
  367. myChart.resize();
  368. });
  369. }, 500);
  370. }

调用:

this.getmap();

 

5.效果图

 

 

 

 

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