Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 20x 4x 4x 20x 4x 4x 4x 10x 8x 8x 20x 20x 12x 10x 8x 20x | import React, { useState } from "react";
/**
* Columns, each split into the series it holds — a crawl's pages by what
* became of them, oldest left. Each segment is its series' colour with a 2px
* surface gap between segments; the legend names every series (identity is
* never colour alone); hovering or focusing a column reads out its label and
* every series' value; a visually hidden table carries the same numbers.
*
* Series classes are whole Tailwind strings (`bg-…`), chosen by the caller
* from a palette checked with the dataviz validator.
*/
export interface StackedSeries {
key: string;
label: string;
/** The segment's fill, light and dark: `bg-blue-600 dark:bg-blue-500`. */
className: string;
}
export interface StackedColumn {
key: string;
/** What the column is: a crawl's date. */
label: string;
values: Record<string, number>;
}
export interface StackedBarsProps {
columns: StackedColumn[];
/** Bottom first. */
series: StackedSeries[];
/** What the chart counts, for its table and a screen reader. */
caption: string;
height?: number;
}
export const StackedBars: React.FC<StackedBarsProps> = ({
columns,
series,
caption,
height = 96,
}) => {
const [active, setActive] = useState<number | null>(null);
const totals = columns.map((c) =>
series.reduce((sum, s) => sum + (c.values[s.key] ?? 0), 0),
);
const peak = Math.max(...totals, 1);
const shown = active !== null ? columns[active] : null;
return (
<div>
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 mb-2 text-[11px] text-gray-500 dark:text-gray-400">
{series.map((s) => (
<span key={s.key} className="inline-flex items-center gap-1.5">
<span className={`inline-block size-2.5 rounded-sm ${s.className}`} aria-hidden />
{s.label}
</span>
))}
<span className="ml-auto min-h-[1rem] text-gray-700 dark:text-gray-200 tabular-nums">
{shown &&
`${shown.label} · ${series
.map((s) => `${(shown.values[s.key] ?? 0).toLocaleString()} ${s.label.toLowerCase()}`)
.join(" · ")}`}
</span>
</div>
<div
className="flex items-end gap-[2px] border-b border-gray-200 dark:border-gray-700"
style={{ height }}
role="img"
aria-label={`${caption}, ${columns.length} columns`}
onMouseLeave={() => setActive(null)}
>
{columns.map((column, i) => {
const total = totals[i];
return (
<button
key={column.key}
type="button"
tabIndex={-1}
aria-hidden
onMouseEnter={() => setActive(i)}
onFocus={() => setActive(i)}
className={`flex-1 h-full min-w-0 p-0 flex flex-col-reverse justify-start gap-[2px] rounded-t-[4px] ${
active === i ? "opacity-80" : ""
}`}
>
{series.map((s) => {
const value = column.values[s.key] ?? 0;
if (!value) return null;
return (
<span
key={s.key}
className={`block w-full shrink-0 first:rounded-none last:rounded-t-[4px] ${s.className}`}
style={{ height: Math.max(2, (value / peak) * (height - 4)) }}
/>
);
})}
{total === 0 && (
<span className="block w-full h-px shrink-0 bg-gray-200 dark:bg-gray-700" />
)}
</button>
);
})}
</div>
{columns.length > 0 && (
<div className="flex justify-between text-[10px] text-gray-400 dark:text-gray-500 mt-1">
<span>{columns[0].label}</span>
<span>{columns[columns.length - 1].label}</span>
</div>
)}
<table className="sr-only">
<caption>{caption}</caption>
<thead>
<tr>
<th scope="col">Column</th>
{series.map((s) => (
<th key={s.key} scope="col">
{s.label}
</th>
))}
</tr>
</thead>
<tbody>
{columns.map((c) => (
<tr key={c.key}>
<th scope="row">{c.label}</th>
{series.map((s) => (
<td key={s.key}>{c.values[s.key] ?? 0}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};
export default StackedBars;
|