index.vue 6.4 KB

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