index.vue
3.38 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
<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, unref } from 'vue'
import type { DatasetComponentOption, ECharts, EChartsOption } from 'echarts'
import { init } from 'echarts'
import { getDefaultOption } from './index.config'
import type { CreateComponentType, RenderComponentExposeType } from '@/core/Library/types'
import { useOnMessage } from '@/core/Library/hook/useOnMessage'
import type { NodeDataDataSourceJsonType } from '@/api/node/model'
import type { SubscriptionData } from '@/core/websocket/type/message'
import { SocketSubscriberEnum } from '@/enums/datasource'
import { formatToDateTime } from '@/utils/dateUtil'
import { useLatestMessageValue } from '@/core/Library/hook/useLatestMessageValue'
import type { CommandSource } from '@/core/websocket/processor'
const props = defineProps<{
config: CreateComponentType
}>()
const getCellBounds = computed(() => props.config.cellBounds || { width: 300, height: 300, x: 0, y: 0 })
const chartElRef = ref<Nullable<HTMLDivElement>>()
const chartInstance = ref<Nullable<ECharts>>()
function initChartInstance() {
chartInstance.value = init(unref(chartElRef))
chartInstance.value.setOption(getDefaultOption())
}
const handleHistoryData = (message: SubscriptionData, attr: string) => {
const data = message[attr]
const xAxisData: string[] = []
const seriesData: number[] = []
for (const item of data) {
const [ts, value] = item
xAxisData.push(formatToDateTime(ts))
seriesData.push(value)
}
unref(chartInstance)?.setOption({
xAxis: { data: xAxisData },
series: { data: seriesData },
} as EChartsOption)
}
function sliceData(data: any[], maxLength = 20) {
if (data.length > maxLength)
return data.slice(1)
return data
}
const handlerTimeSeriesData = (commandSource: CommandSource, message: SubscriptionData, attr: string) => {
const { data } = commandSource
const { attrInfo, deviceInfo } = data as NodeDataDataSourceJsonType
const { deviceName } = deviceInfo || {}
const { ts, latestValue } = useLatestMessageValue(message, attr)
const option = unref(chartInstance)?.getOption() as EChartsOption
const oldDataset = (option.dataset as DatasetComponentOption[])?.[0].source as Recordable[]
oldDataset.push({
ts: formatToDateTime(ts),
[attr]: latestValue,
})
unref(chartInstance)?.setOption({
title: {
text: `${deviceName || ''}-${attrInfo.name || ''}`,
},
dataset: {
dimensions: ['ts', attr],
source: sliceData(oldDataset),
},
} as EChartsOption)
}
const { onMessage } = useOnMessage({
onReceiveDataSourceMessage(commandSource, message) {
const { data } = commandSource
const { chartOption, attr } = data as NodeDataDataSourceJsonType
const { queryType } = chartOption || {}
if (queryType === SocketSubscriberEnum.TS_SUB_CMDS)
handlerTimeSeriesData(commandSource, message.data, attr)
else if (queryType === SocketSubscriberEnum.HISTORY_CMDS)
handleHistoryData(message.data, attr)
},
})
onMounted(() => {
initChartInstance()
})
onUnmounted(() => {
chartInstance.value?.dispose()
chartInstance.value = null
})
defineExpose<RenderComponentExposeType>({ onMessage })
</script>
<template>
<div class="w-full h-full justify-center flex items-center">
<div
ref="chartElRef" :style="{ width: `${getCellBounds.width}px`, height: `${getCellBounds.height}px` }"
class="w-full h-full"
/>
</div>
</template>