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