import type { Pnl } from "../types"; export function PnlChart({ points }: { points: Pnl["points"] }) { const values = points .map((p) => p.value) .filter((v): v is number => v !== null && Number.isFinite(v)); if (!values.length) return
暂无可绘制的 PnL 数据
; const low = Math.min(...values), high = Math.max(...values), range = high - low || Math.max(1, Math.abs(high) * 0.1); const x = (index: number) => 62 + (index / Math.max(1, points.length - 1)) * 610; const y = (v: number) => 24 + ((high - v) / range) * 180; let segments = ""; let connected = false; points.forEach((point, i) => { if (point.value === null) { connected = false; return; } segments += `${connected ? " L" : " M"}${x(i)} ${y(point.value)}`; connected = true; }); return ( {[0, 1, 2, 3].map((i) => ( {(high - (i * range) / 3).toLocaleString(undefined, { maximumFractionDigits: 0, })} ))} {points.length === 1 && points[0].value !== null && ( )} {points[0]?.date.slice(0, 10)} {points.at(-1)?.date.slice(0, 10)} ); }