fix(frontend): MetricCards fmt 兜底字符串值(治结果页整页渲染崩溃)
CI/CD / test (push) Successful in 8s
CI/CD / nas-deploy (push) Successful in 21s
CI/CD / nas-verify (push) Successful in 3s

后端 statistics 字段可返字符串(如 sharpe_ratio="0",vnpy原始统计),原 fmt 直接 value.toFixed 在字符串上抛 TypeError。stack trace 证实:Result.vue onMounted 赋值 relativeMetrics 后 MetricCards fmt 崩溃,中断 Vue re-render → echarts setOption 从未执行 → 指标卡+所有图表全空(cta_3919916e 零成交复现,canvas:0)。Number() 统一转换兜底。vue-tsc RC=0。
This commit is contained in:
2026-08-02 19:51:51 +08:00
parent 857d43c6c2
commit c049e0e97d
@@ -17,10 +17,13 @@ interface RelativeMetrics {
const props = defineProps<{ metrics: RelativeMetrics }>()
// null/NaN 安全格式化后端 NaN→None,退化指标如 sortino 可能为 null
// null/NaN/字符串 安全格式化后端 statistics 字段可能返字符串(如 sharpe_ratio="0",
// vnpy 原始统计),原 value.toFixed 在字符串上抛 TypeError 致整个结果页渲染崩溃
// (指标卡 + 所有图表全空)。Number() 统一转换兜底。
function fmt(value: number | null | undefined, digits: number, percent = false): string {
if (value == null || isNaN(value as number)) return '—'
return percent ? `${(value * 100).toFixed(digits)}%` : value.toFixed(digits)
const n = Number(value)
if (value == null || Number.isNaN(n)) return '—'
return percent ? `${(n * 100).toFixed(digits)}%` : n.toFixed(digits)
}
// 格式化百分比(×100,保留2位小数)