index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div
  3. style="width: 100%; height: 100%"
  4. class="bs-design-wrap bs-bar"
  5. >
  6. <div
  7. :id="`chart${config.code}`"
  8. style="width: 100%; height: 100%"
  9. />
  10. </div>
  11. </template>
  12. <script>
  13. import 'insert-css'
  14. import * as echarts from 'echarts'
  15. import {nameMap} from './json/mapData.js'
  16. import commonMixins from 'data-room-ui/js/mixins/commonMixins.js'
  17. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  18. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  19. export default {
  20. name: 'MapCharts',
  21. mixins: [paramsMixins, commonMixins, linkageMixins],
  22. props: {
  23. id: {
  24. type: String,
  25. default: ''
  26. },
  27. config: {
  28. type: Object,
  29. default: () => ({})
  30. }
  31. },
  32. data () {
  33. return {
  34. charts: null,
  35. hasData: false,
  36. level:''
  37. }
  38. },
  39. computed: {
  40. Data () {
  41. return JSON.parse(JSON.stringify(this.config))
  42. }
  43. },
  44. watch: {
  45. Data: {
  46. handler (newVal, oldVal) {
  47. if (newVal.w !== oldVal.w || newVal.h !== oldVal.h) {
  48. this.$nextTick(() => {
  49. this.charts.resize()
  50. })
  51. }
  52. },
  53. deep: true
  54. }
  55. },
  56. mounted () {
  57. this.chartInit()
  58. },
  59. beforeDestroy () {
  60. this.charts?.clear()
  61. },
  62. methods: {
  63. chartInit () {
  64. const config = this.config
  65. // key和code相等,说明是一进来刷新,调用list接口
  66. if (this.config.code === this.config.key || this.isPreview) {
  67. // 改变数据
  68. this.changeDataByCode(config).then((res) => {
  69. // 改变样式
  70. // config = this.changeStyle(res)
  71. this.newChart(config)
  72. }).catch(() => {})
  73. } else {
  74. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  75. this.changeData(config).then((res) => {
  76. // 初始化图表
  77. this.newChart(res)
  78. })
  79. }
  80. },
  81. dataFormatting (config, data) {
  82. config.option = {
  83. ...config.option,
  84. data: data?.data
  85. }
  86. return config
  87. },
  88. async newChart (config) {
  89. this.charts = echarts.init(
  90. document.getElementById(`chart${this.config.code}`)
  91. )
  92. this.level=config.customize.level
  93. const lines_coord = []
  94. let fromCoord=[]
  95. let coord=[]
  96. const mapUrl =config.customize.level==='world'?`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/worldMap/world.json`:`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/${config.customize.level}/${config.customize.dataMap}`
  97. this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true).then(res=>{
  98. this.config.option.data.forEach(val => {
  99. lines_coord.push({value:val.value,msg:{...val}, coords:[[val.lat1,val.lng1],[val.lat2,val.lng2]]})
  100. if(val.type==='move_in'){
  101. coord.push({name:val.from,value:[val.lat1,val.lng1,val.value],msg:{...val}})
  102. fromCoord.push({name:val.to,value:[val.lat2,val.lng2,val.value],msg:{...val}})
  103. }else{
  104. coord.push({name:val.to,value:[val.lat2,val.lng2,val.value],msg:{...val}})
  105. fromCoord.push({name:val.from,value:[val.lat1,val.lng1,val.value],msg:{...val}})
  106. }
  107. })
  108. echarts.registerMap(config.customize.scope, res)
  109. const option = {
  110. nameMap:config.customize.level=='world'?nameMap:'',
  111. graphic: [
  112. ],
  113. geo: {
  114. map: config.customize.scope,
  115. zlevel: 10,
  116. show:true,
  117. layoutCenter: ['50%', '50%'],
  118. roam: true,
  119. layoutSize: "100%",
  120. zoom: 1,
  121. label: {
  122. // 通常状态下的样式
  123. normal: {
  124. show: config.customize.mapName,
  125. textStyle: {
  126. color: '#fff'
  127. }
  128. },
  129. // 鼠标放上去的样式
  130. emphasis: {
  131. textStyle: {
  132. color: '#fff'
  133. }
  134. }
  135. },
  136. // 地图区域的样式设置
  137. itemStyle: {
  138. normal: {
  139. borderColor: config.customize.mapLineColor,
  140. borderWidth: 1,
  141. areaColor: config.customize.areaColor,
  142. shadowColor: 'fffff',
  143. shadowOffsetX: -2,
  144. shadowOffsetY: 2,
  145. shadowBlur: 10
  146. },
  147. // 鼠标放上去高亮的样式
  148. emphasis: {
  149. areaColor: '#389BB7',
  150. borderWidth: 0
  151. }
  152. }
  153. },
  154. tooltip: {
  155. backgroundColor: config.customize.tooltipBackgroundColor,
  156. borderColor: config.customize.borderColor,
  157. show: true,
  158. textStyle: {
  159. color: config.customize.fontColor,
  160. },
  161. },
  162. series: [
  163. {
  164. type:'effectScatter',
  165. coordinateSystem: 'geo',
  166. zlevel: 15,
  167. symbolSize:8,
  168. rippleEffect: {
  169. period: 4, brushType: 'stroke', scale: 4
  170. },
  171. tooltip: {
  172. trigger: 'item',
  173. formatter(params) {
  174. const a= eval(config.customize.scatterFormatter)
  175. return a
  176. },
  177. },
  178. itemStyle:{
  179. color:config.customize.scatterColor,
  180. opacity:1
  181. },
  182. data:coord
  183. },
  184. {
  185. type:'effectScatter',
  186. coordinateSystem: 'geo',
  187. zlevel: 15,
  188. symbolSize:12,
  189. tooltip: {
  190. trigger: 'item',
  191. formatter(params) {
  192. const a= eval(config.customize.scatterFormatter)
  193. return a
  194. },
  195. },
  196. rippleEffect: {
  197. period: 6, brushType: 'stroke', scale: 8
  198. },
  199. itemStyle:{
  200. color:config.customize.scatterCenterColor,
  201. opacity:1
  202. },
  203. data:fromCoord
  204. },
  205. {
  206. type:'lines',
  207. coordinateSystem:'geo',
  208. zlevel: 15,
  209. tooltip: {
  210. trigger: 'item',
  211. formatter(params) {
  212. const a= eval(config.customize.lineFormatter)
  213. return a
  214. },
  215. },
  216. effect: {
  217. show: true, period: 5, trailLength: 0, symbol: config.customize.symbol, color:config.customize.symbolColor,symbolSize: config.customize.symbolSize,
  218. },
  219. lineStyle: {
  220. normal: {color: function(value){
  221. return '#ffffff'
  222. },width: 2, opacity: 0.6, curveness: 0.2 }
  223. },
  224. data:lines_coord
  225. }
  226. ]
  227. }
  228. if (config.customize.visual) {
  229. option.visualMap = {
  230. show: false,
  231. min: config.customize.range[0],
  232. max: config.customize.range[1],
  233. seriesIndex: [0,2],
  234. inRange: {
  235. color: config.customize.rangeColor
  236. }
  237. }
  238. }
  239. if(config.customize.down){
  240. // config?.customize?.graphic?.forEach((item,index)=>{
  241. option.graphic.push({
  242. type: "text",
  243. left: `250px`,
  244. top: "5%",
  245. style: {
  246. text: '中国',
  247. font: `bolder ${config.customize.fontSize}px "Microsoft YaHei", sans-serif`,
  248. fill: config.customize.fontGraphicColor,
  249. },
  250. onclick:async()=>{
  251. this.level='country'
  252. const index = option.graphic.findIndex(i => i.style.text === '中国');
  253. // 点击元素之后的所有元素全部删除
  254. option.graphic.splice(index + 1);
  255. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/country/中华人民共和国.json`
  256. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  257. option.geo.map = '中华人民共和国'
  258. this.changeData({...config,customize:{...config.customize,level:'country',scope:'中国'}})
  259. echarts.registerMap('中华人民共和国', map);
  260. this.charts.setOption(option, true);
  261. }
  262. },)
  263. // })
  264. }
  265. this.charts.setOption(option)
  266. this.charts.on('click', async(params)=> {
  267. const index = option.graphic.findIndex(i => i.style.text === params.name);
  268. if(params.name=='' || index !== -1) return
  269. if(config.customize.down===false||this.level==='province') return
  270. const idx = option.graphic.length + 1;
  271. option.graphic.push({
  272. type: "text",
  273. left: `${idx * 250}px`,
  274. top: "5%",
  275. style: {
  276. text: params.name,
  277. font: `bolder ${config.customize.fontSize}px "Microsoft YaHei", sans-serif`,
  278. fill: config.customize.fontGraphicColor,
  279. },
  280. onclick: async() => {
  281. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/${params.name=='中华人民共和国'?'country':'province'}/${params.name}.json`
  282. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  283. // 利用函数的作用域,可以直接拿上面的name来用
  284. const index = option.graphic.findIndex(i => i.style.text === params.name);
  285. // 点击元素之后的所有元素全部删除
  286. option.graphic.splice(index + 1);
  287. // 很多操作重复了,你可以将公共部分抽离出来
  288. option.geo.map = params.name;
  289. this.changeData({...config,customize:{...config.customize,level:'province',scope:params.name}})
  290. echarts.registerMap(params.name, map);
  291. this.charts.setOption(option, true);
  292. },
  293. });
  294. this.level='province'
  295. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/province/${params.name}.json`
  296. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  297. this.changeData({...config,customize:{...config.customize,level:'province',scope:params.name}})
  298. option.geo.map = params.name
  299. echarts.registerMap(params.name, map);
  300. this.charts.setOption(option, true);
  301. });
  302. })
  303. }
  304. }
  305. }
  306. </script>
  307. <style lang="scss" scoped>
  308. @import '../../assets/style/echartStyle';
  309. .light-theme {
  310. background-color: #ffffff;
  311. color: #000000;
  312. }
  313. .auto-theme {
  314. background-color: rgba(0, 0, 0, 0);
  315. }
  316. </style>