mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(GrowthChart): refactor y-coordinate mapping and add
This commit is contained in:
@@ -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 (
|
||||
<div>
|
||||
@@ -83,7 +88,20 @@ function Line({ label, values, labels, mode, stroke }: {
|
||||
<span className="ml-3">total <span className="text-white/80">{total.toLocaleString()}</span></span>
|
||||
</span>
|
||||
</div>
|
||||
<svg viewBox={`0 0 ${width} ${height + 10}`} className="w-full" preserveAspectRatio="none">
|
||||
<div className="flex gap-1.5">
|
||||
{/* y-axis */}
|
||||
<div className="relative w-7 shrink-0" style={{ height: vbH }}>
|
||||
{yMarks.map(v => (
|
||||
<span key={v}
|
||||
className="absolute right-0 -translate-y-1/2 font-mono text-[10px] tabular-nums text-white/35"
|
||||
style={{ top: `${(yFor(v) / vbH) * 100}%` }}>
|
||||
{fmtCompact(v)}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{/* plot + x-axis */}
|
||||
<div className="min-w-0 flex-1">
|
||||
<svg viewBox={`0 0 ${width} ${vbH}`} className="w-full" style={{ height: vbH }} preserveAspectRatio="none">
|
||||
{area && <polyline points={area} fill={stroke} fillOpacity="0.08" stroke="none" />}
|
||||
{n > 1 && (
|
||||
<polyline points={pts} fill="none" stroke={stroke} strokeWidth={1.5}
|
||||
@@ -112,9 +130,17 @@ function Line({ label, values, labels, mode, stroke }: {
|
||||
))}
|
||||
</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 }[] {
|
||||
if (labels.length === 0 || n <= 0) return []
|
||||
if (labels.length <= n) return labels.map((label, idx) => ({ idx, label }))
|
||||
|
||||
Reference in New Issue
Block a user