当前位置:   article > 正文

echarts地图下钻与回钻

地图下钻

        最近在项目实际业务中为了更清晰的展示各省市的数据,使用echarts实现了地图的下钻和回钻。里面加了实际业务,所以代码有些冗余。

  1. import * as echarts from 'echarts' //echarts引入
  2. import './china'
  3. // 引入省份经纬度
  4. import { geoCoordMap } from './geoMap'
  5. // 引入全国各市经纬度
  6. import { city } from './city'
  7. async initOptions() {
  8. await getResourceMapInfo(params).then((res) => {
  9. if (res.code == 0) {
  10. this.planData = []
  11. this.reaData = []
  12. this.show = true
  13. res.data.forEach((e) => {
  14. if (e.testRegionList.length > 0) {
  15. this.regionList.push(e.testRegionList)
  16. }
  17. })
  18. res.data.forEach((item) => {
  19. if (geoCoordMap[item.testProvince]) {
  20. //计划派工
  21. if (item.recordNum !== null) {
  22. this.planData.push({
  23. name: item.testProvince,
  24. value: geoCoordMap[item.testProvince].concat(item.recordNum),
  25. list: item.recordNumList,
  26. })
  27. }
  28. //实际派工
  29. if (item.realityNum !== null) {
  30. this.reaData.push({
  31. name: item.testProvince,
  32. value: geoCoordMap[item.testProvince].concat(item.realityNum),
  33. list: item.realityNumList,
  34. })
  35. }
  36. }
  37. })
  38. }
  39. })
  40. this.options = {
  41. // 背景颜色
  42. backgroundColor: '#404a59',
  43. // 地图配置
  44. geo: {
  45. map: 'china',
  46. zoom: 1.2,
  47. roam: false, //支持拖拽缩放
  48. scaleLimit: {
  49. //滚轮缩放的极限控制
  50. min: 1.2, //缩放最小大小
  51. max: 3, //缩放最大大小
  52. },
  53. label: {
  54. // 通常状态下的样式 (字体)
  55. normal: {
  56. show: true,
  57. textStyle: {
  58. color: '#fff',
  59. },
  60. },
  61. // 鼠标放上去的样式
  62. emphasis: {
  63. textStyle: {
  64. color: '#fff',
  65. },
  66. },
  67. },
  68. // 地图区域的样式设置
  69. itemStyle: {
  70. normal: {
  71. borderColor: 'rgba(147, 235, 248, 1)',
  72. borderWidth: 1,
  73. areaColor: {
  74. type: 'radial',
  75. x: 0.5,
  76. y: 0.5,
  77. r: 0.8,
  78. colorStops: [
  79. {
  80. offset: 0,
  81. color: '#4354b6', // 0% 处的颜色
  82. },
  83. {
  84. offset: 1,
  85. color: '#00b6fe', // 100% 处的颜色
  86. },
  87. ],
  88. globalCoord: false, // 缺省为 false
  89. },
  90. shadowColor: 'rgba(128, 217, 248, 1)',
  91. shadowOffsetX: -2,
  92. shadowOffsetY: 2,
  93. shadowBlur: 10,
  94. },
  95. // 鼠标放上去高亮的样式
  96. emphasis: {
  97. areaColor: '#389BB7',
  98. borderWidth: 0,
  99. },
  100. },
  101. },
  102. legend: {
  103. show: true,
  104. bottom: '6%',
  105. left: '8%',
  106. orient: 'vertical',
  107. padding: 20,
  108. backgroundColor: 'rgba(236,246,255,0.30)',
  109. borderWidth: 1,
  110. borderColor: '#e9f3fb',
  111. itemGap: 0,
  112. itemWidth: 25,
  113. itemHeight: 25,
  114. symbolKeepAspect: false,
  115. inactiveColor: '#999999',
  116. textStyle: {
  117. fontSize: 16,
  118. color: '#',
  119. },
  120. data: [
  121. {
  122. name: '计划派工人数',
  123. icon: 'pin',
  124. },
  125. {
  126. name: '实际派工人数',
  127. icon: 'pin',
  128. },
  129. ],
  130. },
  131. // 数值
  132. series: [
  133. {
  134. type: 'map',
  135. name: 'map',
  136. aspectScale: 0.75,
  137. layoutCenter: ['50%', '50%'], //地图位置
  138. layoutSize: '125%',
  139. roam: true,
  140. geoIndex: 0,
  141. label: {
  142. show: false,
  143. },
  144. showLegendSymbol: false,
  145. },
  146. {
  147. name: '计划派工人数',
  148. type: 'scatter',
  149. coordinateSystem: 'geo',
  150. data: this.planData,
  151. symbol: 'pin',
  152. symbolSize: [30, 30],
  153. symbolOffset: [-5, -5],
  154. label: {
  155. show: true,
  156. position: 'inside',
  157. color: '#333',
  158. formatter: function (params) {
  159. return params.value[2]
  160. },
  161. },
  162. },
  163. {
  164. name: '实际派工人数',
  165. type: 'scatter',
  166. coordinateSystem: 'geo',
  167. data: this.reaData,
  168. symbol: 'pin',
  169. symbolSize: [30, 30],
  170. symbolOffset: [10, 10],
  171. label: {
  172. show: true,
  173. position: 'inside',
  174. color: '#333',
  175. formatter: function (params) {
  176. return params.value[2]
  177. },
  178. },
  179. },
  180. ],
  181. }
  182. if (this.charts) {
  183. this.charts.setOption(this.options)
  184. }
  185. this.allCoutryPlanData = this.planData
  186. this.allCoutryReaData = this.reaData
  187. },
  188. drawChart() {
  189. this.charts = echarts.init(document.getElementById('china'))
  190. this.charts.setOption(this.options)
  191. // 鼠标单击地图取消高亮效果
  192. this.charts.on('click', async (params) => {
  193. console.log(params)
  194. if (params.componentSubType == 'scatter') {
  195. this.changeTable = true
  196. console.log(11111)
  197. this.data = params.data.list
  198. this.data = mprowTable(this.data, 'testRegion')
  199. this.province = params.name
  200. this.peopleDesc = params.seriesName
  201. this.peopleNum = params.value[2]
  202. if (params.seriesName == '实际派工人数') {
  203. this.typeNum = 2
  204. this.columns[4].title = '实际进场时间'
  205. this.columns[5].title = '实际离场时间'
  206. } else {
  207. this.typeNum = 1
  208. this.columns[4].title = '计划进场时间'
  209. this.columns[5].title = '计划离场时间'
  210. }
  211. }
  212. await this.charts.dispatchAction({
  213. type: 'unselect',
  214. name: params.name,
  215. })
  216. if (params.componentSubType == 'map') {
  217. console.log(this.regionList, 'regionList')
  218. this.selectRegion = []
  219. let index = this.regionList.findIndex((item) => item[0].testProvince == params.name)
  220. this.selectRegion = this.regionList[index]
  221. this.goDown(params.name)
  222. }
  223. })
  224. },
  225. async getMapJson(name) {
  226. const jsonData = await import('./json/' + name + '.json')
  227. return jsonData.default
  228. },
  229. // 地图下钻
  230. async goDown(name) {
  231. if (this.currentLevel != 2) {
  232. const mapname = name
  233. if (!echarts.getMap(name)) {
  234. const newMapJson = await this.getMapJson(name)
  235. echarts.registerMap(mapname, newMapJson)
  236. }
  237. this.options.geo.map = mapname
  238. this.options.series[0].map = mapname
  239. //然后重新绘制地图
  240. this.currentName = name
  241. this.history.push(this.currentName)
  242. //获取地图数据之后,修改地图options
  243. //需要改data
  244. console.log(this.selectRegion, 'this.selectRegion')
  245. this.options.series[1].data = []
  246. this.options.series[2].data = []
  247. if (this.selectRegion !== undefined) {
  248. this.planData = []
  249. this.reaData = []
  250. this.selectRegion.forEach((item) => {
  251. //计划派工
  252. if (item.recordNum !== null && item.recordNum !== 0) {
  253. console.log(11111)
  254. this.planData.push({
  255. name: item.testRegion,
  256. value: city[item.testRegion].concat(item.recordNum),
  257. list: item.recordNumList,
  258. })
  259. }
  260. //实际派工
  261. if (item.realityNum !== null && item.realityNum !== 0) {
  262. console.log(2222222)
  263. this.reaData.push({
  264. name: item.testRegion,
  265. value: city[item.testRegion].concat(item.realityNum),
  266. list: item.realityNumList,
  267. })
  268. }
  269. })
  270. this.options.series[1].data = this.planData
  271. this.options.series[2].data = this.reaData
  272. }
  273. console.log(this.options)
  274. this.charts.setOption(this.options)
  275. this.currentLevel += 1
  276. this.data = []
  277. this.province = ''
  278. }
  279. },
  280. // 地图回钻
  281. returnUpLevel() {
  282. //先判断history有没有数据,能不能返回
  283. if (this.history.length == 0) {
  284. return false
  285. }
  286. //取出要返回的那个名字
  287. const name = this.history.pop()
  288. const mapname = 'china'
  289. // const currentJson = this.echarts.getMap(mapname).geoJson
  290. //修改地图配置重新绘制地图
  291. this.options.geo.map = mapname
  292. this.options.series[0].map = mapname
  293. //修改data
  294. this.options.series[1].data = this.allCoutryPlanData
  295. this.options.series[2].data = this.allCoutryReaData
  296. this.charts.setOption(this.options)
  297. //修改当前的层级,名字
  298. this.currentName = name
  299. this.currentLevel -= 1
  300. this.data = []
  301. this.province = ''
  302. },

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