Skip to content

Commit d8b5a0b

Browse files
committed
fix: 修复CPU图表线条跳动的问题
1 parent 2a38af5 commit d8b5a0b

File tree

2 files changed

+70
-54
lines changed

2 files changed

+70
-54
lines changed

src/components/ServerCard.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,19 @@ watch(() => props.server, () => {
172172
if (props.server.latest_ts <= cpuHistoryLastUpdated)
173173
return
174174
175+
const list = cpuHistory.value.slice()
176+
list.push({
177+
name: Date.now(),
178+
value: [
179+
props.server.latest_ts * 1000,
180+
props.server.cpu,
181+
],
182+
})
183+
if (list.length > CPU_HISTORY_MAX)
184+
list.shift()
185+
186+
cpuHistory.value = list
175187
cpuHistoryLastUpdated = props.server.latest_ts
176-
cpuHistory.value.push([
177-
props.server.latest_ts * 1000,
178-
props.server.cpu,
179-
])
180-
if (cpuHistory.value.length > CPU_HISTORY_MAX)
181-
cpuHistory.value.shift()
182188
}
183189
})
184190
</script>

src/components/StatusChart.vue

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<VChart class="chart" :option="option" :autoresize="true" />
2+
<VChart ref="chartRef" class="chart" :option="option" :autoresize="true" />
33
</template>
44

55
<script setup lang="ts">
@@ -34,60 +34,70 @@ type EChartsOption = ComposeOption<
3434
| LineSeriesOption
3535
>
3636
37-
const option = computed<EChartsOption>(() => {
38-
return {
39-
grid: {
40-
left: 40,
41-
right: 20,
42-
top: 10,
43-
bottom: 20,
44-
},
45-
tooltip: {
46-
trigger: 'axis',
47-
formatter: (params: any) => {
48-
params = params[0]
49-
return `${formatTime(params.value[0])}: ${params.value[1]}%`
50-
},
51-
axisPointer: {
52-
animation: false,
53-
},
54-
},
55-
xAxis: {
56-
type: 'time',
57-
splitLine: {
58-
show: false,
59-
},
60-
axisLabel: {
61-
hideOverlap: true,
62-
},
63-
},
64-
yAxis: {
65-
type: 'value',
66-
max: (value) => {
67-
return value.max <= 20
68-
? 20
69-
: value.max <= 50
70-
? 50
71-
: 100
72-
},
73-
axisLabel: {
74-
hideOverlap: true,
75-
showMaxLabel: true,
76-
formatter: (value: any) => {
77-
return `${value}%`
78-
},
79-
},
80-
},
37+
const chartRef = ref<any>()
38+
39+
watch(() => props.data, () => {
40+
chartRef.value?.setOption({
8141
series: [
8242
{
8343
data: props.data,
84-
type: 'line',
85-
showSymbol: false,
8644
},
8745
],
88-
}
46+
})
8947
})
9048
49+
const option: EChartsOption = {
50+
grid: {
51+
left: 40,
52+
right: 20,
53+
top: 10,
54+
bottom: 20,
55+
},
56+
tooltip: {
57+
trigger: 'axis',
58+
formatter: (params: any) => {
59+
params = params[0]
60+
return `${formatTime(params.value[0])}: ${params.value[1]}%`
61+
},
62+
axisPointer: {
63+
animation: false,
64+
},
65+
},
66+
xAxis: {
67+
type: 'time',
68+
splitLine: {
69+
show: false,
70+
},
71+
axisLabel: {
72+
hideOverlap: true,
73+
},
74+
},
75+
yAxis: {
76+
type: 'value',
77+
max: (value: any) => {
78+
return value.max <= 20
79+
? 20
80+
: value.max <= 50
81+
? 50
82+
: 100
83+
},
84+
axisLabel: {
85+
hideOverlap: true,
86+
showMaxLabel: true,
87+
formatter: (value: any) => {
88+
return `${value}%`
89+
},
90+
},
91+
},
92+
series: [
93+
{
94+
data: props.data,
95+
type: 'line',
96+
showSymbol: false,
97+
},
98+
],
99+
}
100+
91101
function formatTime(time: number) {
92102
const date = new Date(time)
93103
const hours = date.getHours().toString().padStart(2, '0')

0 commit comments

Comments
 (0)