vue中在echarts里設(shè)置的定時(shí)器清除不掉問題及解決
在echarts里設(shè)置的定時(shí)器清除不掉問題
在動(dòng)態(tài)圖里,往往需要用到定時(shí)器去動(dòng)態(tài)渲染數(shù)據(jù),
我的需求是:當(dāng)x軸類目過多的時(shí)候,就讓它自動(dòng)滾動(dòng),這個(gè)時(shí)候需要用到定時(shí)器去動(dòng)態(tài)的控制dataZoom里endValue的值,
this.barInter = setInterval(() => { ? ? ? ? // 每次向后滾動(dòng)一個(gè),最后一個(gè)從頭開始。 ? ? ? ? // console.log(this.xNum) ? ? ? ? if (option.dataZoom[0].endValue >= option.series[0].data.length) { ? ? ? ? ? option.dataZoom[0].endValue = that.xNum; ? ? ? ? ? option.dataZoom[0].startValue = 0; ? ? ? ? } else { ? ? ? ? ? option.dataZoom[0].endValue = option.dataZoom[0].endValue + that.xNum; ? ? ? ? ? option.dataZoom[0].startValue = option.dataZoom[0].startValue + that.xNum; ? ? ? ? } ? ? ? ? myChart.setOption(option); }, 1000);
然后我就遇到一個(gè)bug,發(fā)現(xiàn)怎么也清除不了這個(gè)定時(shí)器,現(xiàn)在講一下解決思路
造成這個(gè)bug的原因是:
因?yàn)橐獙⒑蠖朔档臄?shù)據(jù)賦值給柱形圖里series的data,我一開始采用的方式是,用watch監(jiān)聽返回的值,然后賦值給data,這樣做的目的是為了能夠渲染出圖形,如果直接賦值,圖形是不會(huì)生效的。
關(guān)鍵就在這個(gè)watch監(jiān)聽,因?yàn)槲乙恢痹诒O(jiān)聽,導(dǎo)致一直生成定時(shí)器,才使得用clearInterval也無法清除掉定時(shí)器,因?yàn)槟慵词骨宄?它又立馬生成了一個(gè)新的定時(shí)器。
分析出問題所在,那我們就換一種方式去賦值,然后我采用了
在.then里使用this.$nextTick 去調(diào)用生成echarts圖形的函數(shù),也就是
getData(){ ?? ?this.$axios.selectEfficiencyExhibition(data).then((res) => { ? ? ? ? // console.log("設(shè)備OEE"); ? ? ? ? // console.log(res.data.data); ? ? ? ? if (res.data.data) { ?? ??? ??? ?this.barData=res.data.data ?? ??? ??? ?this.$nextTick(()=>{ ? ? ? ? ? ? ?? ?this.barChart() ? ? ? ? ? ?? ?}) ?? ??? ?} ?? ?}) } barChart(){ ?? ? ?let myChart = that.$echarts.getInstanceByDom( ? ? ? ? document.getElementById("productivity") ? ? ? ); //有的話就獲取已有echarts實(shí)例的DOM節(jié)點(diǎn)。 ? ? ? if (myChart == null) { ? ? ? ? // 如果不存在,就進(jìn)行初始化。 ? ? ? ? myChart = that.$echarts.init(document.getElementById("productivity")); ? ? ? } ? ? var option={ ?? ??? ?//其他配置我就跳過不寫了 ?? ??? ?dataZoom: [ ? ? ? ? ? { ? ? ? ? ? ? type: "inside", //拖拽 slider 為滾動(dòng) ? ? ? ? ? ? xAxisIndex: 0, //控制第一個(gè)x軸 ? ? ? ? ? ? startValue: 0, ? ? ? ? ? ? endValue: that.xNum,//這個(gè)值是我動(dòng)態(tài)設(shè)置顯示多少個(gè)柱形 ? ? ? ? ? ? show: true, ? ? ? ? ? }, ? ? ? ? ], ? ? ? ? series:{ ?? ??? ??? ?name: "綠燈", ? ? ? ? ? ? type: "bar", ? ? ? ? ? ? stack: "total", ? ? ? ? ? ? barMaxWidth: 50, ? ? ? ? ? ? data: this.barData,//后端返的柱形圖數(shù)據(jù) ? ? ? ? ? ? itemStyle: { ? ? ? ? ? ? ? normal: { color: "#14ee14" }, ? ? ? ? ? ? }, ?? ??? ?} ?? ?} ?? ?// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。 ? ? ? this.barInter = setInterval(() => { ? ? ? ? // 每次向后滾動(dòng)一個(gè),最后一個(gè)從頭開始。 ? ? ? ? if (option.dataZoom[0].endValue >= option.series.data.length) { ? ? ? ? ? option.dataZoom[0].endValue = this.xNum; ? ? ? ? ? option.dataZoom[0].startValue = 0; ? ? ? ? } else { ? ? ? ? ? option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1; ? ? ? ? ? option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1; ? ? ? ? } ? ? ? ? myChart.setOption(option); ? ? ? }, 1000); ? }?? ?
采用這種方式后,就不會(huì)一直生成定時(shí)器了,直接用clearInterval(this.barInter)就能清除掉
整體就這么一個(gè)思路,其實(shí)解決起來也很簡(jiǎn)單,只是換了一種動(dòng)態(tài)賦值方式,關(guān)鍵是要分析正確導(dǎo)致問題的原因。
vue3定時(shí)+echarts的細(xì)節(jié)處理
1、如果在當(dāng)前頁面停留時(shí)間久了會(huì)占滿內(nèi)存導(dǎo)致圖表卡頓最后導(dǎo)致崩潰
2、當(dāng)處理了占滿內(nèi)存之后如果頁面切換不去點(diǎn)回去的過程中還是會(huì)崩潰
3、如果有多個(gè)圖表想進(jìn)行關(guān)聯(lián)處理
4、當(dāng)封裝一個(gè)組件使用echarts.getInstanceByDom類型不匹配處理
5、當(dāng)瀏覽器窗口改變之后 圖表自適應(yīng)之后會(huì)移位的問題
6、折線窗口如果數(shù)據(jù)數(shù)據(jù)過大導(dǎo)致起伏不明顯問題
// 查詢按鈕點(diǎn)擊事件 const search = () => { if (listParams.datePicker) { //如果是時(shí)間則取消自動(dòng)刷新 clearInterval(time.value) getAllList() titleBegin.value = '開啟自動(dòng)刷新' } else { getAllList() titleBegin.value = '關(guān)閉自動(dòng)刷新' } } //監(jiān)聽數(shù)據(jù)并計(jì)時(shí) watch( () => chartAvgCostData.value, (newVal) => { if (!listParams.datePicker) { clearInterval(time.value) timeOut() } } ) //累計(jì)計(jì)時(shí)的方法 const timeOut = () => { time.value = setInterval(() => { getAllList() }, 1000 * 20) } //清空定時(shí)器效果 onUnmounted(() => { clearInterval(time.value) }) // 需要在切換到其他頁面時(shí)停止自動(dòng)刷新,切回來時(shí)再開啟 document.addEventListener('visibilitychange', () => { if ( !listParams.projectName && Object.keys(chartAvgCostData.value).length > 0 ) { return } if (document.hidden) { // 清除定時(shí)任務(wù) clearInterval(time.value) time.value = null } else { // 開啟定時(shí)任務(wù) getAllList() } }) //數(shù)據(jù)值大導(dǎo)致起伏不明顯問題 yAxis: { min: function (value:any) { // 取最小值向下取整為最小刻度 return 0 }, // 設(shè)置y軸最大值 max: function (value:any) { // 取最大值向上取整為最大刻度 return Math.floor(value.max + (Math.floor(value.max / 4))) }, } series:[ markPoint: { data: [ { type: 'max', name: 'Max' }, { type: 'min', name: 'Min' } ] }, ] //組件初次進(jìn)來或者銷毀清空?qǐng)D表 onMounted(() => { nextTick(() => { let myChart = echarts.getInstanceByDom(document.getElementById(myCanvas.value)!) if (myChart) { myChart.clear() } if (myChart === undefined) { myChart = echarts.init(document.getElementById(myCanvas.value) as HTMLElement) myChart.group = 'group1' myChart.setOption(datas.option) echarts.connect('group1') window.addEventListener('resize', () => { setTimeout(() => { myChart?.resize() }, 300) }) // 自適應(yīng)屏幕 } }) }) onBeforeUnmount(() => { const myChart = echarts.getInstanceByDom(document.getElementById(myCanvas.value)!) if (myChart) { myChart.clear() } })
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
深入探討Vue3實(shí)現(xiàn)多數(shù)據(jù)變化監(jiān)聽的方式
隨著 Vue 3 的發(fā)布,框架帶來了更多的新特性和增強(qiáng),其中之一就是 watch 函數(shù)的升級(jí),這一改進(jìn)使得多個(gè)數(shù)據(jù)的變化偵聽更加優(yōu)雅和靈活,本文將深入探討 Vue 3 中的 watch 函數(shù),以及如何以更加優(yōu)雅的方式實(shí)現(xiàn)對(duì)多個(gè)數(shù)據(jù)變化的監(jiān)聽2023-08-08Vue-Router進(jìn)階之滾動(dòng)行為詳解
本篇文章主要介紹了Vue-Router進(jìn)階之滾動(dòng)行為詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Vue使用v-model收集各種表單數(shù)據(jù)、過濾器的示例詳解
這篇文章主要介紹了Vue使用v-model收集各種表單數(shù)據(jù)、過濾器的示例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08Vue封裝Swiper實(shí)現(xiàn)圖片輪播效果
圖片輪播是前端中經(jīng)常需要實(shí)現(xiàn)的一個(gè)功能。最近學(xué)習(xí)Vue.js,就針對(duì)Swiper進(jìn)行封裝,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片輪播組件。感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-02-02vue3系統(tǒng)進(jìn)入頁面前的權(quán)限判斷和重定向方式
這篇文章主要介紹了vue3系統(tǒng)進(jìn)入頁面前的權(quán)限判斷和重定向方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03在vue中嵌入外部網(wǎng)站的實(shí)現(xiàn)
這篇文章主要介紹了在vue中嵌入外部網(wǎng)站的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue中實(shí)現(xiàn)監(jiān)聽數(shù)組內(nèi)部元素
這篇文章主要介紹了vue中實(shí)現(xiàn)監(jiān)聽數(shù)組內(nèi)部元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08