index.vue 18 KB

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