feat(GrowthChart): refactor y-coordinate mapping and add

This commit is contained in:
shay
2026-05-29 10:27:45 -07:00
parent 410b503877
commit 78299fa2b9
@@ -59,20 +59,25 @@ function Line({ label, values, labels, mode, stroke }: {
}) { }) {
const width = 600 const width = 600
const height = 70 const height = 70
const vbH = height + 10 // viewBox height; bottom 10 holds the x-tick marks
const max = Math.max(1, ...values) const max = Math.max(1, ...values)
const n = values.length const n = values.length
const stepX = n > 1 ? width / (n - 1) : 0 const stepX = n > 1 ? width / (n - 1) : 0
const total = values.reduce((a, b) => a + b, 0) const total = values.reduce((a, b) => a + b, 0)
const latest = values[n - 1] ?? 0 const latest = values[n - 1] ?? 0
const xy = values.map((v, i) => { // Map a value to its y coordinate in viewBox space; reused for both the
const x = n > 1 ? i * stepX : width / 2 // line points and the y-axis label positions so they stay aligned.
const y = height - (v / max) * (height - 4) - 2 const yFor = (v: number) => height - (v / max) * (height - 4) - 2
return { x, y } 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(' ') 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). // Sealed area polygon: only valid when we have a real line (n >= 2).
const area = n > 1 ? `0,${height} ${pts} ${width},${height}` : '' const area = n > 1 ? `0,${height} ${pts} ${width},${height}` : ''
const ticks = pickTicks(labels, Math.min(6, n)) 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 ( return (
<div> <div>
@@ -83,38 +88,59 @@ function Line({ label, values, labels, mode, stroke }: {
<span className="ml-3">total <span className="text-white/80">{total.toLocaleString()}</span></span> <span className="ml-3">total <span className="text-white/80">{total.toLocaleString()}</span></span>
</span> </span>
</div> </div>
<svg viewBox={`0 0 ${width} ${height + 10}`} className="w-full" preserveAspectRatio="none"> <div className="flex gap-1.5">
{area && <polyline points={area} fill={stroke} fillOpacity="0.08" stroke="none" />} {/* y-axis */}
{n > 1 && ( <div className="relative w-7 shrink-0" style={{ height: vbH }}>
<polyline points={pts} fill="none" stroke={stroke} strokeWidth={1.5} {yMarks.map(v => (
strokeLinecap="round" strokeLinejoin="round" <span key={v}
vectorEffect="non-scaling-stroke" /> className="absolute right-0 -translate-y-1/2 font-mono text-[10px] tabular-nums text-white/35"
)} style={{ top: `${(yFor(v) / vbH) * 100}%` }}>
{n === 1 && ( {fmtCompact(v)}
<circle cx={xy[0].x} cy={xy[0].y} r={3} fill={stroke} /> </span>
)} ))}
{ticks.map(t => { </div>
const x = n > 1 ? t.idx * stepX : width / 2 {/* plot + x-axis */}
return ( <div className="min-w-0 flex-1">
<line key={`tick-${t.idx}`} x1={x} x2={x} y1={height} y2={height + 3} <svg viewBox={`0 0 ${width} ${vbH}`} className="w-full" style={{ height: vbH }} preserveAspectRatio="none">
stroke="rgb(255 255 255 / 0.15)" strokeWidth={1} {area && <polyline points={area} fill={stroke} fillOpacity="0.08" stroke="none" />}
vectorEffect="non-scaling-stroke" /> {n > 1 && (
) <polyline points={pts} fill="none" stroke={stroke} strokeWidth={1.5}
})} strokeLinecap="round" strokeLinejoin="round"
</svg> vectorEffect="non-scaling-stroke" />
<div className="relative mt-1 h-3"> )}
{ticks.map(t => ( {n === 1 && (
<span key={`label-${t.idx}`} <circle cx={xy[0].x} cy={xy[0].y} r={3} fill={stroke} />
className="absolute -translate-x-1/2 whitespace-nowrap font-mono text-[10px] text-white/40" )}
style={{ left: n > 1 ? `${(t.idx / (n - 1)) * 100}%` : '50%' }}> {ticks.map(t => {
{formatTick(t.label, mode)} const x = n > 1 ? t.idx * stepX : width / 2
</span> return (
))} <line key={`tick-${t.idx}`} x1={x} x2={x} y1={height} y2={height + 3}
stroke="rgb(255 255 255 / 0.15)" strokeWidth={1}
vectorEffect="non-scaling-stroke" />
)
})}
</svg>
<div className="relative mt-1 h-3">
{ticks.map(t => (
<span key={`label-${t.idx}`}
className="absolute -translate-x-1/2 whitespace-nowrap font-mono text-[10px] text-white/40"
style={{ left: n > 1 ? `${(t.idx / (n - 1)) * 100}%` : '50%' }}>
{formatTick(t.label, mode)}
</span>
))}
</div>
</div>
</div> </div>
</div> </div>
) )
} }
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 }[] { function pickTicks(labels: string[], n: number): { idx: number; label: string }[] {
if (labels.length === 0 || n <= 0) return [] if (labels.length === 0 || n <= 0) return []
if (labels.length <= n) return labels.map((label, idx) => ({ idx, label })) if (labels.length <= n) return labels.map((label, idx) => ({ idx, label }))