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
| <template>
| <div class="deviceChart">
| <div class="titleBg">
| <span>设备类型分析</span>
| </div>
| <div class="chartBox" ref="chartRef"></div>
| </div>
| </template>
| <script setup>
| import 'echarts-gl'
| import { statisticalDeviceTypeApi } from '@/views/basicManage/deviceStock/fwDevice'
| import useEchartsResize from '@/hooks/useEchartsResize'
|
| const chartRef = ref(null)
| let { chart } = useEchartsResize(chartRef)
|
| const list = ref([])
|
| const COLORS = ['#2D74EA', '#1BBDCA', '#E3A023', '#FFD900', '#AA4CD3', '#00ADEF']
|
| const getParametricEquation = (startRatio, endRatio, isSelected, isHovered, k, h) => {
| const midRatio = (startRatio + endRatio) / 2
| const startRadian = startRatio * Math.PI * 2
| const endRadian = endRatio * Math.PI * 2
| const midRadian = midRatio * Math.PI * 2
| const kValue = typeof k !== 'undefined' ? k : 1 / 3
| const offsetX = isSelected ? Math.sin(midRadian) * 0.1 : 0
| const offsetY = isSelected ? Math.cos(midRadian) * 0.1 : 0
| const hoverRate = isHovered ? 1.05 : 1
| return {
| u: {
| min: -Math.PI,
| max: Math.PI * 3,
| step: Math.PI / 32,
| },
| v: {
| min: 0,
| max: Math.PI * 2,
| step: Math.PI / 20,
| },
| x: (u, v) => {
| if (u < startRadian) return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * kValue) * hoverRate
| if (u > endRadian) return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * kValue) * hoverRate
| return offsetX + Math.cos(u) * (1 + Math.cos(v) * kValue) * hoverRate
| },
| y: (u, v) => {
| if (u < startRadian) return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * kValue) * hoverRate
| if (u > endRadian) return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * kValue) * hoverRate
| return offsetY + Math.sin(u) * (1 + Math.cos(v) * kValue) * hoverRate
| },
| z: (u, v) => {
| if (u < -Math.PI * 0.5) return Math.sin(u)
| if (u > Math.PI * 2.5) return Math.sin(u) * h * 0.1
| return Math.sin(v) > 0 ? 1 * h * 0.1 : -1
| },
| }
| }
|
| const getPie3D = (pieData, internalDiameterRatio) => {
| const series = []
| let sumValue = 0
| let startValue = 0
| let endValue = 0
| const k =
| typeof internalDiameterRatio !== 'undefined' ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3
|
| for (let i = 0; i < pieData.length; i++) {
| sumValue += pieData[i].value
| const seriesItem = {
| name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
| type: 'surface',
| parametric: true,
| wireframe: {
| show: false,
| },
| pieData: pieData[i],
| pieStatus: {
| selected: false,
| hovered: false,
| k: 1 / 10,
| },
| }
| if (typeof pieData[i].itemStyle !== 'undefined') {
| const itemStyle = {}
| if (typeof pieData[i].itemStyle.color !== 'undefined') itemStyle.color = pieData[i].itemStyle.color
| if (typeof pieData[i].itemStyle.opacity !== 'undefined') itemStyle.opacity = pieData[i].itemStyle.opacity
| seriesItem.itemStyle = itemStyle
| }
| series.push(seriesItem)
| }
|
| for (let i = 0; i < series.length; i++) {
| endValue = startValue + series[i].pieData.value
| series[i].pieData.startRatio = startValue / sumValue
| series[i].pieData.endRatio = endValue / sumValue
| series[i].parametricEquation = getParametricEquation(
| series[i].pieData.startRatio,
| series[i].pieData.endRatio,
| false,
| false,
| k,
| series[i].pieData.value
| )
| startValue = endValue
| }
|
| return series
| }
|
| const buildOption = () => {
| const legendRich = {
| percent: {
| fontWeight: 700,
| },
| }
| const optionsData = list.value.map((item, index) => ({
| name: item.deviceTypeValue,
| value: item.deviceCount,
| itemStyle: {
| color: COLORS[index % COLORS.length],
| },
| }))
| list.value.forEach((item, index) => {
| legendRich[`count${index}`] = {
| color: COLORS[index % COLORS.length],
| }
| })
| const series = getPie3D(optionsData, 0.6)
| series.push({
| name: 'pie2d',
| type: 'pie',
| label: {
| show: false,
| },
| labelLine: {
| show: false,
| },
| startAngle: 300,
| clockwise: false,
| radius: ['42%', '54%'],
| center: ['30%', '50%'],
| data: optionsData,
| itemStyle: {
| opacity: 0,
| },
| })
|
| return {
| graphic: {
| type: 'text',
| left: '22%',
| top: '48%',
| style: {
| text: '设备类型',
| fill: '#FFFFFF',
| font: '600 14px Arial',
| textAlign: 'center',
| textVerticalAlign: 'middle',
| },
| },
| legend: {
| right: 24,
| top: 'center',
| orient: 'vertical',
| textStyle: {
| color: '#FFFFFF',
| fontSize: 12,
| rich: legendRich,
| lineHeight: 30
| },
| itemWidth: 8,
| itemHeight: 8,
| icon: 'rect',
| formatter: name => {
| const itemIndex = list.value.findIndex(v => v.deviceTypeValue === name)
| const item = list.value[itemIndex]
| if (!item) return name
| const percent = item.totalDevices ? Math.round((item.deviceCount / item.totalDevices) * 100) : 0
| return `${name} {count${itemIndex}|${item.deviceCount}} {percent|${percent}%}`
| },
| },
| animation: true,
| tooltip: {
| show: true,
| trigger: 'item',
| confine: true,
| backgroundColor: 'rgba(0,0,0,0.7)',
| borderColor: '#0070FF',
| borderWidth: 1,
| textStyle: {
| color: '#FFFFFF',
| fontSize: 14, // 也可以顺便调整字号
| },
| formatter: params => {
| if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
| return `${params.seriesName}:${series[params.seriesIndex].pieData.value}`
| }
| },
| },
| title: {
| x: 'center',
| top: 0,
| textStyle: {
| color: '#fff',
| fontSize: 22,
| },
| },
| backgroundColor: 'transparent',
| labelLine: {
| show: false,
| },
| label: {
| show: false,
| },
| xAxis3D: {
| min: -1,
| max: 1,
| },
| yAxis3D: {
| min: -1,
| max: 1,
| },
| zAxis3D: {
| min: -1,
| max: 1,
| },
| grid3D: {
| show: false,
| boxHeight: 26,
| left: '0%',
| top: '-2%',
| width: '60%',
| viewControl: {
| distance: 210,
| alpha: 55,
| beta: 70,
| autoRotate: false,
| rotateSensitivity: 1,
| zoomSensitivity: 0,
| panSensitivity: 0,
| },
| },
| series,
| }
| }
|
| onMounted(() => {
| statisticalDeviceTypeApi().then(res => {
| list.value = Array.isArray(res?.data?.data) ? res.data.data : []
| chart.value.setOption(buildOption())
| })
| })
| </script>
|
| <style scoped lang="scss">
| .deviceChart {
| width: 385px;
| }
| .titleBg {
| width: 365px;
| height: 34px;
| background: url('@/assets/images/basicManage/chartBg.svg') no-repeat center / 100% 100%;
| }
| </style>
|
|