index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. <!-- <span style="color:#ffffff">{{config.option.data}}</span>-->
  12. </div>
  13. </template>
  14. <script>
  15. import 'insert-css'
  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, true)
  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', 'changeChartLoading']),
  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. config.loading = true
  90. this.changeChartLoading(config)
  91. this.changeDataByCode(config).then((res) => {
  92. // 初始化图表
  93. config.loading = false
  94. this.changeChartLoading(config)
  95. this.newChart(res)
  96. }).catch(() => {
  97. })
  98. } else {
  99. config.loading = true
  100. this.changeChartLoading(config)
  101. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  102. this.changeData(config).then((res) => {
  103. config.loading = false
  104. this.changeChartLoading(config)
  105. // 初始化图表
  106. this.newChart(res)
  107. })
  108. }
  109. },
  110. /**
  111. * 构造chart
  112. */
  113. newChart (config) {
  114. this.chart = new g2Plot[config.chartType](this.chatId, {
  115. renderer: 'svg',
  116. // 仪表盘缩放状态下,点击准确
  117. supportCSSTransform: true,
  118. ...config.option
  119. })
  120. this.chart.render()
  121. this.registerEvent()
  122. },
  123. /**
  124. * 注册事件
  125. */
  126. registerEvent () {
  127. // 图表添加事件进行数据联动
  128. let formData = {}
  129. // eslint-disable-next-line no-unused-vars
  130. this.chart.on('tooltip:change', (...args) => {
  131. formData = {}
  132. formData = cloneDeep(args[0].data.items[0].data)
  133. })
  134. // eslint-disable-next-line no-unused-vars
  135. this.chart.on('plot:click', (...args) => {
  136. this.linkage(formData)
  137. })
  138. },
  139. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  140. transformSettingToOption (config, type) {
  141. let option = null
  142. config.setting.forEach(set => {
  143. if (set.optionField) {
  144. // 例 point.style.fill
  145. const optionField = set.optionField.split('.')
  146. // 例 [point,style,fill]
  147. option = config.option
  148. optionField.forEach((field, index) => {
  149. if (index === optionField.length - 1) {
  150. // 数据配置时,必须有值才更新
  151. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  152. option[field] = set.value
  153. }
  154. } else {
  155. // 如果没有这个属性,则创建该属性,并赋值为空对值
  156. if (!option[field]) {
  157. option[field] = {}
  158. }
  159. option = option[field]
  160. }
  161. })
  162. }
  163. })
  164. return config
  165. },
  166. dataFormatting (config, data) {
  167. // 数据返回成功则赋值
  168. if (data.success) {
  169. data = data.data || []
  170. config = this.transformSettingToOption(config, 'data')
  171. // 获取到后端返回的数据,有则赋值
  172. const option = config.option
  173. const setting = config.setting
  174. if (config.dataHandler) {
  175. try {
  176. // 此处函数处理data
  177. eval(config.dataHandler)
  178. } catch (e) {
  179. console.error(e)
  180. }
  181. }
  182. if (config.chartType == 'Treemap') {
  183. const xAxis = config.setting.find(item => item.field === 'xField')?.value
  184. const listData = data.children.map(item => {
  185. if (xAxis && typeof item[xAxis] === 'number') {
  186. item[xAxis] = (item[xAxis]).toString()
  187. }
  188. return item
  189. })
  190. config.option.data = { name: 'root', children: [...listData] }
  191. // console.log(config.option.data)
  192. } else {
  193. // 如果维度为数字类型则转化为字符串,否则在不增加其他配置的情况下会导致图标最后一项不显示(g2plot官网已说明)
  194. const xAxis = config.setting.find(item => item.field === 'xField')?.value
  195. const yAxis = config.setting.find(item => item.field === 'yField')?.value
  196. config.option.data = data?.map(item => {
  197. // 此处函数处理data
  198. if (config.dataHandler) {
  199. try {
  200. // 此处函数处理data
  201. eval(config.dataHandler)
  202. } catch (e) {
  203. console.error(e)
  204. }
  205. }
  206. if (config.chartType !== 'Bar' && xAxis && typeof item[xAxis] === 'number') {
  207. item[xAxis] = (item[xAxis]).toString()
  208. } else if (config.chartType === 'Bar' && yAxis && typeof item[yAxis] === 'number') {
  209. item[yAxis] = (item[yAxis]).toString()
  210. }
  211. return item
  212. })
  213. }
  214. } else {
  215. // 数据返回失败则赋前端的模拟数据
  216. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config?.option?.data
  217. const _xField = this.plotList?.find(plot => plot.name === config.name)?.option?.xField || config?.option?.xField
  218. const _yField = this.plotList?.find(plot => plot.name === config.name)?.option?.yField || config?.option?.yField
  219. const _seriesField = this.plotList?.find(plot => plot.name === config.name)?.option?.seriesField || config?.option?.seriesField
  220. config.option = _seriesField ? { ...config.option, xField: _xField, yField: _yField, seriesField: _seriesField } : { ...config.option, xField: _xField, yField: _yField }
  221. }
  222. return config
  223. },
  224. // 组件的样式改变,返回改变后的config
  225. changeStyle (config, isUpdateTheme) {
  226. config = { ...this.config, ...config }
  227. config = this.transformSettingToOption(config, 'custom')
  228. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  229. const option = config.option
  230. const setting = config.setting
  231. if (this.config.optionHandler) {
  232. try {
  233. // 此处函数处理config
  234. eval(this.config.optionHandler)
  235. } catch (e) {
  236. console.error(e)
  237. }
  238. }
  239. // 只有样式改变时更新主题配置,切换主题时不需要保存
  240. if (!isUpdateTheme) {
  241. config.theme = settingToTheme(_.cloneDeep(config), this.customTheme)
  242. }
  243. this.changeChartConfig(config)
  244. if (config.code === this.activeCode) {
  245. this.changeActiveItemConfig(config)
  246. }
  247. if (this.chart) {
  248. this.chart.update(config.option)
  249. }
  250. return config
  251. }
  252. }
  253. }
  254. </script>
  255. <style lang="scss" scoped>
  256. @import '../assets/style/echartStyle';
  257. .light-theme{
  258. background-color: #ffffff;
  259. color: #000000;
  260. }
  261. .auto-theme{
  262. background-color: transparent;
  263. }
  264. </style>