"use client"; type PriceHistoryPoint = { date: string; price: number; }; interface PricingHistoryGraphProps { data: PriceHistoryPoint[]; currentPrice: number; } export function PricingHistoryGraph({ data, currentPrice, }: PricingHistoryGraphProps) { if (data.length === 0) return null; // Calculate chart dimensions const width = 100; const height = 60; const padding = 4; // Find min and max prices for scaling const prices = data.map((d) => d.price); const minPrice = Math.min(...prices); const maxPrice = Math.max(...prices); const priceRange = maxPrice - minPrice || 1; // Avoid division by zero // Generate path points const points = data.map((point, index) => { const x = padding + ((index / (data.length - 1)) * (width - padding * 2)); const normalizedPrice = (point.price - minPrice) / priceRange; const y = padding + (height - padding * 2) * (1 - normalizedPrice); return { x, y, price: point.price }; }); // Create SVG path const pathData = points .map((point, index) => `${index === 0 ? "M" : "L"} ${point.x} ${point.y}`) .join(" "); // Create area path (for gradient fill) const areaPath = `${pathData} L ${width - padding} ${height - padding} L ${padding} ${height - padding} Z`; // Format date for display const formatDate = (dateStr: string) => { const date = new Date(dateStr); return date.toLocaleDateString("en-US", { month: "short", day: "numeric" }); }; return (
Price History (30 Days)
Current: ${currentPrice.toFixed(2)}
{formatDate(data[0].date)} - {formatDate(data[data.length - 1].date)}
{/* Area fill */} {/* Line */} {/* Current price indicator */}
Low: ${minPrice.toFixed(2)} High: ${maxPrice.toFixed(2)}
); }