chenlei 15 часов назад
Родитель
Сommit
2ddda52228
1 измененных файлов с 9 добавлено и 8 удалено
  1. 9 8
      src/pages/Abench/A1statistics/index.tsx

+ 9 - 8
src/pages/Abench/A1statistics/index.tsx

@@ -231,8 +231,9 @@ function A1statistics() {
 
 
     const myChart = echarts.getInstanceByDom(dom) || echarts.init(dom)
     const myChart = echarts.getInstanceByDom(dom) || echarts.init(dom)
 
 
-    // 计算总数用于百分比
+    // 计算总数用于百分比(total 为 0 时避免 NaN)
     const total = data.reduce((sum, item) => sum + item.value, 0)
     const total = data.reduce((sum, item) => sum + item.value, 0)
+    const pct = (val: number) => (total > 0 ? ((val / total) * 100).toFixed(1) + '%' : '0%')
     const dataMax = Math.max(...data.map(d => d.value))
     const dataMax = Math.max(...data.map(d => d.value))
     const yMax = Math.max(5, dataMax)
     const yMax = Math.max(5, dataMax)
     const yInterval = Math.max(1, Math.ceil(yMax / 5))
     const yInterval = Math.max(1, Math.ceil(yMax / 5))
@@ -248,7 +249,7 @@ function A1statistics() {
           return `
           return `
                   <div style="font-weight: bold;">${param.name}</div>
                   <div style="font-weight: bold;">${param.name}</div>
                   <div>数量: ${param.value}</div>
                   <div>数量: ${param.value}</div>
-                  <div>占比: ${((param.value / total) * 100).toFixed(1) + '%'}</div>
+                  <div>占比: ${pct(param.value)}</div>
                 `
                 `
         },
         },
         backgroundColor: 'rgba(255, 255, 255, 0.95)',
         backgroundColor: 'rgba(255, 255, 255, 0.95)',
@@ -325,7 +326,7 @@ function A1statistics() {
           type: 'bar',
           type: 'bar',
           data: data.map((item, index) => ({
           data: data.map((item, index) => ({
             value: item.value,
             value: item.value,
-            percentage: ((item.value / total) * 100).toFixed(1) + '%',
+            percentage: pct(item.value),
             // 为每个柱子设置独立的渐变
             // 为每个柱子设置独立的渐变
             itemStyle: {
             itemStyle: {
               // 关键修改:使用线性渐变实现单个柱子的渐变效果
               // 关键修改:使用线性渐变实现单个柱子的渐变效果
@@ -353,7 +354,7 @@ function A1statistics() {
             position: 'top',
             position: 'top',
             formatter: (params: any) => {
             formatter: (params: any) => {
               const itemData = params.data
               const itemData = params.data
-              return `${itemData.value}\n${((itemData.value / total) * 100).toFixed(1) + '%'}`
+              return `${itemData.value}\n${pct(itemData.value)}`
             },
             },
             fontSize: 10,
             fontSize: 10,
             fontWeight: 'bold',
             fontWeight: 'bold',
@@ -397,14 +398,15 @@ function A1statistics() {
 
 
     // 有数据时先清除,避免从「暂无数据」切换后 graphic 残留
     // 有数据时先清除,避免从「暂无数据」切换后 graphic 残留
     myChart.clear()
     myChart.clear()
-    // 计算总数用于百分比
+    // 计算总数用于百分比(total 为 0 时避免 NaN)
     const total = data.reduce((sum, item) => sum + item.value, 0)
     const total = data.reduce((sum, item) => sum + item.value, 0)
+    const pct = (val: number) => (total > 0 ? ((val / total) * 100).toFixed(1) : '0')
 
 
     const option = {
     const option = {
       color: DEFAULT_COLORS, // 设置颜色方案
       color: DEFAULT_COLORS, // 设置颜色方案
       tooltip: {
       tooltip: {
         trigger: 'item',
         trigger: 'item',
-        formatter: '{b}: {c} ({d}%)'
+        formatter: (params: any) => `${params.name}: ${params.value} (${pct(params.value)}%)`
       },
       },
       legend: {
       legend: {
         type: 'scroll',
         type: 'scroll',
@@ -418,8 +420,7 @@ function A1statistics() {
         formatter: function (name: string) {
         formatter: function (name: string) {
           const item = data.find(d => d.name === name)
           const item = data.find(d => d.name === name)
           if (item) {
           if (item) {
-            const percent = ((item.value / total) * 100).toFixed(1)
-            return `${name}  ${item.value}  ${percent}%`
+            return `${name}  ${item.value}  ${pct(item.value)}%`
           }
           }
           return name
           return name
         }
         }