diff --git a/src/components/StockChart/StockChartAntdModal.js b/src/components/StockChart/StockChartAntdModal.js
index 8cddda26..bb1a0257 100644
--- a/src/components/StockChart/StockChartAntdModal.js
+++ b/src/components/StockChart/StockChartAntdModal.js
@@ -313,12 +313,29 @@ const StockChartAntdModal = ({
axisPointer: { type: 'cross' },
formatter: function(params) {
const d = params[0]?.dataIndex ?? 0;
- const priceChangePercent = ((prices[d] - prevClose) / prevClose * 100);
- const avgChangePercent = ((avgPrices[d] - prevClose) / prevClose * 100);
+ const price = prices[d];
+ const avgPrice = avgPrices[d];
+ const volume = volumes[d];
+
+ // 安全计算涨跌幅,处理 undefined/null/0 的情况
+ const safeCalcPercent = (val, base) => {
+ if (val == null || base == null || base === 0) return 0;
+ return ((val - base) / base * 100);
+ };
+
+ const priceChangePercent = safeCalcPercent(price, prevClose);
+ const avgChangePercent = safeCalcPercent(avgPrice, prevClose);
const priceColor = priceChangePercent >= 0 ? '#ef5350' : '#26a69a';
const avgColor = avgChangePercent >= 0 ? '#ef5350' : '#26a69a';
- return `时间:${times[d]}
现价:¥${prices[d]?.toFixed(2)} (${priceChangePercent >= 0 ? '+' : ''}${priceChangePercent.toFixed(2)}%)
均价:¥${avgPrices[d]?.toFixed(2)} (${avgChangePercent >= 0 ? '+' : ''}${avgChangePercent.toFixed(2)}%)
昨收:¥${prevClose?.toFixed(2)}
成交量:${Math.round(volumes[d]/100)}手`;
+ // 安全格式化数字
+ const safeFixed = (val, digits = 2) => (val != null && !isNaN(val)) ? val.toFixed(digits) : '-';
+ const formatPercent = (val) => {
+ if (val == null || isNaN(val)) return '-';
+ return (val >= 0 ? '+' : '') + val.toFixed(2) + '%';
+ };
+
+ return `时间:${times[d] || '-'}
现价:¥${safeFixed(price)} (${formatPercent(priceChangePercent)})
均价:¥${safeFixed(avgPrice)} (${formatPercent(avgChangePercent)})
昨收:¥${safeFixed(prevClose)}
成交量:${volume != null ? Math.round(volume/100) + '手' : '-'}`;
}
},
grid: [
@@ -337,6 +354,7 @@ const StockChartAntdModal = ({
position: 'left',
axisLabel: {
formatter: function(value) {
+ if (value == null || isNaN(value)) return '-';
return (value >= 0 ? '+' : '') + value.toFixed(2) + '%';
}
},
@@ -354,11 +372,12 @@ const StockChartAntdModal = ({
position: 'right',
axisLabel: {
formatter: function(value) {
+ if (value == null || isNaN(value)) return '-';
return (value >= 0 ? '+' : '') + value.toFixed(2) + '%';
}
}
},
- { type: 'value', gridIndex: 1, scale: true, axisLabel: { formatter: v => Math.round(v/100) + '手' } }
+ { type: 'value', gridIndex: 1, scale: true, axisLabel: { formatter: v => (v != null && !isNaN(v)) ? Math.round(v/100) + '手' : '-' } }
],
dataZoom: [
{ type: 'inside', xAxisIndex: [0, 1], start: 0, end: 100 },
diff --git a/src/views/Community/components/HeroPanel.js b/src/views/Community/components/HeroPanel.js
index 40a69a2a..3c957fe0 100644
--- a/src/views/Community/components/HeroPanel.js
+++ b/src/views/Community/components/HeroPanel.js
@@ -207,9 +207,12 @@ const CompactIndexCard = ({ indexCode, indexName }) => {
const raw = chartData.rawData[idx];
if (!raw) return '';
+ // 安全格式化数字
+ const safeFixed = (val, digits = 2) => (val != null && !isNaN(val)) ? val.toFixed(digits) : '-';
+
// 计算涨跌
const prevClose = raw.prev_close || (idx > 0 ? chartData.rawData[idx - 1]?.close : raw.open) || raw.open;
- const changeAmount = raw.close - prevClose;
+ const changeAmount = (raw.close != null && prevClose != null) ? (raw.close - prevClose) : 0;
const changePct = prevClose ? ((changeAmount / prevClose) * 100) : 0;
const isUp = changeAmount >= 0;
const color = isUp ? '#ef5350' : '#26a69a';
@@ -218,22 +221,22 @@ const CompactIndexCard = ({ indexCode, indexName }) => {
return `