schangxiang@126.com
2024-12-19 4422008672f79f74841e11f20430c5e76686e293
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
<template>
  <div class="home-index-module-c-compontent">
    3
  </div>
</template>
 
<script>
import * as echarts from 'echarts'
import { GetPalceNumbers } from '@/api/modular/system/homeManage'
export default {
  name: 'HomeIndexModuleCCompontent',
  data() {
    return {
      $dom: null,
      chart: null,
      defaultOption: {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            show: false
        },
        title: {
            text: '80.02%',
            left: 'center',
            top: '45%',
            textStyle: {
                color: '#000',
                fontSize: 30,
                align: 'center'
            }
        },
        graphic: {
            type: 'text',
            left: 'center',
            top: '38%',
            style: {
                text: '存货占比',
                textAlign: 'center',
                fill: '#929292',
                fontSize: 18
            }
        },
        series: [
            {
                name: '库存占比',
                type: 'pie',
                radius: ['60px', '80px'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 4,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    formatter: '{fc|{b}}\n{hr|}\n{fc|{d}%} ',
                    rich: {
                        fc: {
                            color: '#333',
                            fontSize: 12,
                            lineHeight: 16,
                            align: 'center'
                        },
                        hr: {
                            borderColor: '#666',
                            width: '100%',
                            borderWidth: 1,
                            height: 0
                        }
                    }
                },
                labelLine: {
                    length: 10,
                    length2: 5
                },
                data: []
            }
        ]
      },
      chartData: {
        total: 0,
        list: [],
        totalRate: '1%'
      }
    }
  },
  methods: {
    init() {
      this.getData((f) => {
          if (f) {
              this.createChart()
              this.drawChart()
          }
      })
    },
    getData(callback) {
      GetPalceNumbers().then((d) => {
        const _arr = []; let _obj = {}
        _obj = { text: '空库位', num: (d.data.emptyNum || 0), color: ['#5988e5', '#36cfdc'] }
        _arr.push(_obj)
        _obj = { text: '有货库位', num: (d.data.cunhuoNum || 0), color: ['#f9cf21', '#f84302'] }
        _arr.push(_obj)
        _obj = { text: '空托库位', num: (d.data.emptyContainerNum || 0), color: ['#66b53d', '#d4f9a6'] }
        _arr.push(_obj)
        this.chartData.list = _arr
        let _total = 0
        this.chartData.list.forEach((item) => {
          _total = _total + item.num
        })
         this.chartData.total = _total
        const _hasNum = d.data.cunhuoNum || 0
        this.chartData.totalRate = _total ? (Number(((_hasNum / _total) * 100).toFixed(2)) + '%') : '0%'
        callback && callback(true)
      }).catch(() => {
        callback && callback(false)
      })
    },
    drawChart() {
      const arr = []
      this.chartData.list.forEach((item) => {
          arr.push({
          value: item.num,
          name: item.text,
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ offset: 0, color: item.color[0] }, { offset: 1, color: item.color[1] }])
            }
          }
        })
      })
      const opt = { ...this.defaultOption }
      opt.series[0].data = arr
      opt.title.text = this.chartData.totalRate
      this.chart.setOption(opt)
    },
    createChart() {
      if (!this.$dom) {
          this.$dom = this.$refs.chart
          this.chart = echarts.init(this.$dom)
      }
    },
    destoryChart() {
      try {
          this.chart.dispose()
          this.$dom.innerHTML = ''
          this.$dom = null
      } catch (e) {
          // TODO handle the exception
      }
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.destoryChart()
  }
}
</script>
 
<style lang="less" scoped>
.home-index-module-c-compontent{
  height: 500px;
  background-color: skyblue;
}
</style>