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: '#fff'
  135. }
  136. },
  137. // 鼠标放上去的样式
  138. emphasis: {
  139. textStyle: {
  140. color: '#fff'
  141. }
  142. }
  143. },
  144. // 地图区域的样式设置
  145. itemStyle: {
  146. normal: {
  147. borderColor: config.customize.mapLineColor,
  148. borderWidth: 1,
  149. areaColor: config.customize.areaColor,
  150. shadowColor: 'fffff',
  151. shadowOffsetX: -2,
  152. shadowOffsetY: 2,
  153. shadowBlur: 10
  154. },
  155. // 鼠标放上去高亮的样式
  156. emphasis: {
  157. areaColor: config.customize.emphasisColor ? config.customize.emphasisColor :'#389BB7',
  158. borderWidth: 0
  159. }
  160. }
  161. },
  162. // 提示浮窗样式
  163. tooltip: {
  164. show: false,
  165. trigger: 'item',
  166. alwaysShowContent: false,
  167. backgroundColor: config.customize.tooltipBackgroundColor,
  168. borderColor: config.customize.borderColor,
  169. hideDelay: 100,
  170. triggerOn: 'mousemove',
  171. enterable: true,
  172. textStyle: {
  173. color: '#DADADA',
  174. fontSize: '12',
  175. width: 20,
  176. height: 30,
  177. overflow: 'break'
  178. },
  179. showDelay: 100
  180. },
  181. // 视觉映射
  182. visualMap: {
  183. show: !config.customize.scatter,
  184. calculable: config.customize.visual,
  185. min: config.customize.range[0],
  186. max: config.customize.range[1],
  187. seriesIndex: [0],
  188. inRange: {
  189. color: config.customize.rangeColor
  190. }
  191. },
  192. series: config.customize.scatter
  193. ? [
  194. {
  195. type: 'scatter',
  196. // 坐标系类型
  197. coordinateSystem: 'geo',
  198. // 标记符号形状 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
  199. symbol: config.customize.scatterSymbol ? config.customize.scatterSymbol : 'pin',
  200. // 是否允许图例和散点图之间的联动效果
  201. legendHoverLink: true,
  202. // 散点图标记符号的大小,[宽度,高度]
  203. symbolSize: config.customize.scatterSize ? [config.customize.scatterSize, config.customize.scatterSize] : [40, 40],
  204. // 触发特效的方式
  205. showEffectOn: 'render',
  206. rippleEffect: {
  207. brushType: 'stroke'
  208. },
  209. hoverAnimation: true,
  210. zlevel: 11,
  211. // 这里渲染标志里的内容以及样式
  212. label: {
  213. show: config.customize.hasOwnProperty('showScatterValue') ? config.customize.showScatterValue : true,
  214. formatter(value) {
  215. return value.data.value[2]
  216. },
  217. color: config.customize.scatterColor
  218. },
  219. // 标志的样式
  220. itemStyle: {
  221. normal: {
  222. color: config.customize.scatterBackgroundColor,
  223. shadowBlur: 2,
  224. shadowColor: 'D8BC37'
  225. }
  226. },
  227. data: config.option?.data
  228. }
  229. ]
  230. : [
  231. {
  232. type: 'map',
  233. map: config.customize.scope,
  234. geoIndex: 0,
  235. roam: false,
  236. zoom: 1.5,
  237. center: [105, 36],
  238. showLegendSymbol: false, // 存在legend时显示
  239. data: config.option?.data,
  240. tooltip: {
  241. formatter(params) {
  242. return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${
  243. params.name
  244. }</p>
  245. <div style="line-height:22px;margin-top:5px">${config.customize.tooltipTitle ? config.customize.tooltipTitle : 'GDP'}<span style="margin-left:12px;color:#fff;float:right">${
  246. params.data?.value[2] || '--'
  247. }</span></div>`
  248. },
  249. show: true
  250. }
  251. }
  252. ]
  253. }
  254. let hasMapId = !!config.customize.mapId
  255. // 根据mapId获取地图数据
  256. let mapInfoUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/bigScreen/map/info/${config.customize.mapId}`
  257. // 如果设置了地图id,就用地图id获取地图数据,否则用默认的世界地图
  258. if (!hasMapId) {
  259. mapInfoUrl = `${window.BS_CONFIG?.httpConfigs?.baseURL}/bigScreen/map/default/chinaMap.country/中华人民共和国`
  260. }
  261. const mapResp = await this.$dataRoomAxios.get(decodeURI(mapInfoUrl), {}, true)
  262. const map = hasMapId ? JSON.parse(mapResp.data.geoJson) : mapResp
  263. if (hasMapId && mapResp.data.uploadedGeoJson !== 1) {
  264. // 没有上传过geoJson
  265. this.$message({
  266. message: '请先上传地图数据',
  267. type: 'warning'
  268. })
  269. return
  270. }
  271. this.mapList.push(mapResp.data)
  272. echarts.registerMap(config.customize.scope, map)
  273. this.charts.setOption(this.option)
  274. // 点击下钻
  275. this.charts.on('click', async (params) => {
  276. if (params.name == '') return
  277. if (!config.customize.down) {
  278. this.$message({
  279. message: '该地图未开启下钻',
  280. type: 'warning'
  281. })
  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>