Open History to see the last commit
1'use client'
2
3import {
4 ResponsiveContainer,
5 LineChart,
6 Line,
7 BarChart,
8 Bar,
9 AreaChart,
10 Area,
11 PieChart,
12 Pie,
13 Cell,
14 XAxis,
15 YAxis,
16 CartesianGrid,
17 Tooltip,
18 Legend,
19} from 'recharts'
20import type { ChartProps } from '@/lib/reports/types'
21
22const DEFAULT_COLORS = [
23 '#06b6d4', // cyan-500
24 '#8b5cf6', // violet-500
25 '#f59e0b', // amber-500
26 '#10b981', // emerald-500
27 '#ef4444', // red-500
28 '#3b82f6', // blue-500
29 '#ec4899', // pink-500
30]
31
32export function Chart({
33 type,
34 data,
35 xKey,
36 yKey,
37 height = 300,
38 showLegend,
39 showGrid = true,
40 showTooltip = true,
41 colors = DEFAULT_COLORS,
42 animate = true,
43}: ChartProps) {
44 const yKeys = Array.isArray(yKey) ? yKey : [yKey]
45 const showLegendFinal = showLegend ?? yKeys.length > 1
46
47 const commonAxisProps = {
48 tick: { fill: 'var(--report-text-muted)', fontSize: 12 },
49 axisLine: { stroke: 'var(--report-border)' },
50 tickLine: { stroke: 'var(--report-border)' },
51 }
52
53 const tooltipStyle = {
54 contentStyle: {
55 backgroundColor: 'var(--report-card-bg)',
56 border: '1px solid var(--report-border)',
57 borderRadius: '8px',
58 color: 'var(--report-text)',
59 },
60 }
61
62 if (type === 'pie' || type === 'donut') {
63 return (
64 <ResponsiveContainer width="100%" height={height}>
65 <PieChart>
66 <Pie
67 data={data}
68 dataKey={yKeys[0]}
69 nameKey={xKey}
70 cx="50%"
71 cy="50%"
72 innerRadius={type === 'donut' ? '60%' : 0}
73 outerRadius="80%"
74 paddingAngle={2}
75 isAnimationActive={animate}
76 >
77 {data.map((_, index) => (
78 <Cell
79 key={`cell-${index}`}
80 fill={colors[index % colors.length]}
81 />
82 ))}
83 </Pie>
84 {showTooltip && <Tooltip {...tooltipStyle} />}
85 {showLegendFinal && (
86 <Legend
87 wrapperStyle={{ color: 'var(--report-text)' }}
88 formatter={(value) => (
89 <span style={{ color: 'var(--report-text)' }}>{value}</span>
90 )}
91 />
92 )}
93 </PieChart>
94 </ResponsiveContainer>
95 )
96 }
97
98 if (type === 'bar') {
99 return (
100 <ResponsiveContainer width="100%" height={height}>
101 <BarChart data={data}>
102 {showGrid && (
103 <CartesianGrid
104 strokeDasharray="3 3"
105 stroke="var(--report-border)"
106 opacity={0.5}
107 />
108 )}
109 <XAxis dataKey={xKey} {...commonAxisProps} />
110 <YAxis {...commonAxisProps} />
111 {showTooltip && <Tooltip {...tooltipStyle} />}
112 {showLegendFinal && (
113 <Legend
114 wrapperStyle={{ color: 'var(--report-text)' }}
115 formatter={(value) => (
116 <span style={{ color: 'var(--report-text)' }}>{value}</span>
117 )}
118 />
119 )}
120 {yKeys.map((key, index) => (
121 <Bar
122 key={key}
123 dataKey={key}
124 fill={colors[index % colors.length]}
125 isAnimationActive={animate}
126 radius={[4, 4, 0, 0]}
127 />
128 ))}
129 </BarChart>
130 </ResponsiveContainer>
131 )
132 }
133
134 if (type === 'area') {
135 return (
136 <ResponsiveContainer width="100%" height={height}>
137 <AreaChart data={data}>
138 {showGrid && (
139 <CartesianGrid
140 strokeDasharray="3 3"
141 stroke="var(--report-border)"
142 opacity={0.5}
143 />
144 )}
145 <XAxis dataKey={xKey} {...commonAxisProps} />
146 <YAxis {...commonAxisProps} />
147 {showTooltip && <Tooltip {...tooltipStyle} />}
148 {showLegendFinal && (
149 <Legend
150 wrapperStyle={{ color: 'var(--report-text)' }}
151 formatter={(value) => (
152 <span style={{ color: 'var(--report-text)' }}>{value}</span>
153 )}
154 />
155 )}
156 {yKeys.map((key, index) => (
157 <Area
158 key={key}
159 type="monotone"
160 dataKey={key}
161 stroke={colors[index % colors.length]}
162 fill={colors[index % colors.length]}
163 fillOpacity={0.2}
164 isAnimationActive={animate}
165 />
166 ))}
167 </AreaChart>
168 </ResponsiveContainer>
169 )
170 }
171
172 // Default: line chart
173 return (
174 <ResponsiveContainer width="100%" height={height}>
175 <LineChart data={data}>
176 {showGrid && (
177 <CartesianGrid
178 strokeDasharray="3 3"
179 stroke="var(--report-border)"
180 opacity={0.5}
181 />
182 )}
183 <XAxis dataKey={xKey} {...commonAxisProps} />
184 <YAxis {...commonAxisProps} />
185 {showTooltip && <Tooltip {...tooltipStyle} />}
186 {showLegendFinal && (
187 <Legend
188 wrapperStyle={{ color: 'var(--report-text)' }}
189 formatter={(value) => (
190 <span style={{ color: 'var(--report-text)' }}>{value}</span>
191 )}
192 />
193 )}
194 {yKeys.map((key, index) => (
195 <Line
196 key={key}
197 type="monotone"
198 dataKey={key}
199 stroke={colors[index % colors.length]}
200 strokeWidth={2}
201 dot={false}
202 isAnimationActive={animate}
203 />
204 ))}
205 </LineChart>
206 </ResponsiveContainer>
207 )
208}