index.vue 5.7 KB

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