index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <div
  3. class="bs-design-wrap bs-bar"
  4. style="width: 100%; height: 100%"
  5. >
  6. <el-button v-if="currentDeep > 0" class="button" type='text' @click="jumpTo(config)"> 返回上一级</el-button>
  7. <div
  8. :id="`chart${config.code}`"
  9. style="width: 100%; height: 100%"
  10. />
  11. </div>
  12. </template>
  13. <script>
  14. import 'insert-css'
  15. import * as echarts from 'echarts'
  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. currentDeep: 0,
  35. mapList: [],
  36. charts: null,
  37. hasData: false,
  38. level: '',
  39. option: {}
  40. }
  41. },
  42. computed: {
  43. Data() {
  44. return JSON.parse(JSON.stringify(this.config))
  45. }
  46. },
  47. watch: {
  48. Data: {
  49. handler(newVal, oldVal) {
  50. if (newVal.w !== oldVal.w || newVal.h !== oldVal.h) {
  51. this.$nextTick(() => {
  52. this.charts.resize()
  53. })
  54. }
  55. },
  56. deep: true
  57. }
  58. },
  59. mounted() {
  60. this.chartInit()
  61. },
  62. beforeDestroy() {
  63. this.charts?.clear()
  64. },
  65. methods: {
  66. chartInit() {
  67. const config = this.config
  68. // key和code相等,说明是一进来刷新,调用list接口
  69. if (this.config.code === this.config.key || this.isPreview) {
  70. // 改变数据
  71. this.changeDataByCode(config).then((res) => {
  72. // 改变样式
  73. // config = this.changeStyle(res)
  74. this.newChart(config)
  75. }).catch(() => {
  76. })
  77. } else {
  78. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  79. this.changeData(config).then((res) => {
  80. // 初始化图表
  81. this.newChart(res)
  82. })
  83. }
  84. },
  85. dataFormatting(config, data) {
  86. const dataList = []
  87. data?.data?.forEach(item => {
  88. dataList.push({
  89. name: item[config.customize.name],
  90. value: [item[config.customize.xaxis], item[config.customize.yaxis], item[config.customize.value]]
  91. })
  92. })
  93. config.option = {
  94. ...config.option,
  95. data: dataList
  96. }
  97. return config
  98. },
  99. async jumpTo(config) {
  100. this.currentDeep--
  101. let map = this.mapList[this.currentDeep]
  102. // 移除mapList中的最后一个元素
  103. this.mapList.pop()
  104. let mapData = JSON.parse(map.geoJson)
  105. this.option.geo.map = map.name;
  106. this.changeData({...config, customize: {...config.customize, level: map.level, scope: map.name}})
  107. echarts.registerMap(map.name, mapData);
  108. this.charts.setOption(this.option, true);
  109. },
  110. async newChart(config) {
  111. let center1 = config.customize.center1 ? config.customize.center1 + '%' : '50%'
  112. let center2 = config.customize.center2 ? config.customize.center2 + '%' : '50%'
  113. this.charts = echarts.init(
  114. document.getElementById(`chart${this.config.code}`)
  115. )
  116. this.option = {
  117. // 背景颜色
  118. backgroundColor: config.customize.backgroundColor,
  119. graphic: [],
  120. geo: {
  121. map: config.customize.scope,
  122. zlevel: 9,
  123. show: true,
  124. // 地图中心点位置
  125. layoutCenter: [center1, center2],
  126. roam: true,
  127. layoutSize: "100%",
  128. zoom: config.customize.zoom || 1,
  129. label: {
  130. // 通常状态下的样式
  131. normal: {
  132. show: config.customize.mapName,
  133. textStyle: {
  134. color: config.customize.mapNameColor || '#fff',
  135. fontSize: config.customize.mapNameSize || 12,
  136. fontWeight: config.customize.mapNameWeight || 500
  137. }
  138. },
  139. // 鼠标放上去的样式
  140. emphasis: {
  141. textStyle: {
  142. color: config.customize.mapNameColor || '#fff',
  143. fontSize: config.customize.mapNameSize || 12,
  144. fontWeight: config.customize.mapNameWeight || 500
  145. }
  146. }
  147. },
  148. // 地图区域的样式设置
  149. itemStyle: {
  150. normal: {
  151. borderColor: config.customize.mapLineColor,
  152. borderWidth: 1,
  153. areaColor: config.customize.areaColor,
  154. shadowColor: 'fffff',
  155. shadowOffsetX: -2,
  156. shadowOffsetY: 2,
  157. shadowBlur: 10
  158. },
  159. // 鼠标放上去高亮的样式
  160. emphasis: {
  161. areaColor: config.customize.emphasisColor ? config.customize.emphasisColor :'#389BB7',
  162. borderWidth: 0
  163. }
  164. }
  165. },
  166. // 提示浮窗样式
  167. tooltip: {
  168. show: false,
  169. trigger: 'item',
  170. alwaysShowContent: false,
  171. backgroundColor: config.customize.tooltipBackgroundColor,
  172. borderColor: config.customize.borderColor,
  173. hideDelay: 100,
  174. triggerOn: 'mousemove',
  175. enterable: true,
  176. textStyle: {
  177. color: '#DADADA',
  178. fontSize: '12',
  179. width: 20,
  180. height: 30,
  181. overflow: 'break'
  182. },
  183. showDelay: 100
  184. },
  185. // 视觉映射
  186. visualMap: {
  187. show: !config.customize.scatter,
  188. calculable: config.customize.visual,
  189. min: config.customize.range[0],
  190. max: config.customize.range[1],
  191. seriesIndex: [0],
  192. inRange: {
  193. color: config.customize.rangeColor
  194. }
  195. },
  196. series: config.customize.scatter
  197. ? [
  198. {
  199. type: 'scatter',
  200. // 坐标系类型
  201. coordinateSystem: 'geo',
  202. // 标记符号形状 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
  203. symbol: config.customize.scatterSymbol ? config.customize.scatterSymbol : 'pin',
  204. // 是否允许图例和散点图之间的联动效果
  205. legendHoverLink: true,
  206. // 散点图标记符号的大小,[宽度,高度]
  207. symbolSize: config.customize.scatterSize ? [config.customize.scatterSize, config.customize.scatterSize] : [40, 40],
  208. // 触发特效的方式
  209. showEffectOn: 'render',
  210. rippleEffect: {
  211. brushType: 'stroke'
  212. },
  213. hoverAnimation: true,
  214. zlevel: 11,
  215. // 这里渲染标志里的内容以及样式
  216. label: {
  217. show: config.customize.hasOwnProperty('showScatterValue') ? config.customize.showScatterValue : true,
  218. formatter(value) {
  219. return value.data.value[2]
  220. },
  221. color: config.customize.scatterColor
  222. },
  223. // 标志的样式
  224. itemStyle: {
  225. normal: {
  226. color: config.customize.scatterBackgroundColor,
  227. shadowBlur: 2,
  228. shadowColor: 'D8BC37'
  229. }
  230. },
  231. data: config.option?.data
  232. }
  233. ]
  234. : [
  235. {
  236. type: 'map',
  237. map: config.customize.scope,
  238. geoIndex: 0,
  239. roam: false,
  240. zoom: 1.5,
  241. center: [105, 36],
  242. showLegendSymbol: false, // 存在legend时显示
  243. data: config.option?.data,
  244. tooltip: {
  245. formatter(params) {
  246. return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  247. params.name
  248. }</p>
  249. <div style="line-height:22px;margin-top:5px">${config.customize.tooltipTitle ? config.customize.tooltipTitle : 'GDP'}<span style="margin-left:12px;color:#fff;float:right">${
  250. params.data?.value[2] || '--'
  251. }</span></div>`
  252. },
  253. show: true
  254. }
  255. }
  256. ]
  257. }
  258. let hasMapId = !!config.customize.mapId
  259. // 根据mapId获取地图数据
  260. let mapInfoUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/bigScreen/map/info/${config.customize.mapId}`
  261. // 如果设置了地图id,就用地图id获取地图数据,否则用默认的世界地图
  262. if (!hasMapId) {
  263. mapInfoUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/bigScreen/map/default/chinaMap.country/中华人民共和国`
  264. }
  265. const mapResp = await this.$dataRoomAxios.get(decodeURI(mapInfoUrl), {}, true)
  266. const map = hasMapId ? JSON.parse(mapResp.data.geoJson) : mapResp
  267. if (hasMapId && mapResp.data.uploadedGeoJson !== 1) {
  268. // 没有上传过geoJson
  269. this.$message({
  270. message: '请先上传地图数据',
  271. type: 'warning'
  272. })
  273. return
  274. }
  275. this.mapList.push(mapResp.data)
  276. echarts.registerMap(config.customize.scope, map)
  277. this.charts.setOption(this.option)
  278. // 点击下钻
  279. this.charts.on('click', async (params) => {
  280. if (params.name == '') return
  281. if (!config.customize.down) {
  282. return
  283. }
  284. // 到达允许下钻的层数,则不再下钻
  285. if (this.currentDeep >= config.customize.downLevel) return
  286. const downMapUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/bigScreen/map/data/${this.mapList[this.currentDeep].id}/${params.name}`
  287. const downMap = await this.$dataRoomAxios.get(decodeURI(downMapUrl), {}, false)
  288. // 地图不可用
  289. if (downMap.available !== 1) {
  290. this.$message({
  291. message: '未找到该地图配置',
  292. type: 'warning'
  293. })
  294. return
  295. }
  296. let geoJsonObj
  297. try {
  298. geoJsonObj = JSON.parse(downMap.geoJson)
  299. } catch (error) {
  300. this.$message({
  301. message: params.name + '地图数据格式错误',
  302. type: 'warning'
  303. })
  304. return
  305. }
  306. this.currentDeep++
  307. this.mapList.push(downMap)
  308. this.changeData({...config, customize: {...config.customize, scope: params.name}})
  309. this.option.geo.map = params.name
  310. echarts.registerMap(params.name, geoJsonObj);
  311. this.charts.setOption(this.option, true);
  312. });
  313. }
  314. }
  315. }
  316. </script>
  317. <style lang="scss" scoped>
  318. @import '../../assets/style/echartStyle';
  319. .light-theme {
  320. background-color: #ffffff;
  321. color: #000000;
  322. }
  323. .auto-theme {
  324. background-color: rgba(0, 0, 0, 0);
  325. }
  326. .bs-design-wrap {
  327. position: relative;
  328. .button {
  329. position: absolute;
  330. z-index: 999;
  331. }
  332. }
  333. </style>