index.vue 9.9 KB

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