index.vue 18 KB

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