import { Fragment } from "react" interface SparklineProps { data: Array width?: number height?: number color?: string filled?: boolean } export function Sparkline({ data, width = 120, height = 28, color = "currentColor", filled = false }: SparklineProps) { const valid = data.filter((value): value is number => typeof value === "number" && Number.isFinite(value)) if (!valid.length) return — const max = Math.max(...valid) const min = Math.min(...valid) const span = Math.max(1, max - min) const segments: Array> = [] let segment: Array<[number, number]> = [] data.forEach((value, index) => { if (typeof value !== "number" || !Number.isFinite(value)) { segment = []; return } if (!segment.length) segments.push(segment) segment.push([ data.length === 1 ? width / 2 : index / (data.length - 1) * width, height - ((value - min) / span) * (height - 2) - 1, ]) }) return ( {segments.map((points, index) => { if (points.length === 1) return const line = points.map(([x, y], i) => `${i === 0 ? "M" : "L"}${x.toFixed(1)},${y.toFixed(1)}`).join(" ") const area = `${line} L${points[points.length - 1][0]},${height} L${points[0][0]},${height} Z` return {filled && } })} ) }