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
| <template>
| <div class="chart-container">
| <div class="chart-title">各品类仓库占用资金</div>
| <div :id="chartId" :class="className" :style="{width:width,height:height}" />
| </div>
| </template>
|
| <script>
| import echarts from 'echarts'
| require('echarts/theme/macarons') // echarts theme
|
| export default {
| props: {
| className: {
| type: String,
| default: 'chart'
| },
| width: {
| type: String,
| default: '100%'
| },
| height: {
| type: String,
| default: '1400px'
| },
| occupiedFundsData: {
| type: Object,
| default: () => ({})
| }
| },
| data() {
| return {
| chart: null,
| chartId: 'bar-chart-' + Math.random().toString(36).substr(2),
| resizeHandler: null
| }
| },
| watch: {
| occupiedFundsData: {
| handler() {
| if (this.chart) {
| this.updateChart()
| }
| },
| deep: true
| }
| },
| mounted() {
| this.initChart()
| // this.resizeHandler = debounce(() => {
| // if (this.chart) {
| // this.chart.resize()
| // }
| // }, 100)
| // window.addEventListener('resize', this.resizeHandler)
| },
| beforeDestroy() {
| if (!this.chart) {
| return
| }
| window.removeEventListener('resize', this.resizeHandler)
| this.chart.dispose()
| this.chart = null
| },
| methods: {
| initChart() {
| this.chart = echarts.init(document.getElementById(this.chartId), 'macarons')
| this.updateChart()
| },
| updateChart() {
| const chartData = Object.entries(this.occupiedFundsData)
| .map(([category, value]) => ({
| category,
| occupiedFunds: this.toNumber(value && value.occupiedFunds),
| inStore: this.toNumber(value && value.inStore)
| }))
| .sort((a, b) => b.occupiedFunds - a.occupiedFunds)
| const yAxisData = chartData.map(item => item.category)
| const fundsData = chartData.map(item => ({
| value: item.occupiedFunds,
| inStore: item.inStore
| }))
|
| const option = {
| tooltip: {
| trigger: 'axis',
| axisPointer: {
| type: 'shadow'
| },
| formatter: params => {
| const item = params && params[0]
| if (!item) {
| return ''
| }
| return `${item.name}<br/>仓库占用资金:${this.formatAmount(item.value)}<br/>库存数量:${item.data.inStore}`
| }
| },
| grid: {
| top: 40,
| left: '2%',
| right: '4%',
| bottom: '3%',
| containLabel: true
| },
| xAxis: {
| type: 'value',
| boundaryGap: [0, 0.01],
| axisLabel: {
| formatter: value => this.formatAmount(value)
| }
| },
| yAxis: {
| type: 'category',
| data: yAxisData,
| // 横向柱状图默认将首项显示在最下方;反转坐标轴后,
| // 已按金额降序排列的首项(最大值)会显示在最上方。
| inverse: true
| },
| series: [
| {
| name: '仓库占用资金',
| type: 'bar',
| barMaxWidth: 35,
| data: fundsData,
| barMinHeight: 1,
| label: {
| show: true,
| position: 'right',
| formatter: params => this.formatAmount(params.value)
| }
| }
| ]
| }
|
| if (yAxisData.length > 20) {
| option.dataZoom = [
| {
| type: 'slider',
| yAxisIndex: 0,
| filterMode: 'empty',
| start: 0,
| end: 20 / yAxisData.length * 100,
| width: 15
| }
| ]
| }
|
| this.chart.setOption(option)
| },
| toNumber(value) {
| const number = Number(value)
| return Number.isFinite(number) ? number : 0
| },
| formatAmount(value) {
| return `¥${this.toNumber(value).toFixed(2)}`
| }
| }
| }
| </script>
|
| <style scoped>
| .chart-container {
| background: #fff;
| padding: 16px;
| position: relative;
| height: 1438px;
| display: flex;
| flex-direction: column;
| }
| .chart-title {
| font-size: 16px;
| font-weight: bold;
| margin-bottom: 10px;
| }
| .chart {
| flex-grow: 1;
| }
| </style>
|
|