index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <!--
  2. @name:
  3. @description:
  4. @author: byx
  5. @time: 桑基图
  6. -->
  7. <template>
  8. <div
  9. :id="config.code + 'wrap'"
  10. class="bs-design-wrap bs-bar"
  11. style="width: 100%; height: 100%"
  12. >
  13. <div
  14. :id="`chart${config.code}`"
  15. style="width: 100%; height: 100%"
  16. />
  17. </div>
  18. </template>
  19. <script>
  20. import 'insert-css'
  21. import * as echarts from 'echarts'
  22. import commonMixins from 'data-room-ui/js/mixins/commonMixins.js'
  23. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  24. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  25. export default {
  26. name: 'Sankey',
  27. mixins: [paramsMixins, commonMixins, linkageMixins],
  28. props: {
  29. id: {
  30. type: String,
  31. default: ''
  32. },
  33. config: {
  34. type: Object,
  35. default: () => ({})
  36. }
  37. },
  38. data () {
  39. return {
  40. currentDeep: 0,
  41. mapList: [],
  42. charts: null,
  43. hasData: false,
  44. level: '',
  45. option: {},
  46. links: [],
  47. yData: []
  48. }
  49. },
  50. computed: {
  51. },
  52. watch: {
  53. },
  54. mounted () {
  55. this.chartInit()
  56. // 监听窗口或者父盒子大小变化
  57. this.chartResize()
  58. },
  59. beforeDestroy () {
  60. this.charts?.clear()
  61. },
  62. methods: {
  63. chartResize () {
  64. window.addEventListener('resize', () => {
  65. if (this.charts) {
  66. this.charts.resize()
  67. }
  68. })
  69. const dom = document.getElementById(`${this.config.code}wrap`)
  70. if (dom) {
  71. this.resizeObserver = new ResizeObserver(() => {
  72. if (this.charts) {
  73. this.charts.resize()
  74. }
  75. })
  76. this.resizeObserver.observe(dom)
  77. }
  78. },
  79. chartInit () {
  80. const config = this.config
  81. // key和code相等,说明是一进来刷新,调用list接口
  82. if (this.config.code === this.config.key || this.isPreview) {
  83. // 改变数据
  84. this.changeDataByCode(config).then((res) => {
  85. // 改变样式
  86. // config = this.changeStyle(res)
  87. this.newChart(config)
  88. }).catch(() => {
  89. })
  90. } else {
  91. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  92. this.changeData(config).then((res) => {
  93. // 初始化图表
  94. this.newChart(config)
  95. })
  96. }
  97. },
  98. /**
  99. * 数据格式化
  100. * 该方法继承自commonMixins
  101. * @param {*} config
  102. * @param {Array} data
  103. */
  104. dataFormatting (config, _data) {
  105. const data = _data?.data
  106. if (data && data.length) {
  107. const list = data.map(item => [item[config.dataSource.dimensionField], item[config.dataSource.metricField]])?.flat() || []
  108. // 去重
  109. this.yData = [...new Set(list)]?.map(item => ({ name: item }))
  110. this.links = data.map(item => ({
  111. source: item[config.dataSource.dimensionField],
  112. target: item[config.dataSource.metricField],
  113. value: item[config.dataSource.seriesField]
  114. }))
  115. } else {
  116. this.links = [
  117. {
  118. source: 'a',
  119. target: 'a1',
  120. value: 5
  121. },
  122. {
  123. source: 'a',
  124. target: 'a2',
  125. value: 3
  126. },
  127. {
  128. source: 'b',
  129. target: 'b1',
  130. value: 8
  131. },
  132. {
  133. source: 'a',
  134. target: 'b1',
  135. value: 3
  136. },
  137. {
  138. source: 'b1',
  139. target: 'a1',
  140. value: 1
  141. },
  142. {
  143. source: 'b1',
  144. target: 'c',
  145. value: 2
  146. }
  147. ]
  148. this.yData = [
  149. { name: 'a' },
  150. { name: 'b' },
  151. { name: 'a1' },
  152. { name: 'a2' },
  153. { name: 'b1' },
  154. { name: 'c' }
  155. ]
  156. }
  157. return config
  158. },
  159. // 更新图表数据
  160. updateChartData (config) {
  161. this.handleOption(config)
  162. this.charts.setOption(this.option)
  163. },
  164. changeStyle (config) {
  165. this.handleOption(config)
  166. this.charts.setOption(this.option)
  167. },
  168. /**
  169. * 初始化地图
  170. * 该方法继承自commonMixins
  171. * @param {*} config
  172. */
  173. async newChart (config) {
  174. if (this.charts) {
  175. this.charts.dispose()
  176. }
  177. this.charts = echarts.init(
  178. document.getElementById(`chart${this.config.code}`)
  179. )
  180. // 处理option,将配置项转换为echarts的option
  181. this.handleOption(config)
  182. this.charts.setOption(this.option)
  183. },
  184. /**
  185. * 处理配置项option
  186. * @param {*} config
  187. */
  188. handleOption (config) {
  189. this.option = {
  190. tooltip: {
  191. // 显示提示框
  192. show: true,
  193. trigger: 'item',
  194. axisPointer: {
  195. type: 'line'
  196. }
  197. },
  198. series: [
  199. {
  200. type: 'sankey',
  201. top: config.customize.top || 20,
  202. bottom: config.customize.bottom || 20,
  203. left: config.customize.left || 20,
  204. right: config.customize.right || 50,
  205. label: {
  206. show: true,
  207. position: config.customize.normal.labelPosition || 'right',
  208. color: config.customize.normal.labelColor || '#fff',
  209. fontSize: config.customize.normal.labelSize || 12,
  210. fontWeight: config.customize.normal.labelFontWeight || 'bold'
  211. },
  212. itemStyle: {
  213. borderColor: config.customize.normal.itemBorderColor || '#aaa',
  214. borderWidth: config.customize.normal.itemBorderWidth || 1,
  215. borderType: config.customize.normal.itemBorderType || 'solid'
  216. },
  217. lineStyle: {
  218. color: config.customize.normal.lineColor || 'gradient',
  219. curveness: config.customize.normal.lineCurveness || 0.5// 图形的弯曲程度
  220. },
  221. emphasis: { // 聚焦文字时产生的高亮状态
  222. disabled: false,
  223. focus: 'trajectory',
  224. label: {
  225. color: config.customize.emphasis.labelColor || '#fff',
  226. fontSize: config.customize.emphasis.labelSize || 12,
  227. fontWeight: config.customize.normal.labelFontWeight || 'bold',
  228. },
  229. itemStyle: {
  230. borderColor: config.customize.emphasis.itemBorderColor || '#aaa',
  231. borderWidth: config.customize.emphasis.itemBorderWidth || 1,
  232. borderType: config.customize.emphasis.itemBorderType || 'solid'
  233. },
  234. lineStyle: {
  235. color: config.customize.emphasis.lineColor || 'gradient'
  236. }
  237. },
  238. data: this.yData,
  239. links: this.links
  240. }
  241. ]
  242. }
  243. }
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. @import '../../assets/style/echartStyle';
  249. .light-theme {
  250. background-color: #ffffff;
  251. color: #000000;
  252. }
  253. .auto-theme {
  254. background-color: rgba(0, 0, 0, 0);
  255. }
  256. .bs-design-wrap {
  257. position: relative;
  258. .button {
  259. position: absolute;
  260. z-index: 999;
  261. }
  262. }
  263. </style>