index.vue 6.3 KB

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