index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. import { get } from 'data-room-ui/js/utils/http'
  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. }
  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. let 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. geo: {
  99. map: config.customize.scope,
  100. label: {
  101. // 通常状态下的样式
  102. normal: {
  103. show: config.customize.mapName,
  104. textStyle: {
  105. color: '#fff'
  106. }
  107. },
  108. // 鼠标放上去的样式
  109. emphasis: {
  110. textStyle: {
  111. color: '#fff'
  112. }
  113. }
  114. },
  115. // 地图区域的样式设置
  116. itemStyle: {
  117. normal: {
  118. borderColor: config.customize.mapLineColor,
  119. borderWidth: 1,
  120. areaColor: config.customize.areaColor,
  121. shadowColor: 'fffff',
  122. shadowOffsetX: -2,
  123. shadowOffsetY: 2,
  124. shadowBlur: 10
  125. },
  126. // 鼠标放上去高亮的样式
  127. emphasis: {
  128. areaColor: '#389BB7',
  129. borderWidth: 0
  130. }
  131. }
  132. },
  133. // 提示浮窗样式
  134. tooltip: {
  135. show: false,
  136. trigger: 'item',
  137. alwaysShowContent: false,
  138. backgroundColor: config.customize.tooltipBackgroundColor,
  139. borderColor: config.customize.borderColor,
  140. hideDelay: 100,
  141. triggerOn: 'mousemove',
  142. enterable: true,
  143. textStyle: {
  144. color: '#DADADA',
  145. fontSize: '12',
  146. width: 20,
  147. height: 30,
  148. overflow: 'break'
  149. },
  150. showDelay: 100
  151. },
  152. series: config.customize.scatter
  153. ? [
  154. // {
  155. // type: 'effectScatter',
  156. // coordinateSystem: 'geo',
  157. // effectType: 'ripple',
  158. // showEffectOn: 'render',
  159. // rippleEffect: {
  160. // period: 10,
  161. // scale: 10,
  162. // brushType: 'fill'
  163. // },
  164. // hoverAnimation: true,
  165. // itemStyle: {
  166. // normal: {
  167. // color: 'rgba(255, 235, 59, .7)',
  168. // shadowBlur: 10,
  169. // shadowColor: '#333'
  170. // }
  171. // },
  172. // tooltip: {
  173. // formatter(params) {
  174. // return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  175. // params.name
  176. // }</p>
  177. // <div style="line-height:22px;margin-top:5px">GDP<span style="margin-left:12px;color:#fff;float:right">${
  178. // params.data?.value[2] || '--'
  179. // }</span></div>`
  180. // },
  181. // show: true
  182. // },
  183. // zlevel: 1,
  184. // data: [
  185. // { name: '西藏自治区', value: [91.23, 29.5, 1] },
  186. // { name: '黑龙江省', value: [128.03, 47.01, 1007] },
  187. // { name: '北京市', value: [116.4551, 40.2539, 5007] }
  188. // ]
  189. // }
  190. {
  191. type: 'scatter',
  192. coordinateSystem: 'geo',
  193. symbol: 'pin',
  194. legendHoverLink: true,
  195. symbolSize: [60, 60],
  196. showEffectOn: 'render',
  197. rippleEffect: {
  198. brushType: 'stroke'
  199. },
  200. hoverAnimation: true,
  201. zlevel: 1,
  202. // 这里渲染标志里的内容以及样式
  203. label: {
  204. show: true,
  205. formatter (value) {
  206. return value.data.value[2]
  207. },
  208. color: config.customize.scatterColor
  209. },
  210. // 标志的样式
  211. itemStyle: {
  212. normal: {
  213. color: config.customize.scatterBackgroundColor,
  214. shadowBlur: 2,
  215. shadowColor: 'D8BC37'
  216. }
  217. },
  218. data: config.option?.data
  219. }
  220. ]
  221. : [
  222. {
  223. type: 'map',
  224. map: config.customize.scope,
  225. geoIndex: 0,
  226. roam: false,
  227. zoom: 1.5,
  228. center: [105, 36],
  229. showLegendSymbol: false, // 存在legend时显示
  230. data: config.option?.data,
  231. tooltip: {
  232. formatter (params) {
  233. return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  234. params.name
  235. }</p>
  236. <div style="line-height:22px;margin-top:5px">GDP<span style="margin-left:12px;color:#fff;float:right">${
  237. params.data?.value[2] || '--'
  238. }</span></div>`
  239. },
  240. show: true
  241. }
  242. }
  243. ]
  244. }
  245. if (config.customize.visual) {
  246. option.visualMap = {
  247. show: true,
  248. min: config.customize.range[0],
  249. max: config.customize.range[1],
  250. seriesIndex: [0],
  251. inRange: {
  252. color: config.customize.rangeColor
  253. }
  254. }
  255. }
  256. const mapUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/${config.customize.level}/${config.customize.dataMap}`
  257. const map = await get(decodeURI(mapUrl), {}, true)
  258. echarts.registerMap(config.customize.scope, map)
  259. this.charts.setOption(option)
  260. // this.charts.on('click', (params) => {
  261. // get(
  262. // `${window.BS_CONFIG?.httpConfigs?.baseURL}/static/chinaMap/province/${params.name}.json`,
  263. // {},
  264. // true
  265. // ).then((res) => {
  266. // option.geo.map = params.name
  267. // echarts.registerMap(params.name, res)
  268. // this.charts.setOption(option, true)
  269. // })
  270. // })
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. @import '../../assets/style/echartStyle';
  277. .light-theme {
  278. background-color: #ffffff;
  279. color: #000000;
  280. }
  281. .auto-theme {
  282. background-color: rgba(0, 0, 0, 0);
  283. }
  284. </style>