DashBoardComponent.vue
5.21 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
<script lang="ts">
export default {
inheritAttrs: false,
};
</script>
<script lang="ts" setup>
import type { ECharts, EChartsOption } from 'echarts';
import { watch } from 'vue';
import { nextTick, onMounted, onUnmounted, ref, unref, computed } from 'vue';
import { init } from 'echarts';
import {
DashboardComponentLayout,
DashBoardValue,
instrumentComponent1,
update_instrument_1_font,
update_instrument_2_font,
update_instrument_1_value,
update_instrument_2_value,
} from './dashBoardComponent.config';
import {
DEFAULT_RADIO_RECORD,
RadioRecord,
fontSize,
getUpdateTime,
} from '../../detail/config/util';
import { Tooltip } from 'ant-design-vue';
import { useThrottleFn } from '@vueuse/shared';
import { buildUUID } from '/@/utils/uuid';
import { FrontComponent } from '../../const/const';
const props = withDefaults(
defineProps<{
add?: Function;
layout?: DashboardComponentLayout;
value?: DashBoardValue;
radio?: RadioRecord;
random?: boolean;
}>(),
{
layout: () => ({} as unknown as DashboardComponentLayout),
value: () => ({ id: buildUUID() }),
radio: () => DEFAULT_RADIO_RECORD,
random: true,
}
);
const getControlsWidgetId = () => `widget-chart-${props.value.id}`;
const chartRef = ref<Nullable<ECharts>>(null);
function initChart() {
const chartDom = document.getElementById(getControlsWidgetId())!;
chartRef.value = init(chartDom);
const option: EChartsOption = props.layout.chartOption || instrumentComponent1();
nextTick(() => {
option && unref(chartRef)?.setOption(option);
});
}
const getRadio = computed(() => {
return props.radio;
});
const getChardRadio = computed(() => {
const realWidth = unref(chartRef)?.getWidth();
const realHeight = unref(chartRef)?.getHeight();
const radioRecord = props.radio;
return {
...radioRecord,
height: realHeight || radioRecord.height,
width: realWidth || radioRecord.height,
};
});
const beforeUpdateFn = (componentType: FrontComponent) => {
if (componentType === 'instrument-component-1') return update_instrument_1_font;
if (componentType === 'instrument-component-2') return update_instrument_2_font;
return (_radio: RadioRecord) => {};
};
function update() {
const option = beforeUpdateFn(props.layout.componentType);
unref(chartRef)?.setOption((option(unref(getChardRadio)) as unknown as EChartsOption) || {});
unref(chartRef)?.resize();
}
const getUpdateValueFn = (componentType: FrontComponent) => {
if (componentType === 'instrument-component-1') return update_instrument_1_value;
if (componentType === 'instrument-component-2') return update_instrument_2_value;
return (_radio: DashBoardValue) => {};
};
const updateChartValue = useThrottleFn(() => {
const updateFn = getUpdateValueFn(props.layout.componentType);
unref(chartRef)?.setOption((updateFn(props.value) as unknown as EChartsOption) || {});
}, 500);
watch(() => props.value, updateChartValue);
const updateChartFont = useThrottleFn(() => {
const option = beforeUpdateFn(props.layout.componentType);
setTimeout(() => {
unref(chartRef)?.setOption((option(unref(getChardRadio)) as unknown as EChartsOption) || {});
});
}, 500);
watch(() => props.radio, updateChartFont);
const updateChartType = useThrottleFn(() => {
unref(chartRef)?.clear();
unref(chartRef)?.setOption(props?.layout?.chartOption || {});
}, 500);
watch(() => props.layout.componentType, updateChartType);
let timeout: Nullable<number> = null;
function handleRandomValue() {
const newValue = Math.floor(Math.random() * 100);
const updateFn = getUpdateValueFn(props.layout.componentType);
unref(chartRef)?.setOption(
(updateFn({ ...props.value, value: newValue }) as unknown as EChartsOption) || {}
);
}
onMounted(() => {
initChart();
props.add && props.add(props.value.id, update);
if (props.random) timeout = setInterval(handleRandomValue, 2000) as unknown as number;
});
onUnmounted(() => {
unref(chartRef)?.clear();
clearInterval(timeout as number);
timeout = null;
});
defineExpose({ update });
</script>
<template>
<div class="flex flex-col w-full h-full min-w-3 min-h-3">
<div :id="getControlsWidgetId()" class="widget-charts w-full h-full flex-auto"></div>
<div>
<div
class="text-center"
:style="{
fontSize: fontSize({ radioRecord: getRadio, basic: 16, max: 18 }),
color: '#666',
}"
>
{{ props.value.name }}
</div>
<div
class="text-xs text-center p-5"
:style="{
fontSize: fontSize({ radioRecord: getRadio, basic: 12, max: 12 }),
color: '#999',
}"
>
<Tooltip placement="top" :title="getUpdateTime(props.value?.updateTime)">
<div class="truncate">
<span class="mr-2">更新时间:</span>
<span>
{{ getUpdateTime(props.value?.updateTime) }}
</span>
</div>
</Tooltip>
</div>
</div>
</div>
</template>
<style scoped>
.widget-charts > div {
width: 100%;
height: 100%;
}
</style>