index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <div
  3. v-loading="config.loading"
  4. element-loading-text="图表加载中"
  5. :element-loading-background="loadingBackground"
  6. style="width: 100%;height: 100%"
  7. class="bs-design-wrap bs-custom-component"
  8. :class="{'light-theme':customTheme === 'light','auto-theme':customTheme !=='light'}"
  9. >
  10. <div
  11. :id="chatId"
  12. style="width: 100%;height: 100%"
  13. />
  14. <!-- <span style="color:#ffffff">{{config.option.data}}</span>-->
  15. </div>
  16. </template>
  17. <script>
  18. import 'insert-css'
  19. import cloneDeep from 'lodash/cloneDeep'
  20. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  21. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  22. import { mapState, mapMutations } from 'vuex'
  23. import * as g2Plot from '@antv/g2plot'
  24. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  25. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  26. import _ from 'lodash'
  27. export default {
  28. name: 'PlotCustomComponent',
  29. mixins: [commonMixins, linkageMixins],
  30. props: {
  31. config: {
  32. type: Object,
  33. default: () => ({})
  34. }
  35. },
  36. data () {
  37. return {
  38. chart: null,
  39. hasData: false,
  40. plotList
  41. }
  42. },
  43. computed: {
  44. ...mapState('bigScreen', {
  45. pageInfo: state => state.pageInfo,
  46. customTheme: state => state.pageInfo.pageConfig.customTheme,
  47. activeCode: state => state.activeCode
  48. }),
  49. chatId () {
  50. let prefix = 'chart_'
  51. if (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) {
  52. prefix = 'preview_chart_'
  53. }
  54. if (this.$route.path === window?.BS_CONFIG?.routers?.designUrl) {
  55. prefix = 'design_chart_'
  56. }
  57. if (this.$route.path === window?.BS_CONFIG?.routers?.pageListUrl) {
  58. prefix = 'management_chart_'
  59. }
  60. return prefix + this.config.code
  61. }
  62. },
  63. created () {
  64. this.plotList = [...this.plotList, ...getCustomPlots()]
  65. },
  66. watch: {
  67. // 监听主题变化手动触发组件配置更新
  68. 'config.option.theme': {
  69. handler (val) {
  70. if (val) {
  71. this.changeStyle(this.config, true)
  72. }
  73. }
  74. }
  75. },
  76. mounted () {
  77. },
  78. beforeDestroy () {
  79. if (this.chart) {
  80. this.chart.destroy()
  81. }
  82. },
  83. methods: {
  84. ...mapMutations('bigScreen', ['changeChartConfig', 'changeActiveItemConfig', 'changeChartLoading']),
  85. chartInit () {
  86. let config = this.config
  87. // key和code相等,说明是一进来刷新,调用list接口
  88. if (this.config.code === this.config.key || this.isPreview) {
  89. // 改变样式
  90. config = this.changeStyle(config)
  91. // 改变数据
  92. config.loading = true
  93. this.changeChartLoading(config)
  94. this.changeDataByCode(config).then((res) => {
  95. // 初始化图表
  96. config.loading = false
  97. this.changeChartLoading(config)
  98. this.newChart(res)
  99. }).catch(() => {
  100. })
  101. } else {
  102. config.loading = true
  103. this.changeChartLoading(config)
  104. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  105. this.changeData(config).then((res) => {
  106. config.loading = false
  107. this.changeChartLoading(config)
  108. // 初始化图表
  109. this.newChart(res)
  110. })
  111. }
  112. },
  113. /**
  114. * 构造chart
  115. */
  116. newChart (config) {
  117. this.chart = new g2Plot[config.chartType](this.chatId, {
  118. renderer: 'svg',
  119. // 仪表盘缩放状态下,点击准确
  120. supportCSSTransform: true,
  121. ...config.option
  122. })
  123. this.chart.render()
  124. this.registerEvent()
  125. },
  126. /**
  127. * 注册事件
  128. */
  129. registerEvent () {
  130. // 图表添加事件进行数据联动
  131. let formData = {}
  132. // eslint-disable-next-line no-unused-vars
  133. this.chart.on('tooltip:change', (...args) => {
  134. formData = {}
  135. formData = cloneDeep(args[0].data.items[0].data)
  136. })
  137. // eslint-disable-next-line no-unused-vars
  138. this.chart.on('plot:click', (...args) => {
  139. this.linkage(formData)
  140. })
  141. },
  142. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  143. transformSettingToOption (config, type) {
  144. let option = null
  145. config.setting.forEach(set => {
  146. if (set.optionField) {
  147. const optionField = set.optionField.split('.')
  148. option = config.option
  149. optionField.forEach((field, index) => {
  150. if (index === optionField.length - 1) {
  151. // 数据配置时,必须有值才更新
  152. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  153. option[field] = set.value
  154. }
  155. } else {
  156. option = option[field]
  157. }
  158. })
  159. }
  160. })
  161. config.option = { ...config.option, ...option }
  162. return config
  163. },
  164. dataFormatting (config, data) {
  165. // 数据返回成功则赋值
  166. if (data.success) {
  167. data = data.data || []
  168. config = this.transformSettingToOption(config, 'data')
  169. // 获取到后端返回的数据,有则赋值
  170. const option = config.option
  171. const setting = config.setting
  172. if (config.dataHandler) {
  173. try {
  174. // 此处函数处理data
  175. eval(config.dataHandler)
  176. } catch (e) {
  177. console.error(e)
  178. }
  179. }
  180. if(config.chartType=='Treemap'){
  181. const xAxis = config.setting.find(item => item.field === 'xField')?.value
  182. const listData = data.children.map(item => {
  183. if (xAxis && typeof item[xAxis] === 'number') {
  184. item[xAxis] = (item[xAxis]).toString()
  185. }
  186. return item
  187. })
  188. config.option.data={name:'root',children:[...listData]}
  189. // console.log(config.option.data)
  190. }else{
  191. // 如果维度为数字类型则转化为字符串,否则在不增加其他配置的情况下会导致图标最后一项不显示(g2plot官网已说明)
  192. const xAxis = config.setting.find(item => item.field === 'xField')?.value
  193. config.option.data = data?.map(item => {
  194. if (xAxis && typeof item[xAxis] === 'number') {
  195. item[xAxis] = (item[xAxis]).toString()
  196. }
  197. return item
  198. })
  199. }
  200. } else {
  201. // 数据返回失败则赋前端的模拟数据
  202. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config?.option?.data
  203. }
  204. return config
  205. },
  206. // 组件的样式改变,返回改变后的config
  207. changeStyle (config, isUpdateTheme) {
  208. config = { ...this.config, ...config }
  209. config = this.transformSettingToOption(config, 'custom')
  210. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  211. const option = config.option
  212. const setting = config.setting
  213. if (this.config.optionHandler) {
  214. try {
  215. // 此处函数处理config
  216. eval(this.config.optionHandler)
  217. } catch (e) {
  218. console.error(e)
  219. }
  220. }
  221. // 只有样式改变时更新主题配置,切换主题时不需要保存
  222. if (!isUpdateTheme) {
  223. config.theme = settingToTheme(_.cloneDeep(config), this.customTheme)
  224. }
  225. this.changeChartConfig(config)
  226. if (config.code === this.activeCode) {
  227. this.changeActiveItemConfig(config)
  228. }
  229. if (this.chart) {
  230. this.chart.update(config.option)
  231. }
  232. return config
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. @import '../assets/style/echartStyle';
  239. .light-theme{
  240. background-color: #FFFFFF;
  241. color: #000000;
  242. }
  243. .auto-theme{
  244. background-color: transparent;
  245. }
  246. </style>