Hiệu năng
Cần chú ýTốc độ khung hình: trung bình 60 fps, p5 59.5 · Tác vụ dài: 0 tác vụ, tổng 0 ms, dài nhất 0 ms · Mã nguồn gzip 8.3 KB · Khi chạy 288 KB
Stacked barsMITHọa tiếtBencho
Component React Họa tiết của Lorenzo Cabra. Được thu thập nguyên vẹn từ Bencho theo giấy phép MIT và hiển thị trực tiếp tại đây.
Báo cáo chất lượng
Xếp loại và điểm số đến từ các kiểm tra tự động lúc dựng hoặc kiểm thử. Nếu một mục chưa chạy, nó được đánh dấu Chưa kiểm tra — không bao giờ bịa số liệu.
Tốc độ khung hình: trung bình 60 fps, p5 59.5 · Tác vụ dài: 0 tác vụ, tổng 0 ms, dài nhất 0 ms · Mã nguồn gzip 8.3 KB · Khi chạy 288 KB
Vấn đề 2 · Trình đọc màn hình: Đạt
Không có chuyển động liên tục
Không tràn
Số liệu tốc độ khung hình và tác vụ dài được đo trong Chromium không giao diện với WebGL phần mềm. Chúng chỉ nên được so sánh giữa các thành phần trên trang này, và không đại diện cho thiết bị thật.
Không áp dụng: không có hoạt ảnh nào thấy được khi đứng yên. Hoạt ảnh kích hoạt khi di chuột chưa được kiểm tra.

Kiểm tra ngày 15 thg 11, 2026 · Số liệu đến từ tập lệnh tự động, không phải mẫu viết tay.
Hiển thị đúng kích thước thật.
Bản sao bắt đầu bằng thông báo giấy phép. Hãy giữ lại khi dùng mã.
<div id="root"></div>:root{--bg:#f6f4f0;--ink:#17181a;--background:40 20% 96%;--foreground:24 10% 8%;--card:0 0% 100%}html,body{min-height:100%;margin:0;background:var(--bg);color:var(--ink)}html,body{height:100%;overflow:hidden}body{display:grid;place-items:center}#root{display:grid;place-items:center;max-width:100%;max-height:100%;box-sizing:border-box;padding:16px;color:hsl(var(--foreground))}#root.full{width:100%;height:100%;padding:0}@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation-play-state:paused!important;transition-duration:0s!important}}
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
/* ── Bencho tokens ─────────────────────────────────────────
The custom properties the free blocks read, at the values
bencho.dev draws them with. Light by default; dark under
[data-theme="dark"] on any ancestor, or automatically with
the system setting unless [data-theme="light"] says otherwise.
Map these to your own theme rather than keeping two: a block
only ever reads the names, never the values. */
:root {
--font-ui: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
--bg: #ffffff;
--board: #ffffff;
--card: #f7f7f6;
--surface: #f7f7f6;
--surface-2: #f1f1f0;
--surface-3: #ecebe7;
--ink: #17181a;
--ink-rgb: 23, 24, 26;
--ink-2: #3c3b37;
--ink-3: #5c5b56;
--ink-4: #6f6e68;
--ink-5: #85847e;
--on-ink: #ffffff;
--on-ink-rgb: 255, 255, 255;
--slab: #17181a;
--on-slab: #ffffff;
--slab-dark: #0e0f11;
--fill-slab: #ffffff;
--fill-on: #17181a;
--fill-on-rgb: 23, 24, 26;
--pane: var(--fill-slab);
--pane-edge: rgba(23, 24, 26, 0.08);
--pane-thumb: rgba(23, 24, 26, 0.07);
--inlay: rgba(255, 255, 255, 0.55);
--inlay-hi: rgba(23, 24, 26, 0.09);
--elev-rgb: 255, 255, 255;
--glass-rgb: 255, 255, 255;
--shadow-rgb: 23, 24, 26;
--wash: 0.06;
--pick: #0d99ff;
--signal: #2231dd;
--focus: #2231dd;
--ok: #15803d;
--ember: #b8431c;
color-scheme: light;
}
[data-theme="dark"] {
--bg: #000000;
--board: #1a1a1a;
--card: #0a0a0a;
--surface: #0f0f0f;
--surface-2: #181818;
--surface-3: #202020;
--ink: #ffffff;
--ink-rgb: 255, 255, 255;
--ink-2: #b4b4b4;
--ink-3: #9a9a9a;
--ink-4: #828282;
--ink-5: #6b6b6b;
--on-ink: #000000;
--on-ink-rgb: 0, 0, 0;
--slab: #1a1a1a;
--on-slab: #ffffff;
--slab-dark: #262626;
--fill-slab: #262626;
--fill-on: #ffffff;
--fill-on-rgb: 255, 255, 255;
--pane-edge: rgba(255, 255, 255, 0.08);
--pane-thumb: rgba(255, 255, 255, 0.07);
--inlay: rgba(255, 255, 255, 0.1);
--inlay-hi: rgba(255, 255, 255, 0.09);
--elev-rgb: 32, 32, 32;
--shadow-rgb: 0, 0, 0;
--wash: 0.09;
--pick: #3aa9ff;
--ok: #34d17a;
color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #000000;
--board: #1a1a1a;
--card: #0a0a0a;
--surface: #0f0f0f;
--surface-2: #181818;
--surface-3: #202020;
--ink: #ffffff;
--ink-rgb: 255, 255, 255;
--ink-2: #b4b4b4;
--ink-3: #9a9a9a;
--ink-4: #828282;
--ink-5: #6b6b6b;
--on-ink: #000000;
--on-ink-rgb: 0, 0, 0;
--slab: #1a1a1a;
--on-slab: #ffffff;
--slab-dark: #262626;
--fill-slab: #262626;
--fill-on: #ffffff;
--fill-on-rgb: 255, 255, 255;
--pane-edge: rgba(255, 255, 255, 0.08);
--pane-thumb: rgba(255, 255, 255, 0.07);
--inlay: rgba(255, 255, 255, 0.1);
--inlay-hi: rgba(255, 255, 255, 0.09);
--elev-rgb: 32, 32, 32;
--shadow-rgb: 0, 0, 0;
--wash: 0.09;
--pick: #3aa9ff;
--ok: #34d17a;
color-scheme: dark;
}
}
/* StackedBars.css */
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
/* ══ Stacked bars (lab/StackedBars.tsx) ═══ */
.stb {
--stb-c1: color-mix(in srgb, var(--fill-on, #17181a) 100%, var(--fill-slab, #ffffff));
--stb-c2: color-mix(in srgb, var(--fill-on, #17181a) 78%, var(--fill-slab, #ffffff));
--stb-c3: color-mix(in srgb, var(--fill-on, #17181a) 60%, var(--fill-slab, #ffffff));
--stb-c4: color-mix(in srgb, var(--fill-on, #17181a) 44%, var(--fill-slab, #ffffff));
--stb-c5: color-mix(in srgb, var(--fill-on, #17181a) 32%, var(--fill-slab, #ffffff));
--stb-c6: color-mix(in srgb, var(--fill-on, #17181a) 22%, var(--fill-slab, #ffffff));
--stb-alert: color-mix(in srgb, var(--fill-on, #17181a) 100%, var(--fill-slab, #ffffff));
--stb-soft: color-mix(in srgb, var(--fill-on, #17181a) 72%, var(--fill-slab, #ffffff));
--stb-mute: color-mix(in srgb, var(--fill-on, #17181a) 52%, var(--fill-slab, #ffffff));
--stb-faint: color-mix(in srgb, var(--fill-on, #17181a) 32%, var(--fill-slab, #ffffff));
--stb-w6: color-mix(in srgb, var(--fill-on, #17181a) 6%, transparent);
--stb-w12: color-mix(in srgb, var(--fill-on, #17181a) 12%, transparent);
position: relative;
display: flex;
flex-direction: column;
gap: 16px;
padding: 16px;
box-sizing: border-box;
border-radius: 20px;
background: var(--fill-slab, #ffffff);
color: var(--fill-on, #17181a);
font-family: var(--font-ui);
font-size: 13px;
user-select: none;
-webkit-user-select: none;}
.stb[data-colour="on"] {
--stb-c1: #9d7bf5; --stb-c2: #5b8def; --stb-c3: #9ab8db;
--stb-c4: #f2c46d; --stb-c5: #74dcae; --stb-c6: #e57bb8;
--stb-alert: #e5484d;}
[data-stroke="on"] .stb { box-shadow: 0 0 0 1px var(--pane-edge); }
/* header: icon tile, title, Day / Week */
.stb-head { display: flex; align-items: center; gap: 12px; }
.stb-titles { flex: 1; min-width: 0; }
.stb-title { font-size: 14px; font-weight: 500; letter-spacing: -0.01em; line-height: 18px; }
.stb-sub { font-size: 12px; color: var(--stb-mute); line-height: 16px; font-variant-numeric: tabular-nums; }
.stb-seg-ctl {
position: relative;
display: grid;
grid-template-columns: 1fr 1fr;
padding: 2px;
border-radius: 10px;
background: var(--stb-w6);}
.stb-thumb {
position: absolute;
top: 2px;
bottom: 2px;
left: 2px;
width: calc(50% - 2px);
border-radius: 8px;
background: var(--fill-slab, #ffffff);
transition: transform 220ms cubic-bezier(0.22, 1, 0.36, 1);}
.stb-seg-ctl[data-mode="Week"] .stb-thumb { transform: translateX(100%); }
.stb-tab {
position: relative;
appearance: none;
border: 0;
background: none;
padding: 0 10px;
height: 28px;
font: inherit;
font-size: 12px;
font-weight: 500;
color: var(--stb-mute);
cursor: pointer;
border-radius: 8px;
transition: color 160ms ease, scale 160ms cubic-bezier(0.22, 1, 0.36, 1);}
.stb-tab[data-on] { color: var(--fill-on, #17181a); }
.stb-tab:hover:not([data-on]) { color: var(--stb-soft); }
.stb-tab:active { scale: 0.96; }
.stb-tab:focus-visible { outline: 2px solid #2231dd; outline-offset: 0; }
/* the one figure */
.stb-figure { display: flex; align-items: baseline; gap: 8px; margin-bottom: -4px; }
.stb-num {
font-size: 30px;
font-weight: 500;
letter-spacing: -0.03em;
line-height: 32px;
font-variant-numeric: tabular-nums;}
.stb-unit { font-size: 13px; color: var(--stb-mute); }
/* Price flow's pop in: each character rises a touch from below, out
of a slight blur and from nothing, a hair past its mark — left to
right, 70ms apart */
.stb-digit { display: inline-block; }
.stb-digit[data-pop] {
animation: stb-pop 500ms cubic-bezier(0.34, 1.45, 0.64, 1) both;
animation-delay: calc(var(--i, 0) * 70ms);}
@media (prefers-reduced-motion: reduce) {
.stb-digit[data-pop] { animation: none; }
}
/* the plot: absolutely placed columns, each segment its own rounded box */
.stb-plot { position: relative; width: 100%; touch-action: none; cursor: crosshair; }
.stb-base {
position: absolute;
left: 0;
right: 0;
height: 1px;
background: var(--stb-w12);}
.stb-col { position: absolute; top: 0; bottom: 0; }
.stb-bar,
.stb-esc {
position: absolute;
left: 0;
right: 0;
border-radius: var(--stb-r);
transition:
height 600ms cubic-bezier(0.22, 1, 0.36, 1) calc(var(--i) * 14ms),
bottom 600ms cubic-bezier(0.22, 1, 0.36, 1) calc(var(--i) * 14ms),
background-color 160ms ease;}
.stb-bar { background-color: var(--c); }
.stb-esc { background-color: var(--stb-alert); }
/* Mono: escalated is full ink, hatched, so it never reads as Identity */
.stb[data-colour="off"] .stb-esc {
background-image: repeating-linear-gradient(-45deg, transparent 0 1.5px, var(--fill-slab, #ffffff) 1.5px 3px);}
/* scrubbing: the others step back by mixing toward the slab */
.stb[data-scrub] .stb-col:not([data-on]) .stb-bar {
background-color: color-mix(in srgb, var(--c) 38%, var(--fill-slab, #ffffff));}
.stb[data-scrub] .stb-col:not([data-on]) .stb-esc {
background-color: color-mix(in srgb, var(--stb-alert) 38%, var(--fill-slab, #ffffff));}
.stb-ring {
position: absolute;
top: 0;
left: 0;
box-sizing: border-box;
border: 1.5px solid var(--stb-mute);
border-radius: var(--stb-rr);
pointer-events: none;
opacity: 0;
transition: opacity 160ms ease;}
.stb[data-scrub] .stb-ring { opacity: 1; }
/* the tooltip: beside the column, gliding on a spring */
.stb-tip {
position: absolute;
top: 0;
left: 0;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 4px;
padding: 8px 10px;
border-radius: 10px;
background: var(--fill-slab, #ffffff);
/* filled like the card, parted from the bars by a soft grey hairline */
border: 1px solid color-mix(in srgb, var(--fill-on, #17181a) 10%, var(--fill-slab, #ffffff));
font-size: 12px;
line-height: 16px;
pointer-events: none;
opacity: 0;
transition: opacity 160ms ease;}
.stb[data-scrub] .stb-tip { opacity: 1; }
.stb-tip-head,
.stb-tip-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.stb-tip-head { justify-content: space-between; color: var(--stb-mute); margin-bottom: 2px; }
.stb-tip b { font-weight: 500; font-variant-numeric: tabular-nums; color: var(--fill-on, #17181a); }
.stb-tip-row span { flex: 1; color: var(--stb-soft); }
.stb-tip-row b { font-weight: 400; }
.stb-tip-row i {
flex: none;
width: 8px;
height: 8px;
border-radius: 2px;
background-color: var(--c);}
.stb-tip-row[data-esc] i { background-color: var(--stb-alert); }
.stb[data-colour="off"] .stb-tip-row[data-esc] i {
background-image: repeating-linear-gradient(-45deg, transparent 0 1.5px, var(--fill-slab, #ffffff) 1.5px 3px);}
/* one tiny footer: first and last date */
.stb-axis {
display: flex;
justify-content: space-between;
margin-top: -8px;
font-size: 11px;
line-height: 14px;
color: var(--stb-faint);
font-variant-numeric: tabular-nums;}
@keyframes stb-pop {
from { transform: translateY(0.18em); opacity: 0; filter: blur(2px); }
to { transform: none; opacity: 1; filter: blur(0); }}
// bencho source (MIT). npm i motion react · Tailwind CSS v4
// ── blocks/stacked-bars/StackedBars.tsx (https://github.com/cabralorenzo/bencho/blob/c65992417427bb09e661674ffa800bef29adef49/blocks/stacked-bars/StackedBars.tsx)
/*
MIT License
Copyright (c) 2026 Lorenzo Cabra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Not covered by this licence: the Bencho name and the goat mark, and the
photographs shown on bencho.dev (none are included here). Third-party
material keeps its own licence; see THIRD_PARTY.md.
Icon substitution (this copy only): lucide-react (ISC) was replaced with @tabler/icons-react (MIT).
Sample photographs were replaced with Incidental AI assets. The Bencho name and goat mark are not licensed for marketing use.
*/
import { type CSSProperties, useEffect, useMemo, useRef, useState } from "react";
import { animate, motion, useMotionValue } from "motion/react";
/* ══ Stacked bars ═════════════════════════════════════════
An alert-volume card: a stack of four sources for every day (or
week), and under the baseline the few that were escalated.
── RUN ALONG IT ────────────────────────────────────────
The column under the pointer gets a ring round its whole stack,
the rest step back (their colour mixed toward the slab, never
faded), and one tooltip glides beside it on a spring with the
date, the total and what made it up.
── DAY / WEEK ──────────────────────────────────────────
Swaps the dataset; every segment eases to its new height and
offset a beat after its left neighbour. The same transition
grows the bars out of the baseline the first time the card is
seen. */
const W = 300;
const PAD = 16;
const CW = W - PAD * 2; // the plot's width
const CH = 104; // room above the baseline
const BH = 22; // room below it for the escalated bars
const SEG_GAP = 2;
const LIFT = 2; // air between the baseline and the bars either side of it
const TIP_W = 132;
const SPRING = { type: "spring" as const, stiffness: 380, damping: 34 };
const CATS = ["Direct", "Search", "Social", "Email"] as const;
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const LAST = Date.UTC(2026, 9, 11); // the last day shown, Oct 11
const DAY_MS = 86400000;
type Mode = "Day" | "Week";
type Bar = { vals: number[]; esc: number; label: string; short: string };
/* a small seeded generator: the same chart on every load */
function mulberry(seed: number) {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const fmtDay = (ms: number) => {
const d = new Date(ms);
return `${MONTHS[d.getUTCMonth()]} ${d.getUTCDate()}`;
};
function makeBars(mode: Mode, n: number): Bar[] {
const rnd = mulberry(mode === "Day" ? 4396 : 9117);
const base = [520, 360, 210, 260];
const out: Bar[] = [];
for (let i = 0; i < 30; i++) {
const back = 29 - i; // only the last n are kept, so the data does not reshuffle with the count
if (mode === "Day") {
const ms = LAST - back * DAY_MS;
const wd = new Date(ms).getUTCDay();
const quiet = wd === 0 || wd === 6 ? 0.55 : 1;
const surge = rnd() < 0.12 ? 1.5 : 1;
const vals = base.map((b, k) => Math.round(b * quiet * (0.6 + rnd() * 0.8) * (k === 3 ? surge * surge : surge)));
const esc = Math.round(3 + rnd() * 9 * quiet * surge);
out.push({ vals, esc, label: fmtDay(ms), short: fmtDay(ms) });
} else {
const end = LAST - back * 7 * DAY_MS;
const start = end - 6 * DAY_MS;
const vals = base.map((b) => Math.round(b * 5.6 * (0.75 + rnd() * 0.5)));
const esc = Math.round(30 + rnd() * 44);
const s = new Date(start), e = new Date(end);
const label = s.getUTCMonth() === e.getUTCMonth()
? `${fmtDay(start)} – ${e.getUTCDate()}`
: `${fmtDay(start)} – ${fmtDay(end)}`;
out.push({ vals, esc, label, short: fmtDay(start) });
}
}
return out.slice(30 - n);
}
const fmt = (v: number) => Math.round(v).toLocaleString("en-US");
const sum = (a: number[]) => a.reduce((s, v) => s + v, 0);
export function StackedBars({
colour = "Colour",
bars = 20,
gap = 6,
}: {
colour?: "Mono" | "Colour";
bars?: number;
gap?: number;
}) {
const n = Math.max(2, Math.round(bars));
const [mode, setMode] = useState<Mode>("Day");
const [at, setAt] = useState<number | null>(null);
const [seen, setSeen] = useState(false);
const root = useRef<HTMLDivElement | null>(null);
const plot = useRef<HTMLDivElement | null>(null);
/* grow in once, the first time the card is on screen */
useEffect(() => {
const el = root.current;
if (!el || seen) return;
if (typeof IntersectionObserver === "undefined") { setSeen(true); return; }
const io = new IntersectionObserver(([e]) => {
if (e.isIntersecting) { setSeen(true); io.disconnect(); }
}, { threshold: 0.3 });
io.observe(el);
return () => io.disconnect();
}, [seen]);
const data = useMemo(() => makeBars(mode, n), [mode, n]);
/* geometry: the gap gives way before a bar gets thinner than 3px,
so 30 bars at the widest gap still draw */
const g = Math.max(1, Math.min(gap, (CW - 3 * n) / (n - 1)));
const bw = (CW - g * (n - 1)) / n;
const r = Math.min(2.5, bw / 2);
const totals = data.map((b) => sum(b.vals));
const maxT = Math.max(...totals);
const maxE = Math.max(...data.map((b) => b.esc));
const scale = (CH - LIFT - 2 - SEG_GAP * (CATS.length - 1)) / maxT;
const layout = data.map((b) => {
let y = 0;
const segs = b.vals.map((v) => {
const h = Math.max(2, v * scale);
const s = { bottom: y, h };
y += h + SEG_GAP;
return s;
});
return { segs, top: y - SEG_GAP, esc: Math.max(3, (b.esc / maxE) * (BH - LIFT - 2)) };
});
/* the headline pops in on a change, Price flow's way: every
character is keyed by a generation, so a new total is new
elements and each plays one short entrance, left to right.
The first paint does not play it; only a change does. */
const total = sum(totals);
const [gen, setGen] = useState(0);
const last = useRef(total);
useEffect(() => {
if (last.current === total) return;
last.current = total;
setGen((g) => g + 1);
}, [total]);
/* ring and tooltip: placed at once when they appear, glide after */
const ringX = useMotionValue(0);
const ringY = useMotionValue(0);
const ringH = useMotionValue(0);
const tipX = useMotionValue(0);
const was = useRef<number | null>(null);
useEffect(() => {
if (at === null || at >= n) { was.current = null; return; }
const L = layout[at];
const left = at * (bw + g);
const x = left - 3;
const up = seen ? L.top : 0;
const down = seen ? L.esc : 0;
const y = CH - LIFT - up - 3;
const h = up + LIFT + 1 + LIFT + down + 6;
/* beside the column: right while it fits there, else left */
const right = left + bw + 8;
const tx = right + TIP_W <= CW || left - 8 - TIP_W < 0
? Math.min(CW - TIP_W, right)
: left - 8 - TIP_W;
const go = (mv: typeof ringX, v: number) => {
if (was.current === null) mv.set(v);
else animate(mv, v, SPRING);
};
go(ringX, x); go(ringY, y); go(ringH, h); go(tipX, tx);
was.current = at;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [at, n, bw, g, mode, seen]);
const pick = (e: React.PointerEvent) => {
const b = plot.current?.getBoundingClientRect();
if (!b || b.width === 0) return;
const i = Math.floor(((e.clientX - b.left) / b.width) * n);
setAt(Math.max(0, Math.min(n - 1, i)));
};
const cur = at !== null && at < n ? data[at] : null;
const top2 = cur
? cur.vals.map((v, k) => ({ v, k })).sort((a, b) => b.v - a.v).slice(0, 2)
: [];
return (
<div
ref={root}
className="stb"
data-colour={colour === "Colour" ? "on" : "off"}
data-scrub={cur ? "" : undefined}
style={{ width: W, ["--stb-r" as string]: `${r}px`, ["--stb-rr" as string]: `${r + 3}px` } as CSSProperties}
>
<div className="stb-head">
<div className="stb-titles">
<div className="stb-title">Traffic</div>
<div className="stb-sub">Last {n} {mode === "Day" ? "days" : "weeks"}</div>
</div>
<div className="stb-seg-ctl" data-mode={mode} role="tablist">
<span className="stb-thumb" aria-hidden />
{(["Day", "Week"] as Mode[]).map((m) => (
<button
key={m}
type="button"
role="tab"
aria-selected={mode === m}
data-on={mode === m || undefined}
className="stb-tab"
onClick={() => { setMode(m); }}
>
{m}
</button>
))}
</div>
</div>
<div className="stb-figure">
<span className="stb-num" aria-label={fmt(total)}>
{fmt(total).split("").map((ch, i) => (
<span key={`${gen}-${i}`} className="stb-digit" data-pop={gen > 0 || undefined} style={{ ["--i" as string]: i } as CSSProperties} aria-hidden>
{ch}
</span>
))}
</span>
<span className="stb-unit">visits</span>
</div>
<div
ref={plot}
className="stb-plot"
style={{ height: CH + 1 + BH }}
onPointerDown={(e) => {
try { e.currentTarget.setPointerCapture(e.pointerId); } catch { /* scripted */ }
pick(e);
}}
onPointerMove={pick}
onPointerUp={(e) => {
try { e.currentTarget.releasePointerCapture(e.pointerId); } catch { /* scripted */ }
if (e.pointerType !== "mouse") setAt(null);
}}
onPointerCancel={() => setAt(null)}
onPointerLeave={() => setAt(null)}
>
<span className="stb-base" style={{ top: CH }} />
{data.map((_, i) => (
<div
key={i}
className="stb-col"
data-on={at === i || undefined}
style={{ left: i * (bw + g), width: bw, ["--i" as string]: i } as CSSProperties}
>
{layout[i].segs.map((s, k) => (
<span
key={k}
className="stb-bar"
style={{
["--c" as string]: `var(--stb-c${k + 1})`,
bottom: BH + 1 + LIFT + (seen ? s.bottom : 0),
height: seen ? s.h : 0,
} as CSSProperties}
/>
))}
<span
className="stb-esc"
style={{ top: CH + 1 + LIFT, height: seen ? layout[i].esc : 0 }}
/>
</div>
))}
<motion.span
className="stb-ring"
aria-hidden
style={{ x: ringX, y: ringY, height: ringH, width: bw + 6 }}
/>
<motion.div className="stb-tip" style={{ x: tipX, width: TIP_W }} aria-hidden={!cur}>
{cur && (
<>
<div className="stb-tip-head">
<span>{cur.label}</span>
<b>{fmt(totals[at!])}</b>
</div>
{top2.map(({ v, k }) => (
<div className="stb-tip-row" key={CATS[k]}>
<i style={{ ["--c" as string]: `var(--stb-c${k + 1})` } as CSSProperties} />
<span>{CATS[k]}</span>
<b>{fmt(v)}</b>
</div>
))}
<div className="stb-tip-row" data-esc="">
<i />
<span>Bounced</span>
<b>{cur.esc}</b>
</div>
</>
)}
</motion.div>
</div>
<div className="stb-axis">
<span>{data[0].short}</span>
<span>{data[n - 1].short}</span>
</div>
</div>
);
}Điều khiển · HTML · CSS · JS
Sửa mã và tinh chỉnh bằng các nút điều khiển. Bản xem trước cập nhật khi bạn gõ.

Thành phần này chạy từ bản đóng gói nên mã script chỉ xem được ở đây. Hãy sửa HTML và CSS; mã nguồn đầy đủ nằm trong thẻ Mã.
Sửa trực tiếp mọi thành phần, tinh chỉnh bằng nút điều khiển, tải mã nguồn đầy đủ và sao chép prompt nâng cao.
$49 một lầnGiá tạm thời
Chế độ thử: không thu tiền thật.
Miễn phí: xem trước, nút sao chép và ghi chú bài học.
Hoạ tiết lặp lại thêm chất liệu mà không tranh chỗ với chữ.
Nền phần mở đầu, vạch chia mục và vùng trống.
Đổi cỡ ô, giảm độ tương phản và chọn hai màu.
Thêm component "Stacked bars" từ Bencho (github.com/cabralorenzo/bencho) (MIT) vào ứng dụng React + Tailwind CSS của tôi. Tệp nguồn: https://github.com/cabralorenzo/bencho/blob/c65992417427bb09e661674ffa800bef29adef49/blocks/stacked-bars/StackedBars.tsx. Hoạ tiết lặp lại thêm chất liệu mà không tranh chỗ với chữ. Dùng cho: Nền phần mở đầu, vạch chia mục và vùng trống. Các thiết lập cần chỉnh: Đổi cỡ ô, giảm độ tương phản và chọn hai màu. Hỗ trợ giao diện sáng và tối, tôn trọng prefers-reduced-motion. Giữ thông báo giấy phép MIT.
Prompt dài hơn, có các bước tinh chỉnh, trợ năng và tích hợp.