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
| <template>
| <div id="utilizationRate-rank"></div>
| </template>
|
| <script>
| import signageApi from '@/api/signage'
|
| export default {
| name: 'UtilizationRateRankChart',
| components: {},
| data() {
| return {
| chartContainer: null,
| dataSource: []
| }
| },
| mounted() {
| window.addEventListener('resize', this.handleWindowSizeChange)
| },
| methods: {
| getUtilizationRateRankByApi() {
| const that = this
| signageApi.getUtilizationRateRankApi()
| .then(res => {
| if (res.success) {
| that.dataSource = res.result.map(item => {
| return {
| name: item.topName,
| value: item.topRate
| }
| })
| that.initChart()
| }
| })
| },
|
| initChart() {
| this.chartContainer = this.$echarts.init(document.getElementById('utilizationRate-rank'))
| const data = this.dataSource
| const colorArray = [
| {
| top: '#79CEAA',
| bottom: '#79CEAA'
| },
| {
| top: '#F589A2',
| bottom: '#F589A2'
| },
| {
| top: '#6FBF9D',
| bottom: '#6FBF9D'
| },
| {
| top: '#66DFE2',
| bottom: '#66DFE2'
| }, {
| top: '#A7F0C1',
| bottom: '#A7F0C1'
| },
| {
| top: '#FAE893',
| bottom: '#FAE893'
| },
| {
| top: '#F7B7A0',
| bottom: '#F7B7A0'
| }
| ]
| const defaultData = []
| const dataMax = data.length > 0 ? +data.sort((x, y) => +y.value - +x.value)[0].value : 0
| let yAxisMax
| if (dataMax === 0) yAxisMax = 1 // 若数据中最大值为0,则将背景默认值设置为1
| else yAxisMax = Math.ceil(dataMax / 5) * 5 // 设置柱图背景阴影默认值,思路为数据最大值最接近的能被5整除的数字
| const yAxisInterval = yAxisMax / 5 // 同时将刻度值分成5份
| data.forEach(item => defaultData.push(yAxisMax))
| const option = {
| title: {
| show: false // 是否显示标题,默认为true
| },
| grid: {
| left: '3%',
| right: '3%',
| bottom: '3%',
| top: '3%',
| containLabel: true
| },
| tooltip: {
| trigger: 'axis',
| axisPointer: {
| type: 'none'
| },
| formatter: function(params) {
| return '<span style="font-weight:bolder;">' + params[0].name + '</span><br/>' +
| '<span style="display:inline-block; width:10px; height:10px; border-radius:100px; margin-right:5px; background:' + params[0].color.colorStops[params[0].dataIndex].color + '"></span>' + params[0].seriesName + ' : ' + params[0].value + '%'
| }
| },
| xAxis: {
| name: '',
| nameTextStyle: {
| color: '#fff'
| },
| axisLabel: {
| margin: 20,
| textStyle: {
| color: '#fff'
| }
| },
| show: true,
| min: 0,
| max: 'dataMax',
| interval: yAxisInterval,
| type: 'value',
| axisTick: {
| show: false
| },
| splitLine: {
| show: false
| }
| },
| yAxis: [
| {
| type: 'category',
| inverse: true,
| triggerEvent: true,
| axisLabel: {
| show: true,
| textStyle: {
| color: '#fff',
| fontSize: '14',
| fontWeight: 'bolder'
| },
| formatter: function(value) {
| return `${data.find(item => item.name === value).name}`
| }
| },
| splitLine: {
| show: false
| },
| axisTick: {
| show: false
| },
| axisLine: {
| show: false
| },
| data: data.map(item => item.name)
| },
| {
| type: 'category',
| inverse: true,
| axisTick: 'none',
| axisLine: 'none',
| show: true,
| axisLabel: {
| textStyle: {
| color: '#ffffff',
| fontSize: '14'
| },
| formatter: function(value) {
| return `${value}%`
| }
| },
| data: data
| }
| ],
| series: [
| {
| name: '利用率',
| type: 'bar',
| zlevel: 1,
| itemStyle: {
| borderRadius: 100,
| color: function(params) {
| let num = colorArray.length
| return {
| type: 'linear',
| colorStops: [{
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }, {
| offset: 1,
| color: colorArray[params.dataIndex % num].top
| }, {
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }, {
| offset: 1,
| color: colorArray[params.dataIndex % num].top
| }, {
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }, {
| offset: 1,
| color: colorArray[params.dataIndex % num].top
| }, {
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }, {
| offset: 1,
| color: colorArray[params.dataIndex % num].top
| }, {
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }, {
| offset: 1,
| color: colorArray[params.dataIndex % num].top
| }, {
| offset: 0,
| color: colorArray[params.dataIndex % num].bottom
| }]
| }
| }
| },
| barWidth: 12,
| data: data
| },
| {
| name: '背景',
| type: 'bar',
| barWidth: 12,
| barGap: '-100%',
| data: defaultData,
| itemStyle: {
| color: '#11294d',
| borderRadius: 100
| }
| }
| ]
| }
| this.chartContainer.setOption(option, true)
| },
|
| //自适应字体方法封装
| fontSize(res) {
| var docEl = document.documentElement,
| clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
| if (!clientWidth) return
| var fontSize = 100 * (clientWidth / 1920)
| return res * fontSize
| },
|
| handleWindowSizeChange() {
| if (this.chartContainer) this.chartContainer.resize()
| }
| }
| }
| </script>
|
|