import * as React from 'react' import { Sparkline as MantineSparkline } from '@mantine/charts' type SparklineVariant = 'line' | 'area' | 'bar' interface SparklineProps { data: number[] variant?: SparklineVariant color?: string colors?: string[] width?: number height?: number strokeWidth?: number showLastPoint?: boolean className?: string } const Sparkline = React.forwardRef( ({ data, variant = 'line', color, colors, width = 80, height = 24, strokeWidth = 1.5, className, ...props }, ref) => { if (data.length === 0) { return
} // Bar variant: use custom SVG (Mantine Sparkline only supports area) if (variant === 'bar') { const min = Math.min(...data, 0) const max = Math.max(...data) const range = max - min || 1 const p = 2 const eh = height - p * 2 const bw = (width - p * 2) / data.length - 1 const barColor = color ?? 'currentColor' return (
{data.map((value, index) => { const bh = ((value - min) / range) * eh const x = p + index * ((width - p * 2) / data.length) + 0.5 const y = p + eh - bh const fill = colors ? colors[index % colors.length] : barColor return })}
) } // Line/area: use Mantine Sparkline return (
) } ) Sparkline.displayName = 'Sparkline' export { Sparkline, type SparklineProps, type SparklineVariant }