index.vue 6.5 KB

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