vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法
vue3 集成 echarts 最大的坑就是出現(xiàn)了,reactive 的數(shù)據(jù) 刷新了,但圖表缺不會刷新,查了很多資料,試了很多方式都沒效果,最后測試出來解決方法很簡單:
核心代碼:
// 省略非核心代碼.......
const init = () => {
chart.value = globalProperties?.echarts.init(pieChartRef.value)
chart.value.setOption(option)
}
watch(
() => props.data,
() => {
// 數(shù)據(jù)變化時(shí),重新對series里面的 data 賦值即可
option.series[0].data= top10()
chart.value.setOption(option)
}
)
onMounted(() => {
init()
addEventListener('resize', resize)
})附上 TSX 整個(gè)頁面參考
import {defineComponent, getCurrentInstance, onBeforeUnmount, onMounted, PropType, ref, watch} from 'vue'
import type { Ref } from 'vue'
import {ECharts, throttle} from "echarts";
import {useI18n} from "vue-i18n";
import {EChartsType} from "echarts/types/dist/echarts";
const props = {
height: {
type: [String, Number] as PropType<string | number>,
default: 590
},
width: {
type: [String, Number] as PropType<string | number>,
default: '100%'
},
data: {
type: Array as PropType<Array<any>>
}
}
const PieChart = defineComponent({
name: 'PieChart',
props,
setup(props) {
const pieChartRef: Ref<HTMLDivElement | null> = ref(null)
const top10 = () => {
return props.data.length>=10 ? props.data.slice(0,10) : props.data
}
const option = {
title: {
text: '分組聚合 Top 10',
left: 'center',
textStyle: {
color:'white'
},
},
tooltip: {
trigger: 'item',
backgroundColor: '#fff'
},
legend: {
bottom: '0%',
left: 'center',
textStyle:{
fontSize: 16,
color: 'white',
fontWeight: 'bold'
}
},
series: [
{
type: 'pie',
radius: ['35%', '60%'],
center: ['50%', '40%'],
avoidLabelOverlap: false,
emphasis: {
label: {
show: true,
fontSize: 30,
fontWeight: 'bolder',
color: 'white'
}
},
label: {
show: false,
position: 'center'
},
labelLine: {
show: false
},
data: top10()
}
]
}
const globalProperties = getCurrentInstance()?.appContext.config.globalProperties
let chart:Ref<ECharts | null> = ref(null)
const init = () => {
chart.value = globalProperties?.echarts.init(pieChartRef.value)
chart.value.setOption(option)
}
const resize = throttle(() => {
chart && chart.value.resize()
}, 20)
watch(
() => props.data,
() => {
// 數(shù)據(jù)變化時(shí),重新對series里面的 data 賦值即可
option.series[0].data= top10()
chart.value.setOption(option)
}
)
onMounted(() => {
init()
addEventListener('resize', resize)
})
return { pieChartRef }
},
render() {
const { height, width } = this
return (
<div
ref='pieChartRef'
id={'myChart'}
style={{
height: "300px",
width: typeof width === 'number' ? width + 'px' : width
}}
>
</div>
)
}
})
export default PieChart到此這篇關(guān)于vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法的文章就介紹到這了,更多相關(guān)vue3集成echarts后圖表不刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue基于element-china-area-data插件實(shí)現(xiàn)省市區(qū)聯(lián)動
省市區(qū)聯(lián)動在日常開發(fā)中用的非常多,本文就介紹一下vue基于element-china-area-data插件實(shí)現(xiàn)省市區(qū)聯(lián)動,具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04
vue3.0自定義指令(drectives)知識點(diǎn)總結(jié)
在本篇文章里小編給大家整體了一篇關(guān)于vue3.0自定義指令(drectives)知識點(diǎn)總結(jié),有興趣的朋友們可以學(xué)習(xí)下。2020-12-12
Vue3中判斷數(shù)據(jù)是否為響應(yīng)式的幾種方法
在Vue3中,判斷數(shù)據(jù)是否為響應(yīng)式通常涉及到檢查數(shù)據(jù)是否是由Vue的響應(yīng)式系統(tǒng)創(chuàng)建的,Vue3 使用 Proxy 來實(shí)現(xiàn)響應(yīng)式系統(tǒng),因此可以通過一些方法來判斷一個(gè)對象是否為響應(yīng)式,需要的朋友可以參考下2025-06-06
Vue實(shí)現(xiàn)input寬度隨文字長度自適應(yīng)操作
這篇文章主要介紹了Vue實(shí)現(xiàn)input寬度隨文字長度自適應(yīng)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
ElementPlus組件與圖標(biāo)按需自動引入的實(shí)現(xiàn)方法
這篇文章主要介紹了ElementPlus組件與圖標(biāo)按需自動引入的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
解決vue2.x中數(shù)據(jù)渲染以及vuex緩存的問題
本篇文章主要介紹了vue2.x中請求之前數(shù)據(jù)顯示以及vuex緩存的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
在Vue中實(shí)現(xiàn)地圖熱點(diǎn)展示與交互的方法詳解(如熱力圖)
隨著大數(shù)據(jù)和可視化技術(shù)的發(fā)展,地圖熱點(diǎn)展示越來越受到人們的關(guān)注,在Vue應(yīng)用中,我們通常需要實(shí)現(xiàn)地圖熱點(diǎn)的展示和交互,以便更好地呈現(xiàn)數(shù)據(jù)和分析結(jié)果,本文將介紹在Vue中如何進(jìn)行地圖熱點(diǎn)展示與交互,包括熱力圖、點(diǎn)聚合等2023-07-07

