index.vue 17 KB

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