index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 cloneDeep from 'lodash/cloneDeep'
  17. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  18. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  19. import { mapState, mapMutations } from 'vuex'
  20. import * as g2Plot from '@antv/g2plot'
  21. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  22. export default {
  23. name: 'PlotCustomComponent',
  24. mixins: [commonMixins, linkageMixins],
  25. props: {
  26. config: {
  27. type: Object,
  28. default: () => ({})
  29. }
  30. },
  31. data () {
  32. return {
  33. chart: null,
  34. hasData: false,
  35. plotList
  36. }
  37. },
  38. computed: {
  39. ...mapState('bigScreen', {
  40. pageInfo: state => state.pageInfo,
  41. customTheme: state => state.pageInfo.pageConfig.customTheme
  42. }),
  43. chatId () {
  44. let prefix = 'chart_'
  45. if (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) {
  46. prefix = 'preview_chart_'
  47. }
  48. if (this.$route.path === window?.BS_CONFIG?.routers?.designUrl) {
  49. prefix = 'design_chart_'
  50. }
  51. if (this.$route.path === window?.BS_CONFIG?.routers?.pageListUrl) {
  52. prefix = 'management_chart_'
  53. }
  54. return prefix + this.config.code
  55. }
  56. },
  57. created () {
  58. this.plotList = [...this.plotList, ...getCustomPlots()]
  59. },
  60. mounted () {
  61. },
  62. beforeDestroy () {
  63. if (this.chart) {
  64. this.chart.destroy()
  65. }
  66. },
  67. methods: {
  68. ...mapMutations('bigScreen', ['changeChartConfig']),
  69. chartInit () {
  70. let config = this.config
  71. // key和code相等,说明是一进来刷新,调用list接口
  72. if (this.config.code === this.config.key || this.isPreview) {
  73. // 改变样式
  74. config = this.changeStyle(config)
  75. // 改变数据
  76. this.changeDataByCode(config).then((res) => {
  77. // 初始化图表
  78. this.newChart(res)
  79. }).catch(() => {})
  80. } else {
  81. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  82. this.changeData(config).then((res) => {
  83. // 初始化图表
  84. this.newChart(res)
  85. })
  86. }
  87. },
  88. /**
  89. * 构造chart
  90. */
  91. newChart (config) {
  92. this.chart = new g2Plot[config.chartType](this.chatId, {
  93. renderer: 'svg',
  94. // 仪表盘缩放状态下,点击准确
  95. supportCSSTransform: true,
  96. ...config.option
  97. })
  98. this.chart.render()
  99. this.registerEvent()
  100. },
  101. /**
  102. * 注册事件
  103. */
  104. registerEvent () {
  105. // 图表添加事件进行数据联动
  106. let formData = {}
  107. // eslint-disable-next-line no-unused-vars
  108. this.chart.on('tooltip:change', (...args) => {
  109. formData = {}
  110. formData = cloneDeep(args[0].data.items[0].data)
  111. })
  112. // eslint-disable-next-line no-unused-vars
  113. this.chart.on('plot:click', (...args) => {
  114. this.linkage(formData)
  115. })
  116. },
  117. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  118. transformSettingToOption (config, type) {
  119. let option = null
  120. config.setting.forEach(set => {
  121. if (set.optionField) {
  122. const optionField = set.optionField.split('.')
  123. option = config.option
  124. optionField.forEach((field, index) => {
  125. if (index === optionField.length - 1) {
  126. // 数据配置时,必须有值才更新
  127. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  128. option[field] = set.value
  129. }
  130. } else {
  131. option = option[field]
  132. }
  133. })
  134. }
  135. })
  136. config.option = { ...config.option, ...option }
  137. return config
  138. },
  139. dataFormatting (config, data) {
  140. // 数据返回成功则赋值
  141. if (data.success) {
  142. data = data.data
  143. config = this.transformSettingToOption(config, 'data')
  144. // 获取到后端返回的数据,有则赋值
  145. const option = config.option
  146. const setting = config.setting
  147. if (config.dataHandler) {
  148. try {
  149. // 此处函数处理data
  150. eval(config.dataHandler)
  151. } catch (e) {
  152. console.error(e)
  153. }
  154. }
  155. config.option.data = data
  156. } else {
  157. // 数据返回失败则赋前端的模拟数据
  158. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data
  159. }
  160. return config
  161. },
  162. // 组件的样式改变,返回改变后的config
  163. changeStyle (config) {
  164. config = { ...this.config, ...config }
  165. config = this.transformSettingToOption(config, 'custom')
  166. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  167. const option = config.option
  168. const setting = config.setting
  169. if (this.config.optionHandler) {
  170. try {
  171. // 此处函数处理config
  172. eval(this.config.optionHandler)
  173. } catch (e) {
  174. console.error(e)
  175. }
  176. }
  177. if (this.chart) {
  178. this.chart.update(config.option)
  179. }
  180. this.changeChartConfig(config)
  181. return config
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. @import '../assets/style/echartStyle';
  188. .light-theme{
  189. background-color: #FFFFFF;
  190. color: #000000;
  191. }
  192. .auto-theme{
  193. background-color: rgba(0,0,0,0);
  194. }
  195. </style>