index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. } else {
  79. // 否则说明是更新或者复制
  80. this.newChart(this.config.option)
  81. }
  82. },
  83. /**
  84. * 构造chart
  85. */
  86. newChart (option) {
  87. this.chart = new g2Plot[this.config.chartType](this.chatId, {
  88. renderer: 'svg',
  89. // 大屏缩放状态下,点击准确
  90. supportCSSTransform: true,
  91. ...option || this.config.option
  92. })
  93. this.chart.render()
  94. this.registerEvent()
  95. },
  96. /**
  97. * @description: 只更新数据
  98. */
  99. updateData () {
  100. this.getCurrentOption().then(({ data, config }) => {
  101. if (data.success) {
  102. // 成功后更新数据
  103. config = this.buildOption(config, data)
  104. const dataKey = config.option.dataKey
  105. // eslint-disable-next-line no-inner-declarations
  106. function getValueFromOption (option, dataKey) {
  107. try {
  108. return eval('option.' + dataKey)
  109. } catch (error) {
  110. return undefined
  111. }
  112. }
  113. const newData = getValueFromOption(config.option, dataKey)
  114. if (this.chart) {
  115. this.chart.changeData(newData)
  116. }
  117. }
  118. })
  119. },
  120. /**
  121. * 更新组件
  122. */
  123. updateChart () {
  124. if (this.isPreview) {
  125. this.getCurrentOption().then(({ data, config }) => {
  126. if (data.success) {
  127. // 成功后更新数据
  128. config = this.buildOption(config, data)
  129. this.changeChartConfig(config)
  130. this.chart.update(config.option)
  131. } else {
  132. config.option.data = this.plotList.find(plot => plot.name === config.name)?.option.data
  133. this.chart.update(config.option)
  134. }
  135. })
  136. } else {
  137. this.updateChartData(this.config)
  138. }
  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. /**
  157. * 组件的配置
  158. * @returns {Promise<unknown>}
  159. */
  160. buildOption (config, data) {
  161. config = _.cloneDeep(config)
  162. // 遍历config.setting,将config.setting中的值赋值给config.option中对应的optionField
  163. config.setting.forEach(set => {
  164. if (set.optionField) {
  165. const optionField = set.optionField.split('.')
  166. let option = config.option
  167. optionField.forEach((field, index) => {
  168. if (index === optionField.length - 1) {
  169. // 数据配置时,必须有值才更新
  170. if ((set.tabName === 'data' && set.value) || set.tabName === 'custom') {
  171. option[field] = set.value
  172. }
  173. } else {
  174. option = option[field]
  175. }
  176. })
  177. }
  178. })
  179. // eslint-disable-next-line no-unused-vars
  180. const option = config.option
  181. // eslint-disable-next-line no-unused-vars
  182. const setting = config.setting
  183. if (this.config.optionHandler) {
  184. try {
  185. // 此处函数处理config
  186. eval(this.config.optionHandler)
  187. } catch (e) {
  188. console.error(e)
  189. }
  190. }
  191. if (data.success) {
  192. data = data.data
  193. if (this.config.dataHandler) {
  194. try {
  195. // 此处函数处理data
  196. eval(this.config.dataHandler)
  197. } catch (e) {
  198. console.error(e)
  199. }
  200. }
  201. // eslint-disable-next-line no-undef
  202. config.option = option
  203. config.option.data = data
  204. } else {
  205. // 数据获取失败,使用前端配置中的默认数据
  206. config.option.data = this.plotList?.find(plot => plot.name === this.config.name)?.option?.data
  207. }
  208. return config
  209. }
  210. }
  211. }
  212. </script>
  213. <style lang="scss" scoped>
  214. @import '~packages/assets/style/echartStyle';
  215. .light-theme{
  216. background-color: #FFFFFF;
  217. color: #000000;
  218. }
  219. .auto-theme{
  220. background-color: rgba(0,0,0,0);
  221. }
  222. </style>