DashBoardComponent.vue
3.13 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
<script lang="ts" setup>
import type { ECharts, EChartsOption } from 'echarts';
import { PropType } from 'vue';
import { nextTick, onMounted, onUnmounted, ref, unref, computed } from 'vue';
import { init } from 'echarts';
import {
DashboardComponentLayout,
DashBoardValue,
instrumentComponent1,
InstrumentComponentType,
update_instrument_1_font,
update_instrument_2_font,
} from './dashBoardComponent.config';
import { dateUtil } from '/@/utils/dateUtil';
import {
DEFAULT_RADIO_RECORD,
RadioRecord,
DEFAULT_DATE_FORMAT,
fontSize,
} from '../../detail/config/util';
import { Tooltip } from 'ant-design-vue';
const props = defineProps({
add: {
type: Function,
},
layout: {
type: Object as PropType<DashboardComponentLayout>,
default: () => ({}),
},
value: {
type: Object as PropType<DashBoardValue>,
default: () => ({}),
},
radio: {
type: Object as PropType<RadioRecord>,
default: () => DEFAULT_RADIO_RECORD,
},
});
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(() => {
const { radio } = props.radio;
return radio;
});
const beforeUpdateFn = (componentType: InstrumentComponentType) => {
if (componentType === 'instrument-component-1') return update_instrument_1_font;
if (componentType === 'instrument-component-2') return update_instrument_2_font;
return (_radio: number) => {};
};
function update() {
const option = beforeUpdateFn(props.layout.componentType);
unref(chartRef)?.setOption((option(unref(getRadio)) as unknown as EChartsOption) || {});
unref(chartRef)?.resize();
}
onMounted(() => {
initChart();
props.add && props.add(props.value.id, update);
});
onUnmounted(() => {
unref(chartRef)?.clear();
});
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 flex-auto"></div>
<div>
<div class="text-center" :style="{ fontSize: fontSize({ radio: getRadio, basic: 16 }) }">{{
props.value.name
}}</div>
<div
class="text-xs text-center text-gray-400 truncate"
:style="{ fontSize: fontSize({ radio: getRadio, basic: 12, max: 16 }) }"
>
<Tooltip
placement="topLeft"
:title="dateUtil(props?.value?.updateTime || new Date()).format(DEFAULT_DATE_FORMAT)"
>
<span class="mr-2">更新时间:</span>
<span>
{{ dateUtil(props?.value?.updateTime || new Date()).format(DEFAULT_DATE_FORMAT) }}
</span>
</Tooltip>
</div>
</div>
</div>
</template>
<style scoped>
.widget-charts > div {
width: 100%;
height: 100%;
}
</style>