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 | 20x 20x 16x 6x 2x 4x 6x 6x 32x 26x 6x 6x 13x 12x 12x 5x 13x 6x 2x 5x 12x 13x | import React from "react";
import { SPARK } from "./theme";
/**
* A small line of values, oldest left: the shape of a series beside the
* number it ends on. A `null` is no reading — the line breaks there rather
* than drawing a zero. A reference (a pass mark) is dashed; a `break` is a
* dotted rule where what the values measure changed (a new dataset version),
* titled so the reader is told why.
*
* Drawn in `currentColor` (`className`, the accent by default), so it holds
* no colour of its own.
*/
export interface SparklineProps {
values: (number | null)[];
/** What the line shows, for a screen reader: values are appended. */
label: string;
/** The value range drawn; the values' own range by default. */
domain?: [number, number];
reference?: number | null;
/** A rule before each index, with what changed there. */
breaks?: { index: number; title: string }[];
/** Formats each value in the screen reader's reading. */
format?: (value: number) => string;
width?: number;
height?: number;
className?: string;
}
const PAD = 3;
export const Sparkline: React.FC<SparklineProps> = ({
values,
label,
domain,
reference = null,
breaks = [],
format = String,
width = 120,
height = 30,
className = SPARK.current,
}) => {
const readings = values.filter((v): v is number => typeof v === "number");
if (readings.length < 2) {
return <span className="inline-block shrink-0" style={{ width }} />;
}
const [low, high] = domain ?? [Math.min(...readings), Math.max(...readings)];
const span = high - low || 1;
const x = (i: number) =>
PAD + (values.length > 1 ? (i * (width - 2 * PAD)) / (values.length - 1) : 0);
const y = (v: number) => PAD + (1 - (v - low) / span) * (height - 2 * PAD);
// One polyline per unbroken run of readings.
const runs: number[][] = [];
values.forEach((v, i) => {
if (typeof v !== "number") return;
const run = runs[runs.length - 1];
if (run && typeof values[i - 1] === "number") run.push(i);
else runs.push([i]);
});
const lastIndex = values.map((v) => typeof v === "number").lastIndexOf(true);
return (
<svg
width={width}
height={height}
className={`shrink-0 ${className}`}
role="img"
aria-label={`${label}, oldest first: ${readings.map(format).join(", ")}`}
>
{reference !== null && (
<line
x1={PAD}
x2={width - PAD}
y1={y(reference)}
y2={y(reference)}
className={SPARK.reference}
strokeDasharray="3 3"
/>
)}
{breaks.map(({ index, title }) => (
<line
key={index}
x1={(x(index - 1) + x(index)) / 2}
x2={(x(index - 1) + x(index)) / 2}
y1={0}
y2={height}
className={SPARK.reference}
strokeDasharray="1 2"
>
<title>{title}</title>
</line>
))}
{runs.map((run) => (
<polyline
key={run[0]}
fill="none"
stroke="currentColor"
strokeWidth={1.5}
points={run.map((i) => `${x(i)},${y(values[i] as number)}`).join(" ")}
/>
))}
{values.map((v, i) =>
typeof v === "number" ? (
<circle
key={i}
cx={x(i)}
cy={y(v)}
r={i === lastIndex ? 2.5 : 1.5}
fill="currentColor"
/>
) : null,
)}
</svg>
);
};
export default Sparkline;
|