index.vue
4.31 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
<script lang="ts" setup>
import { EChartsOption, SeriesOption, ECharts, init } from 'echarts';
import { unref, ref, onMounted, computed, nextTick } from 'vue';
import { useMultipleDataFetch } from '../../../hook/useSocket';
import { ComponentPropsConfigType, MultipleDataFetchUpdateFn } 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';
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 { persetOption, option } = props.config;
const { dataSource = [] } = option || {};
const { unit: presetUnit, fontColor: presetFontColor } = persetOption || {};
return {
dataSource: dataSource?.map((item) => {
const { unit, fontColor } = item.componentInfo || {};
const { attribute, attributeRename } = item;
return {
unit: unit ?? presetUnit,
fontColor: fontColor ?? presetFontColor,
attribute,
attributeRename,
};
}),
};
});
const options = (): EChartsOption => {
// getStageColor(gradientInfo);
return {
tooltip: {
trigger: 'item',
},
legend: {
top: '5%',
left: 'center',
textStyle: {
fontSize: unref(getRatio) ? 14 * unref(getRatio) : 14,
},
},
series: [
{
name: '',
type: 'pie',
radius: ['%', '70%'],
center: ['50%', '65%'],
avoidLabelOverlap: false,
itemStyle: {
borderRadius: 4,
borderColor: '#fff',
borderWidth: 2,
},
label: {
show: false,
position: 'center',
},
labelLine: {
show: false,
},
data: [
{ value: 1048, name: '数据1' },
{ value: 735, name: '数据2' },
{ value: 580, name: '数据3' },
],
},
],
};
};
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 updateChart = (data: SeriesOption['data']) => {
unref(chartInstance)?.setOption({
series: [{ data }],
// color: unref(list).map((item) => item.title.color),
} as EChartsOption);
};
const updateFn: MultipleDataFetchUpdateFn = (message) => {
const { data = {} } = message;
const { dataSource } = unref(getDesign);
const series = dataSource.map((item) => {
const { attribute, attributeRename, fontColor, unit } = item;
const [latest] = data[attribute] || [];
const [_timespan, value] = latest || [];
return {
value,
name: attributeRename ?? attribute,
itemStyle: { color: fontColor },
tooltip: {
valueFormatter(value) {
return `${value} ${unit ?? ''}`;
},
},
} as SeriesOption['data'];
});
updateChart(series);
};
useMultipleDataFetch(props, updateFn);
onMounted(() => {
initial();
// !props.config.option.uuid && randomFn();
!props.config.option.uuid;
});
const resize = async () => {
await nextTick();
// 修改echarts大小
unref(chartInstance)?.setOption({
legend: {
textStyle: {
fontSize: 14 * 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">
<DeviceName :config="config" />
<div ref="chartRefEl" class="flex-1 w-full h-full"> </div>
<UpdateTime :time="time" />
</main>
</template>