index.vue
6.85 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
<script lang="ts" setup>
import { EChartsOption, ECharts, init } from 'echarts';
import { unref, ref, onMounted, computed, nextTick } from 'vue';
import { ComponentPropsConfigType } from '../../../index.type';
import { option } from './config';
import { UpdateTime } from '/@/views/visual/commonComponents/UpdateTime';
import { useIntervalFn } from '@vueuse/core';
import { useComponentScale } from '../../../hook/useComponentScale';
import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
import { useDataFetch } from '../../../hook/socket/useSocket';
import { DataFetchUpdateFn } from '../../../hook/socket/useSocket.type';
const props = defineProps<{
config: ComponentPropsConfigType<typeof option>;
}>();
const chartRefEl = ref<Nullable<HTMLDivElement>>(null);
const chartInstance = ref<Nullable<ECharts>>(null);
const time = ref<Nullable<number>>(null);
const getDesign = computed(() => {
const { option, persetOption } = props.config;
const { componentInfo, attribute, attributeRename, attributeName } = option;
const {
fontColor: presetFontColor,
unit: presetUnit,
pointerColor: presetPointerColor,
instrumentPanelColor: presetInstrumentPanelColor,
gradientInfo: presetGradientInfo,
showTime: persetShowTime,
maxNumber: persetMaxNumber,
} = persetOption || {};
const {
unit,
fontColor,
pointerColor,
instrumentPanelColor,
gradientInfo,
showTime,
maxNumber,
} = componentInfo || {};
return {
unit: unit ?? presetUnit,
fontColor: fontColor ?? presetFontColor,
attribute: attributeRename || attributeName || attribute,
pointerColor: pointerColor ?? presetPointerColor,
instrumentPanelColor: instrumentPanelColor ?? presetInstrumentPanelColor,
gradientInfo: gradientInfo ?? presetGradientInfo,
showTime: showTime ?? persetShowTime,
maxNumber: maxNumber || persetMaxNumber,
};
});
const options = (): EChartsOption => {
const { unit, fontColor, pointerColor, maxNumber } = unref(getDesign);
// getStageColor(gradientInfo);
return {
series: [
{
type: 'gauge',
min: 0,
max: maxNumber,
progress: {
show: true,
width: unref(getRatio) ? 6 * unref(getRatio) : 6,
},
axisLine: {
lineStyle: {
width: unref(getRatio) ? 6 * unref(getRatio) : 6,
},
},
axisTick: {
show: false,
},
splitLine: {
length: unref(getRatio) ? 3 * unref(getRatio) : 3,
lineStyle: {
width: unref(getRatio) ? 1 * unref(getRatio) : 1,
color: '#999',
},
},
axisLabel: {
distance: unref(getRatio) ? 10 * unref(getRatio) : 10,
color: '#999',
fontSize: unref(getRatio) ? 6 * unref(getRatio) : 6,
},
pointer: {
//指针
show: true,
icon: 'path://M2090.36389,615.30999 L2090.36389,615.30999 C2091.48372,615.30999 2092.40383,616.194028 2092.44859,617.312956 L2096.90698,728.755929 C2097.05155,732.369577 2094.2393,735.416212 2090.62566,735.56078 C2090.53845,735.564269 2090.45117,735.566014 2090.36389,735.566014 L2090.36389,735.566014 C2086.74736,735.566014 2083.81557,732.63423 2083.81557,729.017692 C2083.81557,728.930412 2083.81732,728.84314 2083.82081,728.755929 L2088.2792,617.312956 C2088.32396,616.194028 2089.24407,615.30999 2090.36389,615.30999 Z',
width: 4 * unref(getRatio) ? 4 * unref(getRatio) : 4,
length: '100%',
itemStyle: {
color: pointerColor,
},
},
// anchor: {
// show: true,
// showAbove: true,
// size: 10,
// itemStyle: {
// borderWidth: 4,
// },
// },
title: {
show: false,
},
detail: {
valueAnimation: true,
fontSize: unref(getRatio) ? 16 * unref(getRatio) : 16,
offsetCenter: [0, '70%'],
formatter: `{value} ${unit ?? ''}`,
color: fontColor || 'inherit',
},
data: [
{
value: 70,
},
],
},
],
};
};
const updateChart = (value: number) => {
unref(chartInstance)?.setOption({
series: [{ data: [{ value: value.toFixed(2) }] }, { data: [{ value: value.toFixed(2) }] }],
} as EChartsOption);
};
const initial = () => {
chartInstance.value = init(unref(chartRefEl)! as HTMLElement);
chartInstance.value.setOption(options());
};
const randomFn = () => {
useIntervalFn(() => {
const value = (Math.random() * 100).toFixed(0);
unref(chartInstance)?.setOption({
series: [{ data: [{ value }] }, { data: [{ value }] }],
} as EChartsOption);
}, 3000);
};
const updateFn: DataFetchUpdateFn = (message, attribute) => {
const { data = {} } = message;
const [latest] = data[attribute] || [];
if (!latest.length) return;
const [timespan, value] = latest;
time.value = timespan;
updateChart(isNaN(value as unknown as number) ? 0 : Number(value));
};
useDataFetch(props, updateFn);
onMounted(() => {
initial();
!props.config.option.uuid && randomFn();
});
const resize = async () => {
await nextTick();
// 修改echarts大小
unref(chartInstance)?.setOption({
series: [
{
progress: {
width: 6 * unref(getRatio),
},
axisLine: {
// 仪表盘
lineStyle: {
width: 6 * unref(getRatio),
},
},
axisLabel: {
distance: 10 * unref(getRatio),
fontSize: 6 * unref(getRatio),
},
pointer: {
width: 4 * unref(getRatio),
},
splitLine: {
length: 3 * unref(getRatio),
lineStyle: {
width: 1 * unref(getRatio),
},
},
detail: {
fontSize: 16 * unref(getRatio),
},
},
],
} as EChartsOption);
unref(chartInstance)?.resize();
};
const { getRatio } = useComponentScale(props, resize);
</script>
<template>
<main
class="w-full h-full flex flex-col justify-center items-center"
:class="!getDesign.showTime && 'p-5'"
>
<DeviceName :config="config" />
<div class="flex flex-1 flex-col justify-center items-center w-full h-full">
<div ref="chartRefEl" class="flex-1 w-full h-6/7 flex flex-col justify-center items-center">
</div>
<div class="text-gray-500 text-xs text-center truncate">{{
getDesign.attribute || '速度'
}}</div>
</div>
<UpdateTime v-if="getDesign.showTime" :time="time" />
</main>
</template>