From 3ca38c116ce9d4b90697c2905f01ecc145ffd64c Mon Sep 17 00:00:00 2001 From: "@changcode" Date: Wed, 29 Dec 2021 11:31:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9C=A8=E7=BB=84=E4=BB=B6=E9=94=80?= =?UTF-8?q?=E6=AF=81=E6=97=B6=E6=B8=85=E9=99=A4=20setTimeout=20=E5=92=8C?= =?UTF-8?q?=20setInterval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/chart/chart/chartClock.vue | 62 ++++++++++++------- .../chart/chart/options/chartClock.js | 2 +- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/nezha-fronted/src/components/chart/chart/chartClock.vue b/nezha-fronted/src/components/chart/chart/chartClock.vue index f9f709728..f198a40df 100644 --- a/nezha-fronted/src/components/chart/chart/chartClock.vue +++ b/nezha-fronted/src/components/chart/chart/chartClock.vue @@ -24,6 +24,7 @@ import { chartLegendPlacement } from '@/components/common/js/constants' import * as echarts from 'echarts' import { getChart, setChart } from '@/components/common/js/common' import { initColor } from '@/components/chart/chart/tools' +import bus from '@/libs/bus' export default { name: 'chart-clock', @@ -70,20 +71,34 @@ export default { inactive: '#7e7e7e' }, chartId: '', - chartTime: '' + chartTime: null, + localTimer: null, + serverTimer: null, + setTimer: null, + sysTime: '' } }, methods: { + querySystemState () { + this.$get('/healthy').then(response => { + if (response.code == 200) { + if (response.time) { + this.chartTime = this.utcTimeToTimezone(response.time) + this.sysTime = this.getTime() + } + } + }) + }, initChart (chartOption = this.chartOption) { - let _seft = this + const _soft = this /* 使用setTimeout延迟渲染图表,避免样式错乱 */ - setTimeout(() => { + this.setTimer = setTimeout(() => { const myChart = this.isInit ? echarts.init(document.getElementById(`chart-canvas-${this.chartId}`)) : getChart(this.chartId) myChart.setOption(chartOption) this.isInit && setChart(this.chartId, myChart) // 缓存;不使用vue的data是为避免整个chart被监听导致卡顿 this.isInit = false if (this.chartInfo.param && this.chartInfo.param.timeType === 'local') { - setInterval(function () { + _soft.localTimer = setInterval(function () { const date = new Date() const second = date.getSeconds() const minute = date.getMinutes() + second / 60 @@ -98,40 +113,40 @@ export default { animation: minute !== 0, data: [{ value: minute }] }, { - name: 'minute', + name: 'second', animation: second !== 0, data: [{ value: second }] }] myChart.setOption(chartOption) }, 1000) } else { - setInterval(function () { - _seft.$get('/healthy').then((res) => { - this.chartTime = res.time - console.log(res) - }) - const date = new Date(this.chartTime) - const second = date.getSeconds() - const minute = date.getMinutes() + second / 60 - const hour = (date.getHours() % 12) + minute / 60 + _soft.serverTimer = setInterval(function () { + _soft.querySystemState() chartOption.animationDurationUpdate = 300 chartOption.series = [{ name: 'hour', - animation: hour !== 0, - data: [{ value: hour }] + animation: _soft.sysTime.hour !== 0, + data: [{ value: _soft.sysTime.hour }] }, { name: 'minute', - animation: minute !== 0, - data: [{ value: minute }] + animation: _soft.sysTime.minute !== 0, + data: [{ value: _soft.sysTime.minute }] }, { - name: 'minute', - animation: second !== 0, - data: [{ value: second }] + name: 'second', + animation: _soft.sysTime.second !== 0, + data: [{ value: _soft.sysTime.second }] }] myChart.setOption(chartOption) }, 1000) } }, 200) + }, + getTime () { + const date = new Date(this.chartTime) + const second = date.getSeconds() + const minute = date.getMinutes() + second / 60 + const hour = (date.getHours() % 12) + minute / 60 + return { hour, minute, second } } }, mounted () { @@ -142,6 +157,11 @@ export default { this.isStack = this.chartInfo.param.stack } catch (e) {} this.chartInfo.loaded && this.initChart(this.chartOption) + }, + destroyed () { + clearInterval(this.localTimer) + clearInterval(this.serverTimer) + clearInterval(this.setTimer) } } diff --git a/nezha-fronted/src/components/chart/chart/options/chartClock.js b/nezha-fronted/src/components/chart/chart/options/chartClock.js index 50c02f9e5..9d68d58a5 100644 --- a/nezha-fronted/src/components/chart/chart/options/chartClock.js +++ b/nezha-fronted/src/components/chart/chart/options/chartClock.js @@ -26,7 +26,7 @@ const chartClockOption = { } }, axisLabel: { - fontSize: 50, + fontSize: 14, distance: 25, formatter: function (value) { if (value === 0) {