import React from 'react'; const W = 720; const H = 300; const PAD = { left: 68, right: 24, top: 20, bottom: 56 }; const CW = W - PAD.left - PAD.right; const CH = H - PAD.top - PAD.bottom; function money(v) { const n = Number(v) || 0; if (n >= 1000) return `$${(n / 1000).toFixed(0)}k`; return `$${n.toFixed(0)}`; } function fullMoney(v) { return (Number(v) || 0).toLocaleString(undefined, { style: 'currency', currency: 'USD', maximumFractionDigits: 2, }); } function buildPoints(track, startBalance, maxMonths) { const all = [{ month: 0, balance: startBalance }, ...track]; return all.map(({ month, balance }) => ({ x: PAD.left + (month / maxMonths) * CW, y: PAD.top + CH - (balance / startBalance) * CH, month, balance, })); } function toLine(pts) { return pts.map(p => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(' '); } export default function PayoffChart({ minTrack = [], currentTrack = [], simTrack = [], startBalance = 1 }) { const maxMonths = Math.max(minTrack.length, currentTrack.length, simTrack.length, 12); const bal = Math.max(startBalance, 1); const minPts = buildPoints(minTrack, bal, maxMonths); const currentPts = buildPoints(currentTrack, bal, maxMonths); const simPts = buildPoints(simTrack, bal, maxMonths); const xStep = maxMonths <= 24 ? 6 : maxMonths <= 60 ? 12 : 24; const xLabels = []; for (let m = xStep; m <= maxMonths; m += xStep) { xLabels.push(m); } const yTicks = [0, 0.25, 0.5, 0.75, 1]; const showCurrent = currentTrack.length > 0 && currentTrack.some((c, i) => (minTrack[i]?.balance ?? null) !== c.balance); return (