From 9cf2a2365b1e5cf0c5a25cf05281465b8f667428 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 05 二月 2026 11:38:14 +0800
Subject: [PATCH] Merge branch 'dev_New' of http://114.132.189.42:9002/r/product-inventory-management into dev_New

---
 src/components/Echarts/echarts.vue |  107 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 86 insertions(+), 21 deletions(-)

diff --git a/src/components/Echarts/echarts.vue b/src/components/Echarts/echarts.vue
index 2c51ace..54a6c39 100644
--- a/src/components/Echarts/echarts.vue
+++ b/src/components/Echarts/echarts.vue
@@ -1,12 +1,15 @@
 <template>
-  <div>
+  <div style="position: relative;">
     <div ref="chartRef" :style="chartStyle"></div>
+    <slot></slot>
   </div>
 </template>
 
 <script setup>
-import { ref, onMounted, onBeforeUnmount, watchEffect } from 'vue'
+import { ref, onMounted, onBeforeUnmount, nextTick } from 'vue'
 import * as echarts from 'echarts'
+
+const emit = defineEmits(['finished', 'click'])
 
 // Props
 const props = defineProps({
@@ -23,7 +26,7 @@
   },
   dataset: {
     type: Object,
-    default: () => {}
+    default: () => { }
   },
   xAxis: {
     type: Array,
@@ -75,10 +78,14 @@
     type: Array,
     default: () => []
   },
-	option: {
-		type: Object,
-		default: () => ({})
-	},
+  visualMap: {
+    type: Object,
+    default: () => ({})
+  },
+  option: {
+    type: Object,
+    default: () => ({})
+  },
 })
 
 import { watch } from 'vue'
@@ -86,11 +93,55 @@
 // Refs
 const chartRef = ref(null)
 let chartInstance = null
+let finishedHandler = null
+let initTimer = null
+let initAttempts = 0
+
+function clearInitTimer() {
+  if (initTimer) {
+    clearTimeout(initTimer)
+    initTimer = null
+  }
+}
+
+function isContainerReady() {
+  const el = chartRef.value
+  if (!el) return false
+  // offsetWidth/offsetHeight 鏇磋创杩戠湡瀹炲竷灞�锛堜负 0 寰�寰�浠h〃杩樻病甯冨眬/涓嶅彲瑙侊級
+  return el.offsetWidth > 0 && el.offsetHeight > 0
+}
+
+function initChartWhenReady() {
+  clearInitTimer()
+  initAttempts += 1
+
+  if (!isContainerReady()) {
+    // 绛夊鍣ㄧ湡姝f湁灏哄锛堥伩鍏嶉灞忓垵濮嬪寲鍋忕Щ/绌虹櫧锛岀儹鏇存柊鍚庢墠姝e父鐨勬儏鍐碉級
+    // 鏈�澶氶噸璇曠害 3 绉掞紝閬垮厤鏃犻檺寰幆
+    if (initAttempts < 60) {
+      initTimer = setTimeout(initChartWhenReady, 50)
+    }
+    return
+  }
+
+  if (chartInstance) return
+  chartInstance = echarts.init(chartRef.value)
+  finishedHandler = () => emit('finished')
+  chartInstance.on('finished', finishedHandler)
+  chartInstance.on('click', (params) => {
+    emit('click', params)
+  })
+  renderChart()
+  // setOption 鍚庤ˉ涓�娆� resize锛岀‘淇濋灞忓昂瀵告纭�
+  nextTick(() => {
+    if (chartInstance) chartInstance.resize()
+  })
+}
 
 // Methods
 function generateChart(option) {
-  const copiedOption = JSON.parse(JSON.stringify(option)) // 鉁� 娣辨嫹璐�
-  
+  const copiedOption = option
+
   if (copiedOption.series && copiedOption.series.length > 0) {
     copiedOption.series.forEach((s, index) => {
       if (s.type === 'line' && props.lineColors.length) {
@@ -104,7 +155,7 @@
       }
     })
   }
-  
+
   chartInstance.setOption(copiedOption)
 }
 
@@ -112,15 +163,17 @@
   const option = {
     color: props.color.length ? props.color : undefined,
     backgroundColor: props.options.backgroundColor || '#fff',
+    textStyle: props.options.textStyle || { color: '#333' },
     xAxis: props.xAxis,
     yAxis: props.yAxis,
     dataset: props.dataset,
     series: props.series,
     grid: props.grid,
     legend: props.legend,
-    tooltip: props.tooltip
+    tooltip: props.tooltip,
+    visualMap: Object.keys(props.visualMap).length ? props.visualMap : undefined,
   }
-  
+
   chartInstance.clear()
   generateChart(option)
 }
@@ -132,27 +185,39 @@
 
 // Lifecycle hooks
 onMounted(() => {
-  chartInstance = echarts.init(chartRef.value)
-  renderChart()
+  initAttempts = 0
+  initChartWhenReady()
   window.addEventListener('resize', windowResizeListener)
 })
 
 onBeforeUnmount(() => {
   if (chartInstance) {
     window.removeEventListener('resize', windowResizeListener)
+    if (finishedHandler) {
+      chartInstance.off('finished', finishedHandler)
+      finishedHandler = null
+    }
     chartInstance.dispose()
     chartInstance = null
   }
+  clearInitTimer()
 })
 
 // Watch all reactive props that affect the chart
 watch(
-    () => [props.xAxis, props.series],
-    () => {
-      if (chartInstance) {
-        renderChart()
-      }
-    },
-    { deep: true, immediate: true }
+  () => [props.xAxis, props.yAxis, props.series, props.legend, props.tooltip, props.visualMap],
+  () => {
+    // 濡傛灉棣栧睆杩樻病鍒濆鍖栨垚鍔燂紝绛夊緟瀹瑰櫒 ready 鍚庡啀娓叉煋
+    if (!chartInstance) {
+      initChartWhenReady()
+      return
+    }
+    renderChart()
+    // 鏁版嵁鍙樺寲鍚庤ˉ涓�娆� resize锛岄伩鍏嶅竷灞�鍙樺寲瀵艰嚧鐨勫亸绉�
+    nextTick(() => {
+      if (chartInstance) chartInstance.resize()
+    })
+  },
+  { deep: true, immediate: true }
 )
 </script>
\ No newline at end of file

--
Gitblit v1.9.3