File size: 5,401 Bytes
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { useMemo, useState } from "react"
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts"
import { ChartContainer, ChartTooltip } from "@/components/ui/chart"
import { REL_NAME, type Lineage, type Rel } from "@/lib/types"
import { isoDay, mdy, num, toTime } from "@/lib/format"
import { timeTicks } from "@/lib/ticks"
import { Panel, Section } from "@/components/layout"
import { axisTick, relColor, relConfig, RelLegend } from "@/components/rel"

/**
 * Downloads per day of the community models, stacked by kind, about a week per point (the
 * hub-stats history, then our snapshots); drawn like the other time charts on the page.
 */
export function DailyLines({ L, rel, download }: { L: Lineage; rel: Rel[]; download?: string }) {
  const { rows, present, thin } = useMemo(() => {
    const days = [...new Set(L.daily.map((d) => d.day))].sort()
    // biggest kinds at the bottom of the stack
    const total = (r: Rel) => L.daily.reduce((a, d) => a + (d.rel === r ? d.n : 0), 0)
    const present = rel.filter((r) => total(r) > 0).sort((a, b) => total(b) - total(a))
    const at = new Map(L.daily.map((d) => [`${d.day}|${d.rel}`, d.n]))
    const rows = days.map((day) => ({ t: toTime(day), ...Object.fromEntries(present.map((r) => [r, at.get(`${day}|${r}`) ?? 0])) }))
    // a sliver of the total gets no outline, or it would trace the top of the stack in its colour
    const all = present.reduce((a, r) => a + total(r), 0) || 1
    const thin = new Set(present.filter((r) => total(r) / all < 0.02))
    return { rows, present, thin }
  }, [L, rel])
  const [hidden, setHidden] = useState<Set<Rel>>(new Set())
  const toggle = (r: Rel) => setHidden((h) => {
    const n = new Set(h)
    if (n.has(r)) n.delete(r)
    else if (n.size < present.length - 1) n.add(r)
    return n
  })
  const { ticks, label } = useMemo(() => (rows.length ? timeTicks(rows[0].t, rows.at(-1)!.t) : { ticks: [], label: String }), [rows])
  if (rows.length < 2) return null
  const shown = present.filter((r) => !hidden.has(r))

  return (
    <Section title="Daily downloads">
      <Panel download={download}>
        <RelLegend rels={[...present].sort((a, b) => rel.indexOf(a) - rel.indexOf(b))} hidden={hidden} onToggle={toggle} />
        <ChartContainer config={relConfig(present)} className="aspect-auto h-[360px] w-full">
          <AreaChart data={rows} margin={{ left: 4, right: 12, top: 8 }}>
            <defs>
              {present.map((r) => (
                <linearGradient key={r} id={`dd-${r}`} x1="0" y1="0" x2="0" y2="1">
                  <stop offset="0" stopColor={relColor(r)} stopOpacity={0.5} />
                  <stop offset="1" stopColor={relColor(r)} stopOpacity={0.08} />
                </linearGradient>
              ))}
            </defs>
            <CartesianGrid vertical={false} />
            <XAxis dataKey="t" type="number" scale="time" domain={["dataMin", "dataMax"]} ticks={ticks}
                   tickFormatter={label} tickLine={false} axisLine={false} tick={axisTick} />
            <YAxis tickLine={false} axisLine={false} tick={axisTick} width={56} tickFormatter={(v) => num(Number(v))} />
            <ChartTooltip cursor={{ stroke: "var(--muted-foreground)", strokeOpacity: 0.4 }}
                          content={({ active, payload }) => {
                            if (!active || !payload?.length) return null
                            const row = payload[0].payload as Record<string, number>
                            const items = [...shown].reverse().filter((r) => row[r] > 0)
                            return (
                              <div className="min-w-[13rem] rounded-xl border border-border/60 bg-background px-3.5 py-3 text-[15px] shadow-xl">
                                <div className="text-muted-foreground">{mdy(isoDay(row.t))}</div>
                                <div className="mt-0.5 font-display text-[24px] leading-tight font-semibold tabular-nums">
                                  {num(shown.reduce((a, r) => a + (row[r] ?? 0), 0))}
                                </div>
                                <div className="mt-2 space-y-1">
                                  {items.map((r) => (
                                    <div key={r} className="flex items-center justify-between gap-6">
                                      <span className="inline-flex items-center gap-2 text-muted-foreground">
                                        <span className="size-2.5 rounded-full" style={{ background: relColor(r) }} />
                                        {REL_NAME[r]}
                                      </span>
                                      <span className="font-medium tabular-nums">{num(row[r])}</span>
                                    </div>
                                  ))}
                                </div>
                              </div>
                            )
                          }} />
            {shown.map((r) => (
              <Area key={r} dataKey={r} stackId="d" type="monotone" stroke={relColor(r)} strokeWidth={thin.has(r) ? 0 : 1.75}
                    fill={`url(#dd-${r})`} dot={false} activeDot={{ r: 4, strokeWidth: 2, stroke: "var(--card)" }}
                    animationDuration={1100} animationEasing="ease-out" />
            ))}
          </AreaChart>
        </ChartContainer>
      </Panel>
    </Section>
  )
}