index.vue 15 KB

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