forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparkline.tsx
More file actions
47 lines (44 loc) · 1006 Bytes
/
Copy pathSparkline.tsx
File metadata and controls
47 lines (44 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
export function Sparkline({
data,
width = 96,
height = 32,
className,
}: {
data: number[];
width?: number;
height?: number;
className?: string;
}) {
if (data.length < 2) return null;
const max = Math.max(...data);
const min = Math.min(...data);
const range = max - min || 1;
const step = width / (data.length - 1);
const points = data
.map((v, i) => `${i * step},${height - ((v - min) / range) * height}`)
.join(" ");
const areaPoints = `0,${height} ${points} ${width},${height}`;
return (
<svg
width={width}
height={height}
viewBox={`0 0 ${width} ${height}`}
className={className}
preserveAspectRatio="none"
>
<polyline
points={areaPoints}
fill="currentColor"
className="opacity-10"
/>
<polyline
points={points}
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}