index.vue 17 KB

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