index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 _ from 'lodash'
  16. import linkageMixins from 'packages/js/mixins/linkageMixins'
  17. import commonMixins from 'packages/js/mixins/commonMixins'
  18. import { mapState, mapMutations } from 'vuex'
  19. import * as g2Plot from '@antv/g2plot'
  20. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  21. export default {
  22. name: 'PlotCustomComponent',
  23. mixins: [commonMixins, linkageMixins],
  24. props: {
  25. config: {
  26. type: Object,
  27. default: () => ({})
  28. }
  29. },
  30. data () {
  31. return {
  32. chart: null,
  33. hasData: false,
  34. plotList
  35. }
  36. },
  37. computed: {
  38. ...mapState('bigScreen', {
  39. pageInfo: state => state.pageInfo,
  40. customTheme: state => state.pageInfo.pageConfig.customTheme
  41. }),
  42. chatId () {
  43. let prefix = 'chart_'
  44. if (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) {
  45. prefix = 'preview_chart_'
  46. }
  47. if (this.$route.path === window?.BS_CONFIG?.routers?.designUrl) {
  48. prefix = 'design_chart_'
  49. }
  50. if (this.$route.path === window?.BS_CONFIG?.routers?.pageListUrl) {
  51. prefix = 'management_chart_'
  52. }
  53. return prefix + this.config.code
  54. }
  55. },
  56. created () {
  57. this.plotList = [...this.plotList, ...getCustomPlots()]
  58. },
  59. mounted () {
  60. this.chartInit()
  61. },
  62. beforeDestroy () {
  63. if (this.chart) {
  64. this.chart.destroy()
  65. }
  66. },
  67. methods: {
  68. ...mapMutations('bigScreen', ['changeChartConfig']),
  69. chartInit () {
  70. // key和code相等,说明是一进来刷新,调用/chart/data/list
  71. if (this.config.code === this.config.key || this.isPreview) {
  72. // 先给默认数据, 渲染出图表
  73. const config = _.cloneDeep(this.config)
  74. // config.option = plotList.find(plot => plot.name === config.name)?.option
  75. this.newChart(config.option)
  76. // 再根据数据更新组件
  77. // this.updateChart()
  78. this.updateData()
  79. } else {
  80. // 否则说明是更新或者复制
  81. this.newChart(this.config.option)
  82. }
  83. },
  84. /**
  85. * 构造chart
  86. */
  87. newChart (option) {
  88. this.chart = new g2Plot[this.config.chartType](this.chatId, {
  89. renderer: 'svg',
  90. // 大屏缩放状态下,点击准确
  91. supportCSSTransform: true,
  92. ...option || this.config.option
  93. })
  94. this.chart.render()
  95. this.registerEvent()
  96. },
  97. // 根据G2Plot组件配置的dataKey,将获取到的数据赋值给指定的dataKey
  98. updateData () {
  99. this.getCurrentOption().then(({ data, config }) => {
  100. if (data.success) {
  101. // 成功后更新数据
  102. config = this.buildOption(config, data)
  103. const dataKey = config.option.dataKey
  104. // eslint-disable-next-line no-inner-declarations
  105. function getValueFromOption (option, dataKey) {
  106. try {
  107. return eval('option.' + dataKey)
  108. } catch (error) {
  109. return undefined
  110. }
  111. }
  112. const newData = getValueFromOption(config.option, dataKey)
  113. if (this.chart) {
  114. this.chart.changeData(newData)
  115. }
  116. }
  117. })
  118. },
  119. /**
  120. * 更新组件
  121. */
  122. // updateChart () {
  123. // if (this.isPreview) {
  124. // this.getCurrentOption().then(({ data, config }) => {
  125. // if (data.success) {
  126. // // 成功后更新数据
  127. // config = this.buildOption(config, data)
  128. // this.changeChartConfig(config)
  129. // this.chart.update(config.option)
  130. // } else {
  131. // config.option.data = this.plotList.find(plot => plot.name === config.name)?.option.data
  132. // this.chart.update(config.option)
  133. // }
  134. // })
  135. // } else {
  136. // this.updateChartData(this.config)
  137. // }
  138. // },
  139. /**
  140. * 注册事件
  141. */
  142. registerEvent () {
  143. // 图表添加事件进行数据联动
  144. let formData = {}
  145. // eslint-disable-next-line no-unused-vars
  146. this.chart.on('tooltip:change', (...args) => {
  147. formData = {}
  148. formData = _.cloneDeep(args[0].data.items[0].data)
  149. })
  150. // eslint-disable-next-line no-unused-vars
  151. this.chart.on('plot:click', (...args) => {
  152. this.linkage(formData)
  153. })
  154. },
  155. /**
  156. * 组件的配置
  157. * @returns {Promise<unknown>}
  158. */
  159. buildOption (config, data) {
  160. config = _.cloneDeep(config)
  161. // 遍历config.setting,将config.setting中的值赋值给config.option中对应的optionField
  162. config.setting.forEach(set => {
  163. if (set.optionField) {
  164. const optionField = set.optionField.split('.')
  165. let option = config.option
  166. optionField.forEach((field, index) => {
  167. if (index === optionField.length - 1) {
  168. // 数据配置时,必须有值才更新
  169. if ((set.tabName === 'data' && set.value) || set.tabName === 'custom') {
  170. option[field] = set.value
  171. }
  172. } else {
  173. option = option[field]
  174. }
  175. })
  176. }
  177. })
  178. // eslint-disable-next-line no-unused-vars
  179. const option = config.option
  180. // eslint-disable-next-line no-unused-vars
  181. // const setting = config.setting
  182. if (this.config.optionHandler) {
  183. try {
  184. // 此处函数处理config
  185. eval(this.config.optionHandler)
  186. } catch (e) {
  187. console.error(e)
  188. }
  189. }
  190. if (data.success) {
  191. data = data.data
  192. if (this.config.dataHandler) {
  193. try {
  194. // 此处函数处理data
  195. eval(this.config.dataHandler)
  196. } catch (e) {
  197. console.error(e)
  198. }
  199. }
  200. // eslint-disable-next-line no-undef
  201. config.option = option
  202. config.option.data = data
  203. } else {
  204. // 数据获取失败,使用前端配置中的默认数据
  205. config.option.data = this.plotList?.find(plot => plot.name === this.config.name)?.option?.data
  206. }
  207. return config
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. @import '~packages/assets/style/echartStyle';
  214. .light-theme{
  215. background-color: #FFFFFF;
  216. color: #000000;
  217. }
  218. .auto-theme{
  219. background-color: rgba(0,0,0,0);
  220. }
  221. </style>