fix(frontend): MetricCards fmt 兜底字符串值(治结果页整页渲染崩溃)
后端 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:
@@ -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位小数)
|
||||
|
||||
Reference in New Issue
Block a user