index.vue
6.37 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
<script lang="ts" setup>
import { EChartsOption, ECharts, init } from 'echarts';
import { unref, ref, onMounted, nextTick, toRaw, computed } from 'vue';
import { ComponentPropsConfigType } from '../../../index.type';
import { option } from './config';
// import { UpdateTime } from '/@/views/visual/commonComponents/UpdateTime';
import { useComponentScale } from '../../../hook/useComponentScale';
import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
import { useIntervalFn } from '@vueuse/core';
import { useMultipleDataFetch } from '../../../hook/socket/useSocket';
import { MultipleDataFetchUpdateFn } from '../../../hook/socket/useSocket.type';
import { useReceiveMessage } from '../../../hook/useReceiveMessage';
import { formatToDateTime } from '/@/utils/dateUtil';
interface IList {
[key: string]: string | number;
}
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 updateInterval = ref<number>(1000); //默认每秒更新一次
const maxDataPoints = ref<number>(30); //默认每秒显示10个数据点
const chartData = ref<{ time: string | number; value: number }[]>([]);
const legendData = ref<string[]>(['温度']);
const timeList = ref<string[]>([]);
const generateRandomData = () => {
const minValue = 0;
const maxValue = 100;
const time = new Date().toLocaleTimeString();
const value = Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue;
return { time, value, name: '温度' };
};
const options = (): EChartsOption => {
return {
tooltip: {
// trigger: 'axis',
},
legend: {
top: '10%',
left: 'center',
data: ['温度'],
},
grid: {
top: '30%',
left: '6%',
right: '10%',
bottom: '8%',
containLabel: true,
},
xAxis: {
type: 'category',
// boundaryGap: false,
},
yAxis: {
type: 'value',
boundaryGap: [0, '100%'],
},
series: [{ type: 'line', name: '温度', data: [] }],
};
};
const random = () => {
useIntervalFn(() => {
const newData = generateRandomData();
chartData.value.push(newData);
if (unref(chartData).length > maxDataPoints.value) {
chartData.value.shift();
}
unref(chartInstance)?.setOption({
xAxis: {
data: toRaw(unref(chartData).map((data) => data.time)),
},
series: [{ data: toRaw(unref(chartData).map((item) => item.value)) }],
});
}, updateInterval);
};
const getDesign = computed(() => {
const { persetOption, option } = props.config;
const { dataSource = [] } = option || {};
const {
unit: presetUnit,
fontColor: presetFontColor,
// lineColor: persetLineColor,
} = persetOption || {};
return {
dataSource: dataSource?.map((item) => {
const { unit, showDeviceName, fontColor } = item.componentInfo || {};
const { attribute, attributeRename, deviceId, attributeName, deviceName, deviceRename } =
item;
return {
unit: unit ?? presetUnit,
fontColor: fontColor ?? presetFontColor,
attribute,
attributeName: attributeRename || attributeName,
showDeviceName,
deviceName: deviceRename || deviceName,
id: deviceId,
// lineColor: lineColor || persetLineColor,
};
}),
};
});
const initial = () => {
chartInstance.value = init(unref(chartRefEl)! as HTMLElement);
chartInstance.value.setOption(options());
};
const series = ref(
unref(getDesign).dataSource.map((item) => {
return {
type: 'line',
name: `${item.deviceName} - ${item.attributeName}`,
data: [] as { name: string; value: number }[],
id: item.id,
attribute: item.attribute,
// itemStyle: {
// color: item.lineColor,
// },
// lineStyle: {
// color: item.lineColor,
// },
};
})
);
const { forEachGroupMessage } = useReceiveMessage();
const updateFn: MultipleDataFetchUpdateFn = async (message, deviceId, attribute) => {
legendData.value = unref(getDesign).dataSource.map((item) => {
return `${item.deviceName} - ${item.attributeName}`;
});
const list: IList | any = {};
forEachGroupMessage(message, deviceId, attribute, (attribute, value, timespan) => {
list.time = timespan || list.time;
series.value.forEach((item) => {
if (item.id === deviceId && item.attribute === attribute) {
item.data.push({
name: formatToDateTime(list.time, 'YYYY-MM-DD HH:mm:ss'),
value: value,
});
if (item.data.length > unref(maxDataPoints)) {
item.data.shift();
}
}
});
});
list.time && timeList.value.push(formatToDateTime(list.time, 'YYYY-MM-DD HH:mm:ss'));
if (unref(timeList).length > unref(maxDataPoints)) {
timeList.value.shift();
}
await nextTick();
unref(chartInstance)?.setOption({
xAxis: {
data: toRaw(unref(timeList)),
},
legend: {
data: toRaw(unref(legendData)),
},
series: toRaw(
unref(series).map((item) => {
const { type, name, data } = item;
return {
type,
name,
data,
// itemStyle,
// lineStyle,
};
})
),
});
};
useMultipleDataFetch(props, updateFn);
onMounted(() => {
initial();
!props.config.option.dataSource?.length && random();
});
const resize = async () => {
await nextTick();
// 修改echarts大小
// unref(chartInstance)?.setOption({
// legend: {
// textStyle: {
// fontSize: 14 * unref(getRatio),
// },
// },
// } as EChartsOption);
unref(chartInstance)?.resize();
};
useComponentScale(props, resize);
</script>
<template>
<main class="w-full h-full flex flex-col justify-center items-center flex-1">
<DeviceName :config="config" />
<div
ref="chartRefEl"
class="flex-1 w-full h-7/8 flex-col justify-center items-center flex mb-3"
>
</div>
<!-- <UpdateTime :time="time" /> -->
</main>
</template>