index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 commonMixins from 'data-room-ui/js/mixins/commonMixins.js'
  16. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  17. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  18. export default {
  19. name: 'MapCharts',
  20. mixins: [paramsMixins, commonMixins, linkageMixins],
  21. props: {
  22. id: {
  23. type: String,
  24. default: ''
  25. },
  26. config: {
  27. type: Object,
  28. default: () => ({})
  29. }
  30. },
  31. data () {
  32. return {
  33. charts: null,
  34. hasData: false,
  35. level:''
  36. }
  37. },
  38. computed: {
  39. Data () {
  40. return JSON.parse(JSON.stringify(this.config))
  41. }
  42. },
  43. watch: {
  44. Data: {
  45. handler (newVal, oldVal) {
  46. if (newVal.w !== oldVal.w || newVal.h !== oldVal.h) {
  47. this.$nextTick(() => {
  48. this.charts.resize()
  49. })
  50. }
  51. },
  52. deep: true
  53. }
  54. },
  55. mounted () {
  56. this.chartInit()
  57. },
  58. beforeDestroy () {
  59. this.charts?.clear()
  60. },
  61. methods: {
  62. chartInit () {
  63. const config = this.config
  64. // key和code相等,说明是一进来刷新,调用list接口
  65. if (this.config.code === this.config.key || this.isPreview) {
  66. // 改变数据
  67. this.changeDataByCode(config).then((res) => {
  68. // 改变样式
  69. // config = this.changeStyle(res)
  70. this.newChart(config)
  71. }).catch(() => {})
  72. } else {
  73. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  74. this.changeData(config).then((res) => {
  75. // 初始化图表
  76. this.newChart(res)
  77. })
  78. }
  79. },
  80. dataFormatting (config, data) {
  81. const dataList = []
  82. data?.data?.forEach(item => {
  83. dataList.push({ name: item[config.customize.name], value: [item[config.customize.xaxis], item[config.customize.yaxis], item[config.customize.value]] })
  84. })
  85. config.option = {
  86. ...config.option,
  87. data: dataList
  88. }
  89. return config
  90. },
  91. async newChart (config) {
  92. this.charts = echarts.init(
  93. document.getElementById(`chart${this.config.code}`)
  94. )
  95. const option = {
  96. // 背景颜色
  97. backgroundColor: config.customize.backgroundColor,
  98. graphic: [
  99. ],
  100. geo: {
  101. map: config.customize.scope,
  102. zlevel: 10,
  103. show:true,
  104. layoutCenter: ['50%', '50%'],
  105. roam: true,
  106. layoutSize: "100%",
  107. zoom: 1,
  108. label: {
  109. // 通常状态下的样式
  110. normal: {
  111. show: config.customize.mapName,
  112. textStyle: {
  113. color: '#fff'
  114. }
  115. },
  116. // 鼠标放上去的样式
  117. emphasis: {
  118. textStyle: {
  119. color: '#fff'
  120. }
  121. }
  122. },
  123. // 地图区域的样式设置
  124. itemStyle: {
  125. normal: {
  126. borderColor: config.customize.mapLineColor,
  127. borderWidth: 1,
  128. areaColor: config.customize.areaColor,
  129. shadowColor: 'fffff',
  130. shadowOffsetX: -2,
  131. shadowOffsetY: 2,
  132. shadowBlur: 10
  133. },
  134. // 鼠标放上去高亮的样式
  135. emphasis: {
  136. areaColor: '#389BB7',
  137. borderWidth: 0
  138. }
  139. }
  140. },
  141. // 提示浮窗样式
  142. tooltip: {
  143. show: false,
  144. trigger: 'item',
  145. alwaysShowContent: false,
  146. backgroundColor: config.customize.tooltipBackgroundColor,
  147. borderColor: config.customize.borderColor,
  148. hideDelay: 100,
  149. triggerOn: 'mousemove',
  150. enterable: true,
  151. textStyle: {
  152. color: '#DADADA',
  153. fontSize: '12',
  154. width: 20,
  155. height: 30,
  156. overflow: 'break'
  157. },
  158. showDelay: 100
  159. },
  160. series: config.customize.scatter
  161. ? [
  162. // {
  163. // type: 'effectScatter',
  164. // coordinateSystem: 'geo',
  165. // effectType: 'ripple',
  166. // showEffectOn: 'render',
  167. // rippleEffect: {
  168. // period: 10,
  169. // scale: 10,
  170. // brushType: 'fill'
  171. // },
  172. // hoverAnimation: true,
  173. // itemStyle: {
  174. // normal: {
  175. // color: 'rgba(255, 235, 59, .7)',
  176. // shadowBlur: 10,
  177. // shadowColor: '#333'
  178. // }
  179. // },
  180. // tooltip: {
  181. // formatter(params) {
  182. // return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  183. // params.name
  184. // }</p>
  185. // <div style="line-height:22px;margin-top:5px">GDP<span style="margin-left:12px;color:#fff;float:right">${
  186. // params.data?.value[2] || '--'
  187. // }</span></div>`
  188. // },
  189. // show: true
  190. // },
  191. // zlevel: 1,
  192. // data: [
  193. // { name: '西藏自治区', value: [91.23, 29.5, 1] },
  194. // { name: '黑龙江省', value: [128.03, 47.01, 1007] },
  195. // { name: '北京市', value: [116.4551, 40.2539, 5007] }
  196. // ]
  197. // }
  198. {
  199. type: 'scatter',
  200. coordinateSystem: 'geo',
  201. symbol: 'pin',
  202. legendHoverLink: true,
  203. symbolSize: [60, 60],
  204. showEffectOn: 'render',
  205. rippleEffect: {
  206. brushType: 'stroke'
  207. },
  208. hoverAnimation: true,
  209. zlevel: 1,
  210. // 这里渲染标志里的内容以及样式
  211. label: {
  212. show: true,
  213. formatter (value) {
  214. return value.data.value[2]
  215. },
  216. color: config.customize.scatterColor
  217. },
  218. // 标志的样式
  219. itemStyle: {
  220. normal: {
  221. color: config.customize.scatterBackgroundColor,
  222. shadowBlur: 2,
  223. shadowColor: 'D8BC37'
  224. }
  225. },
  226. data: config.option?.data
  227. }
  228. ]
  229. : [
  230. {
  231. type: 'map',
  232. map: config.customize.scope,
  233. geoIndex: 0,
  234. roam: false,
  235. zoom: 1.5,
  236. center: [105, 36],
  237. showLegendSymbol: false, // 存在legend时显示
  238. data: config.option?.data,
  239. tooltip: {
  240. formatter (params) {
  241. return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  242. params.name
  243. }</p>
  244. <div style="line-height:22px;margin-top:5px">GDP<span style="margin-left:12px;color:#fff;float:right">${
  245. params.data?.value[2] || '--'
  246. }</span></div>`
  247. },
  248. show: true
  249. }
  250. }
  251. ]
  252. }
  253. if (config.customize.visual) {
  254. option.visualMap = {
  255. show: true,
  256. min: config.customize.range[0],
  257. max: config.customize.range[1],
  258. seriesIndex: [0],
  259. inRange: {
  260. color: config.customize.rangeColor
  261. }
  262. }
  263. }
  264. if(config.customize.down){
  265. // config?.customize?.graphic?.forEach((item,index)=>{
  266. option.graphic.push({
  267. type: "text",
  268. left: `220px`,
  269. top: "5%",
  270. style: {
  271. text: '中国',
  272. font: `bolder ${config.customize.fontSize}px "Microsoft YaHei", sans-serif`,
  273. fill: config.customize.fontGraphicColor,
  274. },
  275. onclick:async()=>{
  276. this.level='country'
  277. const index = option.graphic.findIndex(i => i.style.text === '中国');
  278. // 点击元素之后的所有元素全部删除
  279. option.graphic.splice(index + 1);
  280. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/country/中华人民共和国.json`
  281. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  282. option.geo.map = '中华人民共和国'
  283. this.changeData({...config,customize:{...config.customize,level:'country',scope:'中国'}})
  284. echarts.registerMap('中华人民共和国', map);
  285. this.charts.setOption(option, true);
  286. }
  287. },)
  288. // })
  289. }
  290. const mapUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/${config.customize.level}/${config.customize.dataMap}`
  291. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  292. echarts.registerMap(config.customize.scope, map)
  293. this.charts.setOption(option)
  294. this.charts.on('click', async(params)=> {
  295. const index = option.graphic.findIndex(i => i.style.text === params.name);
  296. if(params.name=='' || index !== -1) return
  297. if(config.customize.down===false||this.level==='province') return
  298. const idx = option.graphic.length + 1;
  299. option.graphic.push({
  300. type: "text",
  301. left: `${idx * 220}px`,
  302. top: "5%",
  303. style: {
  304. text: params.name,
  305. font: `bolder ${config.customize.fontSize}px "Microsoft YaHei", sans-serif`,
  306. fill: config.customize.fontGraphicColor,
  307. },
  308. onclick: async() => {
  309. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/${params.name=='中华人民共和国'?'country':'province'}/${params.name}.json`
  310. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  311. // 利用函数的作用域,可以直接拿上面的name来用
  312. const index = option.graphic.findIndex(i => i.style.text === params.name);
  313. // 点击元素之后的所有元素全部删除
  314. option.graphic.splice(index + 1);
  315. // 很多操作重复了,你可以将公共部分抽离出来
  316. option.geo.map = params.name;
  317. this.changeData({...config,customize:{...config.customize,level:'province',scope:params.name}})
  318. echarts.registerMap(params.name, map);
  319. this.charts.setOption(option, true);
  320. },
  321. });
  322. this.level='province'
  323. const mapUrl =`${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/province/${params.name}.json`
  324. const map = await this.$dataRoomAxios.get(decodeURI(mapUrl), {}, true)
  325. this.changeData({...config,customize:{...config.customize,level:'province',scope:params.name}})
  326. option.geo.map = params.name
  327. echarts.registerMap(params.name, map);
  328. this.charts.setOption(option, true);
  329. });
  330. }
  331. }
  332. }
  333. </script>
  334. <style lang="scss" scoped>
  335. @import '../../assets/style/echartStyle';
  336. .light-theme {
  337. background-color: #ffffff;
  338. color: #000000;
  339. }
  340. .auto-theme {
  341. background-color: rgba(0, 0, 0, 0);
  342. }
  343. </style>