The best way to control Claude Code, Codex and more from any computer or phone

main
astro 221 lines 5.77 KB
Raw
1 ---
2 /**
3 * `<BarChart />` — server-rendered grouped horizontal bar chart for blog
4 * posts. Each row is one label with one bar per series. Styled with the
5 * brand tokens, so it follows the active theme.
6 *
7 * Usage in MDX:
8 * <BarChart
9 * title="Bundle size"
10 * unit=" KB"
11 * series={[{ name: "Official", tone: "official" }, { name: "distilled", tone: "distilled" }]}
12 * rows={[{ label: "S3", values: [157, 61], note: "2.6× smaller" }]}
13 * />
14 */
15 type Tone = "official" | "distilled" | "strict" | "before";
16
17 interface Props {
18 title: string;
19 caption?: string;
20 unit?: string;
21 decimals?: number;
22 log?: boolean;
23 /** Linear-scale maximum; longer bars are cut with a break mark. */
24 cap?: number;
25 series: ReadonlyArray<{ name: string; tone: Tone }>;
26 rows: ReadonlyArray<{ label: string; values: ReadonlyArray<number>; note?: string; /** Text shown instead of each value. */ labels?: ReadonlyArray<string> }>;
27 }
28
29 const { title, caption, unit = "", decimals = 1, log = false, cap, series, rows } = Astro.props;
30
31 const W = 600;
32 const LABEL_W = 170;
33 const VALUE_W = 74;
34 const BAR_W = W - LABEL_W - VALUE_W;
35 const BAR_H = 16;
36 const BAR_GAP = 3;
37 const ROW_GAP = 16;
38 const TOP = 12;
39
40 const all = rows.flatMap((r) => r.values);
41 const max = cap ?? Math.max(...all);
42 const min = Math.min(...all);
43 const logMin = Math.log10(min / 2);
44 const scale = (v: number) =>
45 log
46 ? ((Math.log10(v) - logMin) / (Math.log10(max) - logMin)) * BAR_W
47 : (Math.min(v, max) / max) * BAR_W;
48
49 const groupH = series.length * BAR_H + (series.length - 1) * BAR_GAP;
50 const rowH = groupH + ROW_GAP;
51 const H = TOP + rows.length * rowH;
52
53 const fmt = (v: number) =>
54 `${v.toLocaleString("en-US", { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}${unit}`;
55
56 const label = `${title}. ${rows
57 .map((r) => `${r.label}: ${r.values.map((v, i) => `${series[i]!.name} ${r.labels?.[i] ?? fmt(v)}`).join(", ")}`)
58 .join("; ")}`;
59 ---
60
61 <figure class="alc-bars" data-nosnippet>
62 <figcaption class="alc-bars__title">{title}</figcaption>
63 <ul class="alc-bars__legend">
64 {
65 series.map((s) => (
66 <li>
67 <span class={`alc-bars__swatch alc-bars__fill--${s.tone}`} />
68 {s.name}
69 </li>
70 ))
71 }
72 </ul>
73 <svg class="alc-bars__svg" viewBox={`0 0 ${W} ${H}`} role="img" aria-label={label}>
74 {
75 rows.map((row, r) => {
76 const y0 = TOP + r * rowH;
77 return (
78 <g>
79 <text class="alc-bars__label" x={LABEL_W - 14} y={y0 + groupH / 2 + (row.note ? -2 : 4)} text-anchor="end">
80 {row.label}
81 </text>
82 {row.note && (
83 <text class="alc-bars__note" x={LABEL_W - 14} y={y0 + groupH / 2 + 13} text-anchor="end">
84 {row.note}
85 </text>
86 )}
87 {row.values.map((v, i) => {
88 const y = y0 + i * (BAR_H + BAR_GAP);
89 const w = Math.max(2, scale(v));
90 return (
91 <g>
92 <rect class={`alc-bars__fill--${series[i]!.tone}`} x={LABEL_W} y={y} width={w} height={BAR_H} rx="3" />
93 {cap !== undefined && v > cap && (
94 <path class="alc-bars__break" d={`M${LABEL_W + w - 26},${y - 2} l6,${BAR_H + 4} M${LABEL_W + w - 18},${y - 2} l6,${BAR_H + 4}`} />
95 )}
96 <text class="alc-bars__value" x={LABEL_W + w + 6} y={y + BAR_H - 3.5}>
97 {row.labels?.[i] ?? fmt(v)}
98 </text>
99 </g>
100 );
101 })}
102 </g>
103 );
104 })
105 }
106 <line class="alc-bars__axis" x1={LABEL_W} x2={LABEL_W} y1={TOP - 6} y2={H - ROW_GAP + 6} />
107 </svg>
108 {caption && <p class="alc-bars__caption">{caption}</p>}
109 </figure>
110
111 <style>
112 .alc-bars {
113 margin: 2rem 0;
114 padding: 1.25rem 1.25rem 1rem;
115 background: var(--alc-bg-elev-1);
116 border: 1px solid var(--alc-hairline-2);
117 border-radius: 10px;
118 }
119
120 .alc-bars__title {
121 font-family: var(--alc-font-sans);
122 font-weight: 700;
123 font-size: 1.05rem;
124 color: var(--alc-fg-1);
125 }
126
127 .alc-bars__legend {
128 display: flex;
129 flex-wrap: wrap;
130 gap: 0.25rem 1.25rem;
131 margin: 0.5rem 0 0.75rem;
132 padding: 0;
133 list-style: none;
134 font-family: var(--alc-font-mono);
135 font-size: 0.78rem;
136 color: var(--alc-fg-2);
137 }
138
139 .alc-bars__legend li {
140 display: flex;
141 align-items: center;
142 gap: 0.4rem;
143 margin: 0;
144 }
145
146 .alc-bars__swatch {
147 display: inline-block;
148 width: 0.8rem;
149 height: 0.8rem;
150 border-radius: 3px;
151 }
152
153 span.alc-bars__fill--official {
154 background: var(--alc-terracotta);
155 }
156 span.alc-bars__fill--distilled {
157 background: var(--alc-accent);
158 }
159 span.alc-bars__fill--strict {
160 background: var(--alc-info);
161 }
162 span.alc-bars__fill--before {
163 background: var(--alc-hairline-3);
164 }
165
166 .alc-bars__svg {
167 display: block;
168 width: 100%;
169 height: auto;
170 overflow: visible;
171 }
172
173 rect.alc-bars__fill--official {
174 fill: var(--alc-terracotta);
175 }
176 rect.alc-bars__fill--distilled {
177 fill: var(--alc-accent);
178 }
179 rect.alc-bars__fill--strict {
180 fill: var(--alc-info);
181 }
182 rect.alc-bars__fill--before {
183 fill: var(--alc-hairline-3);
184 }
185
186 .alc-bars__label {
187 font-family: var(--alc-font-mono);
188 font-size: 13.5px;
189 font-weight: 600;
190 fill: var(--alc-fg-1);
191 }
192
193 .alc-bars__note {
194 font-family: var(--alc-font-mono);
195 font-size: 12px;
196 font-weight: 700;
197 fill: var(--alc-accent-text);
198 }
199
200 .alc-bars__value {
201 font-family: var(--alc-font-mono);
202 font-size: 12.5px;
203 fill: var(--alc-fg-2);
204 }
205
206 .alc-bars__break {
207 stroke: var(--alc-bg-elev-1);
208 stroke-width: 4;
209 }
210
211 .alc-bars__axis {
212 stroke: var(--alc-hairline-3);
213 stroke-width: 1;
214 }
215
216 .alc-bars__caption {
217 margin: 0.5rem 0 0;
218 font-size: 0.82rem;
219 color: var(--alc-fg-3);
220 }
221 </style>