| 10 | } |
| 11 | |
| 12 | export function SimpleChart({ |
| 13 | width = 300, |
| 14 | height = 100, |
| 15 | points = [0, 10, 5, 8, 12, 4, 7], |
| 16 | strokeWidth = 2, |
| 17 | strokeColor = '#2563eb', |
| 18 | className, |
| 19 | }: SimpleChartProps) { |
| 20 | // Skip if no points |
| 21 | if (!points.length) return null; |
| 22 | |
| 23 | // Calculate scaling factors |
| 24 | const maxValue = Math.max(...points); |
| 25 | const xStep = width / (points.length - 1); |
| 26 | const yScale = height / maxValue; |
| 27 | |
| 28 | // Generate path commands |
| 29 | const pathCommands = points |
| 30 | .map((point, index) => { |
| 31 | const x = index * xStep; |
| 32 | const y = height - point * yScale; |
| 33 | return `${index === 0 ? 'M' : 'L'} ${x},${y}`; |
| 34 | }) |
| 35 | .join(' '); |
| 36 | |
| 37 | // Create area path by adding bottom corners |
| 38 | const areaPath = `${pathCommands} L ${width},${height} L 0,${height} Z`; |
| 39 | |
| 40 | // Generate unique gradient ID |
| 41 | const gradientId = `gradient-${strokeColor |
| 42 | .replace('#', '') |
| 43 | .replaceAll('(', '') |
| 44 | .replaceAll(')', '')}`; |
| 45 | |
| 46 | return ( |
| 47 | <svg |
| 48 | viewBox={`0 0 ${width} ${height}`} |
| 49 | className={`w-full ${className ?? ''}`} |
| 50 | > |
| 51 | <defs> |
| 52 | <linearGradient id={gradientId} x1="0" x2="0" y1="0" y2="1"> |
| 53 | <stop offset="0%" stopColor={strokeColor} stopOpacity="0.3" /> |
| 54 | <stop offset="100%" stopColor={strokeColor} stopOpacity="0" /> |
| 55 | </linearGradient> |
| 56 | </defs> |
| 57 | |
| 58 | {/* Area fill */} |
| 59 | {/* <path d={areaPath} fill={`url(#${gradientId})`} /> */} |
| 60 | |
| 61 | {/* Stroke line */} |
| 62 | <path |
| 63 | d={pathCommands} |
| 64 | fill="none" |
| 65 | stroke={strokeColor} |
| 66 | strokeWidth={strokeWidth} |
| 67 | /> |
| 68 | </svg> |
| 69 | ); |