Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

Sparkline.tsx

83 lines1.8 KB

1'use client'
2​
3import {
4 ResponsiveContainer,
5 LineChart,
6 Line,
7 BarChart,
8 Bar,
9 AreaChart,
10 Area,
11} from 'recharts'
12import type { SparklineProps } from '@/lib/reports/types'
13​
14export function Sparkline({
15 data,
16 type = 'line',
17 color = '#06b6d4',
18 height = 32,
19 width = 100,
20 showEndValue = false,
21}: SparklineProps) {
22 // Transform data to recharts format
23 const chartData = data.map((value, index) => ({ index, value }))
24 const lastValue = data[data.length - 1]
25​
26 const formatValue = (val: number) => {
27 if (val >= 1000000) return `${(val / 1000000).toFixed(1)}M`
28 if (val >= 1000) return `${(val / 1000).toFixed(1)}K`
29 return val.toFixed(0)
30 }
31​
32 const renderChart = () => {
33 if (type === 'bar') {
34 return (
35 <BarChart data={chartData}>
36 <Bar dataKey="value" fill={color} radius={[2, 2, 0, 0]} />
37 </BarChart>
38 )
39 }
40​
41 if (type === 'area') {
42 return (
43 <AreaChart data={chartData}>
44 <Area
45 type="monotone"
46 dataKey="value"
47 stroke={color}
48 fill={color}
49 fillOpacity={0.3}
50 strokeWidth={1.5}
51 />
52 </AreaChart>
53 )
54 }
55​
56 return (
57 <LineChart data={chartData}>
58 <Line
59 type="monotone"
60 dataKey="value"
61 stroke={color}
62 strokeWidth={1.5}
63 dot={false}
64 />
65 </LineChart>
66 )
67 }
68​
69 return (
70 <div className="flex items-center gap-2">
71 <div style={{ width, height }}>
72 <ResponsiveContainer width="100%" height="100%">
73 {renderChart()}
74 </ResponsiveContainer>
75 </div>
76 {showEndValue && (
77 <span className="text-sm font-medium" style={{ color }}>
78 {formatValue(lastValue)}
79 </span>
80 )}
81 </div>
82 )