index.vue 6.0 KB

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