index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <template>
  2. <div
  3. v-loading="config.loading"
  4. element-loading-text="图表加载中"
  5. :element-loading-background="loadingBackground"
  6. style="width: 100%;height: 100%"
  7. class="bs-design-wrap bs-custom-component"
  8. :class="{'light-theme':customTheme === 'light','auto-theme':customTheme !=='light'}"
  9. >
  10. <div
  11. :id="chatId"
  12. style="width: 100%;height: 100%"
  13. />
  14. </div>
  15. </template>
  16. <script>
  17. import 'insert-css'
  18. import cloneDeep from 'lodash/cloneDeep'
  19. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  20. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  21. import { mapState, mapMutations } from 'vuex'
  22. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  23. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  24. import _ from 'lodash'
  25. import * as echarts from 'echarts'
  26. import CloneDeep from 'lodash-es/cloneDeep'
  27. export default {
  28. name: 'PlotCustomComponent',
  29. mixins: [commonMixins, linkageMixins],
  30. props: {
  31. config: {
  32. type: Object,
  33. default: () => ({})
  34. }
  35. },
  36. data () {
  37. return {
  38. chart: null,
  39. hasData: false,
  40. plotList
  41. }
  42. },
  43. computed: {
  44. ...mapState('bigScreen', {
  45. pageInfo: state => state.pageInfo,
  46. customTheme: state => state.pageInfo.pageConfig.customTheme,
  47. activeCode: state => state.activeCode
  48. }),
  49. chatId () {
  50. let prefix = 'chart_'
  51. if (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) {
  52. prefix = 'preview_chart_'
  53. }
  54. if (this.$route.path === window?.BS_CONFIG?.routers?.designUrl) {
  55. prefix = 'design_chart_'
  56. }
  57. if (this.$route.path === window?.BS_CONFIG?.routers?.pageListUrl) {
  58. prefix = 'management_chart_'
  59. }
  60. return prefix + this.config.code
  61. }
  62. },
  63. created () {
  64. this.plotList = [...this.plotList, ...getCustomPlots()]
  65. },
  66. watch: {
  67. // 监听主题变化手动触发组件配置更新
  68. 'config.option.theme': {
  69. handler (val) {
  70. if (val) {
  71. this.changeStyle(this.config, true)
  72. }
  73. }
  74. }
  75. },
  76. mounted () {
  77. },
  78. beforeDestroy () {
  79. if (this.chart) {
  80. this.chart.dispose()
  81. }
  82. },
  83. methods: {
  84. ...mapMutations('bigScreen', ['changeChartConfig', 'changeActiveItemConfig', 'changeChartLoading']),
  85. chartInit () {
  86. let config = this.config
  87. // key和code相等,说明是一进来刷新,调用list接口
  88. if (this.config.code === this.config.key || this.isPreview) {
  89. // 改变样式
  90. config = this.changeStyle(config)
  91. // 改变数据
  92. config.loading = true
  93. this.changeChartLoading(config)
  94. this.changeDataByCode(config).then((res) => {
  95. // 初始化图表
  96. config.loading = false
  97. this.changeChartLoading(config)
  98. this.newChart(res)
  99. }).catch(() => {
  100. })
  101. } else {
  102. config.loading = true
  103. this.changeChartLoading(config)
  104. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  105. this.changeData(config).then((res) => {
  106. config.loading = false
  107. this.changeChartLoading(config)
  108. // 初始化图表
  109. this.newChart(res)
  110. })
  111. }
  112. },
  113. /**
  114. * 构造chart
  115. */
  116. newChart (config) {
  117. const chartDom = document.getElementById(this.chatId)
  118. this.chart = echarts.init(chartDom)
  119. config.option && this.chart.setOption(config.option)
  120. },
  121. /**
  122. * 注册事件
  123. */
  124. registerEvent () {
  125. // 图表添加事件进行数据联动
  126. let formData = {}
  127. // eslint-disable-next-line no-unused-vars
  128. this.chart.on('tooltip:change', (...args) => {
  129. formData = {}
  130. formData = cloneDeep(args[0].data.items[0].data)
  131. })
  132. // eslint-disable-next-line no-unused-vars
  133. this.chart.on('plot:click', (...args) => {
  134. this.linkage(formData)
  135. })
  136. },
  137. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  138. transformSettingToOption (config, type) {
  139. let option = null
  140. config.setting.forEach(set => {
  141. if (set.optionField) {
  142. const optionField = set.optionField.split('.')
  143. option = config.option
  144. optionField.forEach((field, index) => {
  145. if (index === optionField.length - 1) {
  146. // 数据配置时,必须有值才更新
  147. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  148. option[field] = set.value
  149. }
  150. } else {
  151. option = option[field]
  152. }
  153. })
  154. }
  155. })
  156. config.option = { ...config.option, ...option }
  157. return config
  158. },
  159. dataFormatting (config, data) {
  160. // 数据返回成功则赋值
  161. if (data.success) {
  162. data = data.data
  163. config = this.transformSettingToOption(config, 'data')
  164. // 获取到后端返回的数据,有则赋值
  165. const option = config.option
  166. const setting = config.setting
  167. if (config.dataHandler) {
  168. try {
  169. // 此处函数处理data
  170. eval(config.dataHandler)
  171. } catch (e) {
  172. console.error(e)
  173. }
  174. }
  175. config.option = this.echartsOptionFormatting(config, data)
  176. } else {
  177. // 数据返回失败则赋前端的模拟数据
  178. // config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config?.option?.data
  179. }
  180. return config
  181. },
  182. getxDataAndYData (xField, yField, data, hasSeries) {
  183. let list = []
  184. let xData = []
  185. let yData = []
  186. const uniqueData = {}
  187. // 遍历原始数据数组
  188. data.forEach((item) => {
  189. // 使用城市名称作为键,覆盖旧数据,始终保留最后一条数据
  190. uniqueData[item[xField]] = item
  191. })
  192. // 将唯一数据对象的值(即去重后的数据)转换回数组
  193. list = Object.values(uniqueData)
  194. xData = list.map(item => item[xField])
  195. yData = list.map(item => item[yField])
  196. return { xData, yData }
  197. },
  198. // 格式化echarts的配置
  199. echartsOptionFormatting (config, data) {
  200. const option = config.option
  201. // 分组字段
  202. const xField = config.setting.find(item => item.optionField === 'xField')?.value
  203. const yField = config.setting.find(item => item.optionField === 'yField')?.value
  204. const hasSeries = config.setting.find(item => item.optionField === 'seriesField' && item.value !== '')
  205. const { xData, yData } = this.getxDataAndYData(xField, yField, data, hasSeries)
  206. // const xData = [...new Set(data.map(item => item[xField]))]
  207. // const yData = data.map(item => item[yField])
  208. const maxY = Math.max(...yData) + Math.max(...yData) * 0.2
  209. // 生成阴影柱子的值
  210. const shadowData = Array.from({ length: xData.length }, () => maxY)
  211. option.xAxis = option.xAxis.map(item => {
  212. return {
  213. ...item,
  214. data: xData
  215. }
  216. })
  217. // 判断是否存在分组字段
  218. if (hasSeries) {
  219. const seriesField = config.setting.find(item => item.optionField === 'seriesField')?.value
  220. const seriesFieldList = [...new Set(data.map(item => item[seriesField]))]
  221. option.series = []
  222. const offsetArr = []
  223. let index = 0
  224. let barWidth = 10
  225. if (seriesFieldList.length % 2 === 0) {
  226. const length = seriesFieldList.length / 2
  227. for (let i = 0; i < length; i++) {
  228. const offsetX = (parseInt('10%') + parseInt('50%')) * (2 * i + 1)
  229. offsetArr.push(offsetX)
  230. offsetArr.unshift(-offsetX)
  231. }
  232. } else {
  233. const length = Math.ceil(seriesFieldList.length / 2)
  234. for (let i = 0; i < length; i++) {
  235. if (i === 0) {
  236. offsetArr.push(0)
  237. } else {
  238. const offsetX = (parseInt('20%') + parseInt('100%')) * i
  239. offsetArr.push(offsetX)
  240. offsetArr.unshift(-offsetX)
  241. }
  242. }
  243. }
  244. for (const seriesFieldItem of seriesFieldList) {
  245. const seriesData = (data.filter(item => item[seriesField] === seriesFieldItem))?.map(item => item[yField])
  246. const seriesItem = [
  247. {
  248. name: seriesFieldItem + '柱子顶部',
  249. type: 'pictorialBar',
  250. tooltip: { show: false },
  251. symbol: 'diamond',
  252. symbolSize: [barWidth, barWidth / 2],
  253. symbolOffset: [offsetArr[index] + '%', -barWidth / 4],
  254. symbolPosition: 'end',
  255. z: 15,
  256. zlevel: 2,
  257. color: 'rgba(2, 175, 249,1)',
  258. data: seriesData
  259. },
  260. {
  261. name: seriesFieldItem,
  262. type: 'bar',
  263. barGap: '20%',
  264. barWidth: barWidth,
  265. itemStyle: {
  266. normal: {
  267. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  268. {
  269. offset: 0,
  270. color: '#115ba6'
  271. },
  272. {
  273. offset: 1,
  274. color: '#1db0dd'
  275. }
  276. ]),
  277. opacity: 0.8,
  278. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  279. shadowBlur: 0 // 阴影模糊值
  280. }
  281. },
  282. label: {
  283. show: false
  284. },
  285. zlevel: 2,
  286. z: 12,
  287. data: seriesData
  288. },
  289. {
  290. name: seriesFieldItem + '柱子底部',
  291. type: 'pictorialBar',
  292. tooltip: { show: false },
  293. symbol: 'diamond',
  294. symbolSize: [barWidth, barWidth / 2],
  295. symbolOffset: [offsetArr[index] + '%', barWidth / 4],
  296. zlevel: 2,
  297. z: 15,
  298. color: 'rgb(2, 192, 255)',
  299. data: seriesData
  300. },
  301. {
  302. name: seriesFieldItem + '背景柱子',
  303. type: 'bar',
  304. tooltip: { show: false },
  305. xAxisIndex: 1,
  306. barGap: '20%',
  307. data: shadowData,
  308. zlevel: 1,
  309. barWidth: barWidth,
  310. itemStyle: {
  311. normal: {
  312. color: 'rgba(9, 44, 76,.8)'
  313. }
  314. }
  315. },
  316. {
  317. name: seriesFieldItem + '背景柱子顶部',
  318. type: 'pictorialBar',
  319. tooltip: { show: false },
  320. symbol: 'diamond',
  321. symbolSize: [barWidth, barWidth / 2],
  322. symbolOffset: [offsetArr[index] + '%', -barWidth / 4],
  323. symbolPosition: 'end',
  324. z: 15,
  325. color: 'rgb(15, 69, 133)',
  326. zlevel: 1,
  327. data: shadowData
  328. }
  329. ]
  330. index++
  331. option.series.push(...seriesItem)
  332. }
  333. } else {
  334. option.series = [
  335. {
  336. id: 'barTopColor', // 用于区分是图表的什么部分
  337. type: 'pictorialBar', // 象形柱图
  338. symbol: 'diamond',
  339. symbolOffset: [0, '-50%'], // 上部菱形
  340. symbolSize: [30, 15],
  341. // symbolOffset: [0, -6], // 上部椭圆
  342. symbolPosition: 'end',
  343. z: 12,
  344. label: {
  345. normal: {
  346. show: true,
  347. position: 'top',
  348. fontSize: 15,
  349. fontWeight: 'bold',
  350. color: '#27a7ce'
  351. }
  352. },
  353. color: option.seriesCustom.barTopColor,
  354. data: yData
  355. },
  356. {
  357. id: 'barBottomColor', // 用于区分是图表的什么部分
  358. type: 'pictorialBar',
  359. symbol: 'diamond',
  360. symbolSize: [30, 15],
  361. symbolOffset: ['0%', '50%'], // 下部菱形
  362. // symbolOffset: [0, 7], // 下部椭圆
  363. z: 12,
  364. color: option.seriesCustom.barBottomColor,
  365. data: yData
  366. },
  367. {
  368. id: 'barColor', // 用于区分是图表的什么部分
  369. type: 'bar',
  370. barWidth: 30,
  371. z: 10,
  372. itemStyle: {
  373. normal: {
  374. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  375. {
  376. offset: 0,
  377. color: option.seriesCustom.barColor1
  378. },
  379. {
  380. offset: 1,
  381. color: option.seriesCustom.barColor2
  382. }
  383. ]),
  384. opacity: 0.8,
  385. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  386. shadowBlur: 0 // 阴影模糊值
  387. }
  388. },
  389. data: yData
  390. },
  391. {
  392. id: 'shadowColor', // 用于区分是图表的什么部分
  393. type: 'bar',
  394. barWidth: 30,
  395. xAxisIndex: 1,
  396. itemStyle: {
  397. normal: {
  398. color: option.seriesCustom.shadowColor,
  399. opacity: 0.8,
  400. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  401. shadowBlur: 0 // 阴影模糊值
  402. }
  403. },
  404. label: {
  405. show: false
  406. },
  407. tooltip: {
  408. show: false
  409. },
  410. data: shadowData
  411. },
  412. {
  413. id: 'shadowTopColor', // 用于区分是图表的什么部分
  414. type: 'pictorialBar', // 象形柱图
  415. xAxisIndex: 1,
  416. symbol: 'diamond',
  417. symbolOffset: [0, '-50%'], // 上部菱形
  418. symbolSize: [30, 15],
  419. // symbolOffset: [0, -6], // 上部椭圆
  420. symbolPosition: 'end',
  421. z: 12,
  422. label: {
  423. normal: {
  424. show: false,
  425. position: 'top',
  426. fontSize: 15,
  427. fontWeight: 'bold',
  428. color: '#27a7ce'
  429. }
  430. },
  431. color: option.seriesCustom.shadowTopColor,
  432. tooltip: {
  433. show: false
  434. },
  435. data: shadowData
  436. }
  437. ]
  438. }
  439. return option
  440. },
  441. // 对series里面的样式进行配置
  442. seriesStyle (config) {
  443. const _config = CloneDeep(config)
  444. // 如果
  445. // const ids = Object.keys(config.option.seriesCustom)
  446. const ids = ['barTopColor', 'barBottomColor', 'shadowColor', 'shadowTopColor']
  447. _config.option.series.forEach((item) => {
  448. if (ids.includes(item.id)) {
  449. item.color = _config.option.seriesCustom[item.id]
  450. } else {
  451. item.itemStyle.normal.color = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  452. {
  453. offset: 0,
  454. color: _config.option.seriesCustom.barColor1
  455. },
  456. {
  457. offset: 1,
  458. color: _config.option.seriesCustom.barColor2
  459. }
  460. ])
  461. }
  462. })
  463. return _config
  464. },
  465. // 组件的样式改变,返回改变后的config
  466. changeStyle (config, isUpdateTheme) {
  467. config = { ...this.config, ...config }
  468. config = this.transformSettingToOption(config, 'custom')
  469. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  470. const option = config.option
  471. const setting = config.setting
  472. if (this.config.optionHandler) {
  473. try {
  474. // 此处函数处理config
  475. eval(this.config.optionHandler)
  476. } catch (e) {
  477. console.error(e)
  478. }
  479. }
  480. config = this.seriesStyle(config)
  481. // 只有样式改变时更新主题配置,切换主题时不需要保存
  482. if (!isUpdateTheme) {
  483. config.theme = settingToTheme(_.cloneDeep(config), this.customTheme)
  484. }
  485. this.changeChartConfig(config)
  486. if (config.code === this.activeCode) {
  487. this.changeActiveItemConfig(config)
  488. }
  489. if (this.chart) {
  490. this.chart.setOption(config.option)
  491. }
  492. return config
  493. }
  494. }
  495. }
  496. </script>
  497. <style lang="scss" scoped>
  498. @import '../assets/style/echartStyle';
  499. .light-theme{
  500. background-color: #FFFFFF;
  501. color: #000000;
  502. }
  503. .auto-theme{
  504. background-color: transparent;
  505. }
  506. </style>