From 78299fa2b967ceaeb294e647ea6f71cb96f8bf1a Mon Sep 17 00:00:00 2001 From: shay Date: Fri, 29 May 2026 10:27:45 -0700 Subject: [PATCH] feat(GrowthChart): refactor y-coordinate mapping and add --- .../frontend/src/components/GrowthChart.tsx | 90 ++++++++++++------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/generator/frontend/src/components/GrowthChart.tsx b/generator/frontend/src/components/GrowthChart.tsx index 4eb0d2e..ef7475f 100644 --- a/generator/frontend/src/components/GrowthChart.tsx +++ b/generator/frontend/src/components/GrowthChart.tsx @@ -59,20 +59,25 @@ function Line({ label, values, labels, mode, stroke }: { }) { const width = 600 const height = 70 + const vbH = height + 10 // viewBox height; bottom 10 holds the x-tick marks const max = Math.max(1, ...values) const n = values.length const stepX = n > 1 ? width / (n - 1) : 0 const total = values.reduce((a, b) => a + b, 0) const latest = values[n - 1] ?? 0 - const xy = values.map((v, i) => { - const x = n > 1 ? i * stepX : width / 2 - const y = height - (v / max) * (height - 4) - 2 - return { x, y } - }) + // Map a value to its y coordinate in viewBox space; reused for both the + // line points and the y-axis label positions so they stay aligned. + const yFor = (v: number) => height - (v / max) * (height - 4) - 2 + const xy = values.map((v, i) => ({ x: n > 1 ? i * stepX : width / 2, y: yFor(v) })) const pts = xy.map(p => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(' ') // Sealed area polygon: only valid when we have a real line (n >= 2). const area = n > 1 ? `0,${height} ${pts} ${width},${height}` : '' const ticks = pickTicks(labels, Math.min(6, n)) + // y-axis labels: top (max), middle, baseline (0). Percent positions are + // relative to the same viewBox the chart is drawn in. + const yMarks = max > 1 + ? [max, Math.round(max / 2), 0] + : [max, 0] return (
@@ -83,38 +88,59 @@ function Line({ label, values, labels, mode, stroke }: { total {total.toLocaleString()}
- - {area && } - {n > 1 && ( - - )} - {n === 1 && ( - - )} - {ticks.map(t => { - const x = n > 1 ? t.idx * stepX : width / 2 - return ( - - ) - })} - -
- {ticks.map(t => ( - 1 ? `${(t.idx / (n - 1)) * 100}%` : '50%' }}> - {formatTick(t.label, mode)} - - ))} +
+ {/* y-axis */} +
+ {yMarks.map(v => ( + + {fmtCompact(v)} + + ))} +
+ {/* plot + x-axis */} +
+ + {area && } + {n > 1 && ( + + )} + {n === 1 && ( + + )} + {ticks.map(t => { + const x = n > 1 ? t.idx * stepX : width / 2 + return ( + + ) + })} + +
+ {ticks.map(t => ( + 1 ? `${(t.idx / (n - 1)) * 100}%` : '50%' }}> + {formatTick(t.label, mode)} + + ))} +
+
) } +function fmtCompact(n: number): string { + if (n >= 10000) return `${Math.round(n / 1000)}k` + if (n >= 1000) return `${(n / 1000).toFixed(1)}k` + return String(Math.round(n)) +} + function pickTicks(labels: string[], n: number): { idx: number; label: string }[] { if (labels.length === 0 || n <= 0) return [] if (labels.length <= n) return labels.map((label, idx) => ({ idx, label }))