11<template >
22 <div style =" position :relative ;width :100% ;height :280px " >
33 <div ref =" el" style =" position :absolute ;inset :0 ;width :100% ;height :100% ;background :#fafafa ;border :1px dashed #ddd ;border-radius :8px " ></div >
4- <div v-if =" noData" style =" position :absolute ;inset :0 ;display :flex ;align-items :center ;justify-content :center ;color :#9CA3AF ;font-size :14px ;pointer-events :none " >暂无数据</div >
4+ <div v-if =" noData && !loading " style =" position :absolute ;inset :0 ;display :flex ;align-items :center ;justify-content :center ;color :#9CA3AF ;font-size :14px ;pointer-events :none " >暂无数据</div >
55 </div >
66</template >
77
@@ -11,9 +11,11 @@ import { ref, computed, onMounted, onUnmounted, watch, nextTick } from 'vue'
1111const props = defineProps ({ data: { type: Array , default : () => [] }, colors: { type: Array , default : () => [] } })
1212
1313const el = ref (null )
14+ const loading = ref (true )
1415let chart = null
1516let echartsMod = null
1617let ro = null
18+ let initPromise = null
1719
1820const noData = computed (()=> {
1921 const arr = (props .data || []).map (d => Number (d? .value || 0 ))
@@ -53,44 +55,68 @@ const buildOption = ()=> {
5355 }
5456}
5557
56- onMounted (async () => {
57- console .log (' === [EChartsPie] Mounted ===' )
58- console .log (' [EChartsPie] props.data:' , props .data )
59- await nextTick ()
60- console .log (' [EChartsPie] el.value:' , el .value )
61- if (! el .value ) { console .warn (' [EChartsPie] el.value is null' ); return }
62- const w = el .value ? .clientWidth || 0 , h = el .value ? .clientHeight || 0
63- console .log (' [EChartsPie] mounted size:' , w, h)
64- try {
65- const mod = await import (' echarts' )
66- echartsMod = mod .default || mod
67- console .log (' [EChartsPie] ECharts loaded' )
68- } catch (err) {
69- console .error (' [EChartsPie] ECharts load error:' , err)
70- return
71- }
72- try {
73- chart = echartsMod .init (el .value , null , { renderer: ' svg' })
74- console .log (' [EChartsPie] chart initialized' )
75- } catch (e) {
76- console .error (' [EChartsPie] chart init error:' , e)
77- return
78- }
58+ async function initChart () {
59+ if (initPromise) return initPromise
60+ initPromise = (async () => {
61+ console .log (' === [EChartsPie] Initializing ===' )
62+ await nextTick ()
63+ if (! el .value ) { console .warn (' [EChartsPie] el.value is null' ); return false }
64+ const w = el .value ? .clientWidth || 0 , h = el .value ? .clientHeight || 0
65+ console .log (' [EChartsPie] size:' , w, h)
66+ if (w === 0 || h === 0 ) {
67+ console .warn (' [EChartsPie] Container has zero size, retrying...' )
68+ await new Promise (r => setTimeout (r, 100 ))
69+ await nextTick ()
70+ }
71+ try {
72+ if (! echartsMod) {
73+ const mod = await import (' echarts' )
74+ echartsMod = mod .default || mod
75+ console .log (' [EChartsPie] ECharts loaded' )
76+ }
77+ } catch (err) {
78+ console .error (' [EChartsPie] ECharts load error:' , err)
79+ return false
80+ }
81+ try {
82+ if (! chart) {
83+ chart = echartsMod .init (el .value , null , { renderer: ' svg' })
84+ console .log (' [EChartsPie] chart initialized' )
85+ ro = new ResizeObserver (() => { try { chart && chart .resize () } catch {} })
86+ ro .observe (el .value )
87+ }
88+ } catch (e) {
89+ console .error (' [EChartsPie] chart init error:' , e)
90+ return false
91+ }
92+ return true
93+ })()
94+ return initPromise
95+ }
96+
97+ async function updateChart () {
98+ const ok = await initChart ()
99+ if (! ok || ! chart) return
79100 const option = buildOption ()
80- console .log (' [EChartsPie] init series length:' , option? .series ? .[0 ]? .data ? .length )
101+ console .log (' [EChartsPie] update series length:' , option? .series ? .[0 ]? .data ? .length )
81102 chart .setOption (option, true )
82103 chart .resize ()
83104 setTimeout (() => { try { chart && chart .resize () } catch {} }, 120 )
84- ro = new ResizeObserver (() => { try { chart && chart .resize () } catch {} })
85- ro .observe (el .value )
105+ }
106+
107+ onMounted (async () => {
108+ console .log (' === [EChartsPie] Mounted ===' )
109+ console .log (' [EChartsPie] props.data:' , props .data )
110+ loading .value = true
111+ await updateChart ()
112+ loading .value = false
86113})
87114
88- watch (()=> props .data , (val ) => {
115+ watch (()=> props .data , async (val ) => {
89116 console .log (' [EChartsPie] data changed:' , val)
90- if (! chart) return
91- const option = buildOption ()
92- chart .setOption (option, true )
93- chart .resize ()
117+ loading .value = true
118+ await updateChart ()
119+ loading .value = false
94120}, { deep: true })
95121
96122onUnmounted (()=> { try { ro && ro .disconnect () } catch {} ro = null ; try { chart && chart .dispose () } catch {} chart = null })
0 commit comments