import { SparklinePoint } from '../api/client';
interface SparklineProps {
data: SparklinePoint[];
width?: number;
height?: number;
color?: string;
showTrend?: boolean;
}
export default function Sparkline({
data,
width = 120,
height = 40,
color,
showTrend = true,
}: SparklineProps) {
if (!data || data.length < 2) {
return (
No data
);
}
const prices = data.map((d) =>
typeof d.price === 'string' ? parseFloat(d.price) : d.price
);
const minPrice = Math.min(...prices);
const maxPrice = Math.max(...prices);
const priceRange = maxPrice - minPrice || 1;
// Calculate trend
const firstPrice = prices[0];
const lastPrice = prices[prices.length - 1];
const trend = lastPrice < firstPrice ? 'down' : lastPrice > firstPrice ? 'up' : 'flat';
// Determine color based on trend (green for down = good, red for up = bad for prices)
const lineColor = color || (trend === 'down' ? '#10b981' : trend === 'up' ? '#ef4444' : '#6366f1');
// Create SVG path
const padding = 4;
const chartWidth = width - padding * 2;
const chartHeight = height - padding * 2;
const points = prices.map((price, index) => {
const x = padding + (index / (prices.length - 1)) * chartWidth;
const y = padding + chartHeight - ((price - minPrice) / priceRange) * chartHeight;
return `${x},${y}`;
});
const pathD = `M ${points.join(' L ')}`;
// Create gradient fill path
const fillPoints = [...points];
fillPoints.push(`${padding + chartWidth},${padding + chartHeight}`);
fillPoints.push(`${padding},${padding + chartHeight}`);
const fillD = `M ${points.join(' L ')} L ${padding + chartWidth},${padding + chartHeight} L ${padding},${padding + chartHeight} Z`;
return (
{showTrend && (
{trend === 'down' && '↓'}
{trend === 'up' && '↑'}
{trend === 'flat' && '→'}
)}
);
}