index.vue
6.24 KB
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
273
274
275
276
277
278
279
<!--
* 功能:水滴图
* 作者:闫李壮
* 日期:2024-06-04
-->
<template>
<div class="chart-container" ref="spinRef">
<!-- 加载动画,加载完成后显示水滴图或空状态 -->
<a-spin :loading="loading" class="chart-view" :tip="$t('global.loading')">
<div :class="'chart-water-ref'+randomNumber" :style="[{'height': chartHeight+'px'},{'width':chartWidth+'px'}]"
v-show="chartInfo.dashboardInfo.varSn"></div>
<a-empty v-show="!chartInfo.dashboardInfo.varSn"/>
</a-spin>
</div>
</template>
<script setup lang="ts">
import useLoading from '@/hooks/loading';
import {useIntervalFn, useWindowSize} from '@vueuse/core';
import {inject, onBeforeUnmount, onMounted, ref, watch, watchEffect} from "vue";
import * as echarts from "echarts";
import 'echarts-liquidfill';
import {getChartInfo} from "@/api/dashboard/api";
import {SocketData} from "@/api/websocketService";
import {getTimeObject} from "@/utils/charts";
// 定义组件属性
const props = defineProps({
public: {
type: Object,
default: () => ({})
},
// 设备编号
deviceSn: {
type: String,
default: ''
}
});
console.log(props.public, 'shui')
// 加载状态
const {loading, setLoading} = useLoading(false);
const randomNumber = ref<number>(getRandomInt());
// 窗口大小
const {width} = useWindowSize();
const chartWidth = ref<number>();
const chartHeight = ref<number>();
const spinRef = ref<HTMLDivElement | null>(null);
// echarts实例
const chartInstance = ref<any>(null);
// 图表详情
const chartInfo = ref<any>({
cardName: '',
dashboardInfo: {},
dashboardRightList: []
});
// socket 数据
const {data, sendData}: SocketData = inject('webSocketData', {
data: {
value: 0
},
sendData: () => {
}
});
const timeOptions = ref<any>({});
/**
* 时间改变
*/
const timeBarChange = async (timer: any) => {
timeOptions.value = timer;
await fetchData();
}
// 监听webSocket数据
watch(data, async (newVal, oldVal) => {
if (newVal.key && newVal.key === chartInfo.value.dashboardInfo.varSn) {
await fetchData();
}
});
// 初始化并获取数据
const fetchData = async () => {
if(!props.public.cardKey || !props.public.dashboardConfigId){
return;
}
try {
setLoading(true);
const params = {
configId: props.public.dashboardConfigId,
cardKey: props.public.cardKey,
...getTimeObject(timeOptions.value),
};
const res = await getChartInfo(params);
if (res.code === 200 && res.data) {
chartInfo.value = {
cardName: res.data.cardName,
dashboardInfo: res.data.dashboardInfo,
dashboardRightList: res.data.dashboardRightList,
deviceSn: props.deviceSn
};
chartData(res.data.dashboardRightList);
} else {
chartInfo.value = {
cardName: '',
dashboardInfo: {},
dashboardRightList: []
}
}
} catch (e) {
console.error('获取图表数据时出错:', e);
} finally {
setLoading(false);
}
};
// 设置水滴图数据
const chartData = (data: any) => {
// 获取对应的dom元素
const el = document.querySelector('.chart-water-ref' + randomNumber.value) as HTMLDivElement;
// 初始化
if (chartInstance.value) {
chartInstance.value.dispose();
}
chartInstance.value = echarts.init(el);
const option: any = {
series: []
};
option.series = data.map((item: any, index: number) => {
const centerResult = 0;
return {
type: 'liquidFill', // 水位图
radius: '110px', // 显示比例
center: [(index + 1) * 25 + '%', '45%'], // 中心点
amplitude: 10, // 水波振幅
data: [item.value], // data个数代表波浪数
color: [
{
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: '#446bf5',
},
{
offset: 1,
color: '#2ca3e2',
},
],
globalCoord: false,
},
], // 波浪颜色
backgroundStyle: {
borderWidth: 1, // 外边框
color: 'RGBA(51, 66, 127, 0.7)', // 边框内部填充部分颜色
},
label: {
color: '#fff',
insideColor: 'transparent',
fontSize: 26,
fontWeight: 'bold'
},
outline: {
show: true,
borderDistance: 5,
itemStyle: {
borderColor: 'rgba(67,209,100,1)',
borderWidth: 0
}
},
}
});
chartInstance.value.setOption(option);
};
// 监听窗口大小变化
watch(width, () => {
if (chartInstance.value) {
chartInstance.value.resize();
}
});
watchEffect(() => {
// 确保 DOM 渲染完成后再获取宽高
if (spinRef.value) {
const {width, height} = spinRef.value.getBoundingClientRect();
chartWidth.value = props.public.waterPre ? width * 2 : width;
chartHeight.value = props.public.waterPre ? height * 2 : height;
} else {
console.warn('Div元素未找到');
}
})
// 5分钟轮询
const {resume: startInterval, pause: stopInterval} = useIntervalFn(async () => {
await fetchData();
}, 1000 * 60 * 5);
function getRandomInt() {
let min = Math.ceil(0);
let max = Math.floor(100);
return Math.floor(Math.random() * (100 - 0 + 1)) + min;
}
// 组件挂载时获取数据并订阅
onMounted(async () => {
await fetchData();
startInterval();
sendData({
action: 'subscribe',
bizSn: chartInfo.value.dashboardInfo.varSn
});
});
// 组件卸载时取消订阅
onBeforeUnmount(() => {
stopInterval();
sendData({
action: 'unsubscribe',
bizSn: chartInfo.value.dashboardInfo.varSn
});
});
// 对外暴露的接口
defineExpose({
fetchData,
handleTime: (val:any) => {
timeBarChange(val);
},
});
</script>
<style scoped lang="less">
.chart-container {
width: 100%;
height: 100%;
:deep(.arco-card-body) {
width: 100%;
height: calc(100% - 46px);
}
.chart-view {
width: 100%;
height: 100%;
.chart-water-ref {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.arco-empty {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
}
}
</style>