mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): add endpoint for retrieving growth data for the dashboard
This commit is contained in:
@@ -8,7 +8,12 @@ export function GrowthChart() {
|
||||
const [mode, setMode] = useState<Mode>('daily')
|
||||
useEffect(() => { fetchGrowth().then(setData).catch(() => {}) }, [])
|
||||
|
||||
const points = data ? (mode === 'daily' ? data.daily : data.weekly) : []
|
||||
// Trim leading empty buckets so the chart starts at the first day/week
|
||||
// with traffic instead of e.g. four weeks of zeros from before the
|
||||
// service went live.
|
||||
const rawPoints = data ? (mode === 'daily' ? data.daily : data.weekly) : []
|
||||
const firstActive = rawPoints.findIndex(p => p.requests > 0 || p.users > 0)
|
||||
const points = firstActive >= 0 ? rawPoints.slice(firstActive) : []
|
||||
const labels = points.map(p => 'day' in p ? p.day : p.week)
|
||||
const requests = points.map(p => p.requests)
|
||||
const users = points.map(p => p.users)
|
||||
@@ -22,12 +27,14 @@ export function GrowthChart() {
|
||||
<Tab active={mode === 'weekly'} onClick={() => setMode('weekly')}>week · 12</Tab>
|
||||
</div>
|
||||
</div>
|
||||
{points.length === 0 ? (
|
||||
{!data ? (
|
||||
<div className="text-xs text-white/40">loading…</div>
|
||||
) : points.length === 0 ? (
|
||||
<div className="text-xs text-white/40">no traffic yet</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
<Line label="Requests" values={requests} labels={labels} stroke="#60a5fa" />
|
||||
<Line label="Unique users" values={users} labels={labels} stroke="#a78bfa" />
|
||||
<Line label="Requests" values={requests} labels={labels} mode={mode} stroke="#60a5fa" />
|
||||
<Line label="Unique users" values={users} labels={labels} mode={mode} stroke="#a78bfa" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -47,21 +54,26 @@ function Tab({ active, onClick, children }: { active: boolean; onClick: () => vo
|
||||
)
|
||||
}
|
||||
|
||||
function Line({ label, values, labels, stroke }: {
|
||||
label: string; values: number[]; labels: string[]; stroke: string
|
||||
function Line({ label, values, labels, mode, stroke }: {
|
||||
label: string; values: number[]; labels: string[]; mode: Mode; stroke: string
|
||||
}) {
|
||||
const width = 600
|
||||
const height = 70
|
||||
const max = Math.max(1, ...values)
|
||||
const stepX = width / Math.max(1, values.length - 1)
|
||||
const n = values.length
|
||||
const stepX = n > 1 ? width / (n - 1) : 0
|
||||
const total = values.reduce((a, b) => a + b, 0)
|
||||
const latest = values[values.length - 1] ?? 0
|
||||
const pts = values.map((v, i) => {
|
||||
const x = i * stepX
|
||||
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.toFixed(1)},${y.toFixed(1)}`
|
||||
}).join(' ')
|
||||
const area = `0,${height} ${pts} ${width},${height}`
|
||||
return { x, y }
|
||||
})
|
||||
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))
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="mb-1 flex items-baseline justify-between text-xs">
|
||||
@@ -71,14 +83,55 @@ function Line({ label, values, labels, stroke }: {
|
||||
<span className="ml-3">total <span className="text-white/80">{total.toLocaleString()}</span></span>
|
||||
</span>
|
||||
</div>
|
||||
<svg viewBox={`0 0 ${width} ${height}`} className="w-full" preserveAspectRatio="none">
|
||||
<polyline points={area} fill={stroke} fillOpacity="0.08" stroke="none" />
|
||||
<polyline points={pts} fill="none" stroke={stroke} strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round" />
|
||||
<svg viewBox={`0 0 ${width} ${height + 10}`} className="w-full" 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}
|
||||
strokeLinecap="round" strokeLinejoin="round"
|
||||
vectorEffect="non-scaling-stroke" />
|
||||
)}
|
||||
{n === 1 && (
|
||||
<circle cx={xy[0].x} cy={xy[0].y} r={3} fill={stroke} />
|
||||
)}
|
||||
{ticks.map(t => {
|
||||
const x = n > 1 ? t.idx * stepX : width / 2
|
||||
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="flex justify-between font-mono text-[10px] text-white/30">
|
||||
<span>{labels[0]}</span>
|
||||
<span>{labels[labels.length - 1]}</span>
|
||||
<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>
|
||||
)
|
||||
}
|
||||
|
||||
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 }))
|
||||
const out: { idx: number; label: string }[] = []
|
||||
for (let i = 0; i < n; i++) {
|
||||
const idx = Math.round((i * (labels.length - 1)) / (n - 1))
|
||||
out.push({ idx, label: labels[idx] })
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function formatTick(label: string, mode: Mode): string {
|
||||
if (mode === 'daily') {
|
||||
// "2026-05-27" -> "05-27"
|
||||
return label.length >= 10 ? label.slice(5) : label
|
||||
}
|
||||
// "2026-W21" -> "W21"
|
||||
const parts = label.split('-')
|
||||
return parts[1] || label
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user