index.vue 6.2 KB

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