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 | 20x 26x 26x 26x 451x 26x 26x 451x 451x 451x 451x | import React from "react";
import { SPARK } from "./theme";
/**
* A row of thin columns, one per period, oldest left: how often something
* happened, beside the number it adds up to. The history is in the
* de-emphasis grey and the current period in the accent; a period with
* nothing is a hairline on the baseline, so a quiet stretch reads as quiet
* rather than missing.
*
* Each column's hover (and its title, for a screen reader's exploration)
* names its period and count, over a target the full height of the strip
* and as wide as the column and its gap — never only the painted pixels.
*/
export interface SparkBarsProps {
values: number[];
/** What it counts, for a screen reader: the total is appended. */
label: string;
/** Each column's hover: its period and count. */
titles?: string[];
barWidth?: number;
gap?: number;
height?: number;
className?: string;
}
export const SparkBars: React.FC<SparkBarsProps> = ({
values,
label,
titles,
barWidth = 2,
gap = 2,
height = 24,
className = "",
}) => {
Iif (!values.length) return null;
const width = values.length * barWidth + (values.length - 1) * gap;
const peak = Math.max(...values, 1);
const total = values.reduce((sum, v) => sum + v, 0);
const last = values.length - 1;
return (
<svg
width={width}
height={height}
className={`shrink-0 ${className}`}
role="img"
aria-label={`${label}: ${total.toLocaleString()} in all, ${values[last].toLocaleString()} in the latest period`}
>
{values.map((value, i) => {
const x = i * (barWidth + gap);
const h = value > 0 ? Math.max(2, (value / peak) * (height - 1)) : 1;
const tone =
value === 0 ? SPARK.empty : i === last ? SPARK.current : SPARK.history;
return (
<g key={i}>
<rect
x={x}
y={height - h}
width={barWidth}
height={h}
rx={value > 0 ? Math.min(1, barWidth / 2) : 0}
fill="currentColor"
className={tone}
/>
<rect
x={x - gap / 2}
y={0}
width={barWidth + gap}
height={height}
fill="transparent"
>
<title>{titles?.[i] ?? value.toLocaleString()}</title>
</rect>
</g>
);
})}
</svg>
);
};
export default SparkBars;
|