Skip to content

Commit

Permalink
fix: 修复CPU图表线条跳动的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
orilights committed Nov 11, 2023
1 parent 2a38af5 commit d8b5a0b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 54 deletions.
18 changes: 12 additions & 6 deletions src/components/ServerCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,19 @@ watch(() => props.server, () => {
if (props.server.latest_ts <= cpuHistoryLastUpdated)
return
const list = cpuHistory.value.slice()
list.push({
name: Date.now(),
value: [
props.server.latest_ts * 1000,
props.server.cpu,
],
})
if (list.length > CPU_HISTORY_MAX)
list.shift()
cpuHistory.value = list
cpuHistoryLastUpdated = props.server.latest_ts
cpuHistory.value.push([
props.server.latest_ts * 1000,
props.server.cpu,
])
if (cpuHistory.value.length > CPU_HISTORY_MAX)
cpuHistory.value.shift()
}
})
</script>
106 changes: 58 additions & 48 deletions src/components/StatusChart.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<VChart class="chart" :option="option" :autoresize="true" />
<VChart ref="chartRef" class="chart" :option="option" :autoresize="true" />
</template>

<script setup lang="ts">
Expand Down Expand Up @@ -34,60 +34,70 @@ type EChartsOption = ComposeOption<
| LineSeriesOption
>
const option = computed<EChartsOption>(() => {
return {
grid: {
left: 40,
right: 20,
top: 10,
bottom: 20,
},
tooltip: {
trigger: 'axis',
formatter: (params: any) => {
params = params[0]
return `${formatTime(params.value[0])}: ${params.value[1]}%`
},
axisPointer: {
animation: false,
},
},
xAxis: {
type: 'time',
splitLine: {
show: false,
},
axisLabel: {
hideOverlap: true,
},
},
yAxis: {
type: 'value',
max: (value) => {
return value.max <= 20
? 20
: value.max <= 50
? 50
: 100
},
axisLabel: {
hideOverlap: true,
showMaxLabel: true,
formatter: (value: any) => {
return `${value}%`
},
},
},
const chartRef = ref<any>()
watch(() => props.data, () => {
chartRef.value?.setOption({
series: [
{
data: props.data,
type: 'line',
showSymbol: false,
},
],
}
})
})
const option: EChartsOption = {
grid: {
left: 40,
right: 20,
top: 10,
bottom: 20,
},
tooltip: {
trigger: 'axis',
formatter: (params: any) => {
params = params[0]
return `${formatTime(params.value[0])}: ${params.value[1]}%`
},
axisPointer: {
animation: false,
},
},
xAxis: {
type: 'time',
splitLine: {
show: false,
},
axisLabel: {
hideOverlap: true,
},
},
yAxis: {
type: 'value',
max: (value: any) => {
return value.max <= 20
? 20
: value.max <= 50
? 50
: 100
},
axisLabel: {
hideOverlap: true,
showMaxLabel: true,
formatter: (value: any) => {
return `${value}%`
},
},
},
series: [
{
data: props.data,
type: 'line',
showSymbol: false,
},
],
}
function formatTime(time: number) {
const date = new Date(time)
const hours = date.getHours().toString().padStart(2, '0')
Expand Down

0 comments on commit d8b5a0b

Please sign in to comment.