index.vue 18 KB

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