import * as React from "react" import { cn } from "@/lib/utils" type SvgSvgAttrs = Omit, "points" | "strokeWidth"> export interface SparklineProps extends SvgSvgAttrs { points: number[] stroke?: string fill?: string peakIndex?: number peakColor?: string showPeak?: boolean strokeWidth?: number min?: number max?: number width?: number height?: number } const VIEW_W = 100 const VIEW_H = 28 const PEAK_R = 0.9 export const Sparkline = React.forwardRef( ( { points, stroke = "var(--chart-1)", fill = "var(--chart-1)", peakIndex, peakColor = "var(--data-peak)", showPeak = true, strokeWidth, min, max, width, height, className, ...rest }, ref, ) => { const id = React.useId() if (!points || points.length < 2) { return ( ) } const lo = min ?? Math.min(...points) const hi = max ?? Math.max(...points) const range = hi - lo || 1 const coords = points.map((v, i) => { const x = (i / (points.length - 1)) * VIEW_W const y = VIEW_H - ((v - lo) / range) * VIEW_H return [x, y] as const }) const line = coords.map(([x, y], i) => `${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`).join(" ") const area = `${line} L${VIEW_W.toFixed(2)},${VIEW_H} L0,${VIEW_H} Z` const resolvedPeakIndex = typeof peakIndex === "number" ? peakIndex : points.indexOf(Math.max(...points)) const peak = showPeak && resolvedPeakIndex >= 0 ? coords[resolvedPeakIndex] : null return ( ) }, ) Sparkline.displayName = "Sparkline"