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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
"use client";
import { TrendingUp, BarChart3, PieChart } from "lucide-react";
import { Bar, BarChart, CartesianGrid, XAxis, YAxis, Pie, PieChart as RechartsPieChart, Cell, ResponsiveContainer, LabelList } from "recharts";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/components/ui/chart";
import { DashboardStats } from "@/lib/dashboard/service";
interface DashboardOverviewChartProps {
data: DashboardStats[];
title: string;
description?: string;
}
// 차트 설정
const chartConfig = {
pending: {
label: "대기",
color: "hsl(var(--chart-1))", // 회색 계열
},
inProgress: {
label: "진행중",
color: "hsl(var(--chart-2))", // 파란색 계열
},
completed: {
label: "완료",
color: "hsl(var(--chart-3))", // 초록색 계열
},
} satisfies ChartConfig;
// 파이 차트용 색상
const PIE_COLORS = {
pending: "#6b7280",
inProgress: "#3b82f6",
completed: "#10b981"
};
export function DashboardOverviewChart({ data, title, description }: DashboardOverviewChartProps) {
// 바 차트용 데이터 변환
const barChartData = data.map(item => ({
name: item.displayName.length > 10 ?
item.displayName.substring(0, 10) + "..." :
item.displayName,
fullName: item.displayName,
pending: item.pending,
inProgress: item.inProgress,
completed: item.completed,
total: item.total
}));
// 파이 차트용 데이터 (전체 요약)
const totalPending = data.reduce((sum, item) => sum + item.pending, 0);
const totalInProgress = data.reduce((sum, item) => sum + item.inProgress, 0);
const totalCompleted = data.reduce((sum, item) => sum + item.completed, 0);
const totalTasks = totalPending + totalInProgress + totalCompleted;
const pieChartData = [
{ name: "대기", value: totalPending, color: PIE_COLORS.pending },
{ name: "진행중", value: totalInProgress, color: PIE_COLORS.inProgress },
{ name: "완료", value: totalCompleted, color: PIE_COLORS.completed }
].filter(item => item.value > 0);
// 완료율 계산
const completionRate = totalTasks > 0 ? Math.round((totalCompleted / totalTasks) * 100) : 0;
const isImproving = completionRate > 50; // 50% 이상이면 개선으로 간주
return (
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
{/* 바 차트 - 2/3 너비 */}
<Card className="md:col-span-2">
<CardHeader className="pb-4">
<CardTitle className="flex items-center gap-2 text-lg">
<BarChart3 className="h-5 w-5" />
{title} - 업무별 현황
</CardTitle>
{description && <CardDescription className="text-sm">{description}</CardDescription>}
</CardHeader>
<CardContent className="pb-2">
<ChartContainer
config={chartConfig}
className="max-h-[200px] w-full" // 고정 높이로 바 차트 크기 제한
>
<BarChart
accessibilityLayer
data={barChartData}
margin={{ top: 30, right: 15, left: 10, bottom: 5 }} // top margin 증가로 라벨 공간 확보
>
<CartesianGrid vertical={false} />
<XAxis
dataKey="name"
tickLine={false}
tickMargin={10}
axisLine={false}
fontSize={12}
interval={0}
// angle={-45}
textAnchor="end"
height={60}
/>
<YAxis
tickLine={false}
axisLine={false}
fontSize={12}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent
indicator="dashed"
labelFormatter={(label, payload) => {
const item = barChartData.find(d => d.name === label);
return item?.fullName || label;
}}
/>}
/>
<Bar
dataKey="pending"
fill="var(--color-pending)"
radius={[0, 0, 4, 4]}
stackId="status"
/>
<Bar
dataKey="inProgress"
fill="var(--color-inProgress)"
radius={[0, 0, 0, 0]}
stackId="status"
/>
<Bar
dataKey="completed"
fill="var(--color-completed)"
radius={[4, 4, 0, 0]}
stackId="status"
>
<LabelList
dataKey="total"
position="top"
offset={8}
className="fill-foreground"
fontSize={12}
formatter={(value: number) => value > 0 ? value : ''}
/>
</Bar>
</BarChart>
</ChartContainer>
</CardContent>
<CardFooter className="flex-col items-start gap-1 text-sm pt-2">
<div className="flex gap-2 items-center leading-none font-medium">
{totalTasks > 0 && (
<>
<TrendingUp className={`h-4 w-4 ${isImproving ? 'text-green-600' : 'text-orange-600'}`} />
<span>완료율 {completionRate}% - {isImproving ? '순조롭게 진행중' : '진행 필요'}</span>
</>
)}
</div>
<div className="text-muted-foreground leading-none">
총 {totalTasks}건의 업무 현황
</div>
</CardFooter>
</Card>
{/* 파이 차트 - 1/3 너비 */}
<Card className="md:col-span-1">
<CardHeader className="pb-4">
<CardTitle className="flex items-center gap-2 text-lg">
<PieChart className="h-5 w-5" />
업무 분포
</CardTitle>
<CardDescription className="text-sm">상태별 비율</CardDescription>
</CardHeader>
<CardContent className="pb-2">
<ChartContainer
config={chartConfig}
className="max-h-[200px] w-full" // 고정 높이로 바 차트 크기 제한
>
<RechartsPieChart>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent
hideLabel
formatter={(value, name) => [
`${value}건 (${Math.round((Number(value) / totalTasks) * 100)}%)`,
name
]}
/>}
/>
<Pie
data={pieChartData}
cx="50%"
cy="50%"
labelLine={false}
label={({ name, percent }) =>
percent > 0.1 ? `${(percent * 100).toFixed(0)}%` : ''
}
outerRadius={70} // 작은 공간에 맞게 더 작게 조정
fill="#8884d8"
dataKey="value"
>
{pieChartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
</RechartsPieChart>
</ChartContainer>
</CardContent>
<CardFooter className="pt-2">
<div className="grid grid-cols-1 gap-1 w-full text-center">
<div className="text-xs space-y-1">
<div className="flex items-center justify-between">
<span className="text-muted-foreground">대기</span>
<span className="font-medium text-gray-600">{totalPending}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-muted-foreground">진행</span>
<span className="font-medium text-blue-600">{totalInProgress}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-muted-foreground">완료</span>
<span className="font-medium text-green-600">{totalCompleted}</span>
</div>
</div>
</div>
</CardFooter>
</Card>
</div>
);
}
// 더 컴팩트한 버전 (높이를 더 많이 줄이고 싶은 경우)
export function CompactDashboardChart({ data, title, description }: DashboardOverviewChartProps) {
const totalPending = data.reduce((sum, item) => sum + item.pending, 0);
const totalInProgress = data.reduce((sum, item) => sum + item.inProgress, 0);
const totalCompleted = data.reduce((sum, item) => sum + item.completed, 0);
const totalTasks = totalPending + totalInProgress + totalCompleted;
const pieChartData = [
{ name: "대기", value: totalPending, color: PIE_COLORS.pending },
{ name: "진행중", value: totalInProgress, color: PIE_COLORS.inProgress },
{ name: "완료", value: totalCompleted, color: PIE_COLORS.completed }
].filter(item => item.value > 0);
const completionRate = totalTasks > 0 ? Math.round((totalCompleted / totalTasks) * 100) : 0;
return (
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
{/* 요약 통계 */}
<Card className="flex items-center p-4">
<div className="flex-1">
<h3 className="text-sm font-medium text-muted-foreground">완료율</h3>
<div className="text-2xl font-bold">{completionRate}%</div>
<p className="text-xs text-muted-foreground">총 {totalTasks}건</p>
</div>
<TrendingUp className={`h-8 w-8 ${completionRate > 50 ? 'text-green-600' : 'text-orange-600'}`} />
</Card>
{/* 컴팩트 파이 차트 */}
<Card className="col-span-2">
<CardHeader className="pb-3">
<CardTitle className="text-lg">{title}</CardTitle>
</CardHeader>
<CardContent className="pb-3">
<div className="flex items-center gap-6">
{/* 작은 파이 차트 */}
<div className="flex-shrink-0">
<ChartContainer
config={chartConfig}
className="h-[120px] w-[120px]"
>
<RechartsPieChart>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent
hideLabel
formatter={(value, name) => [`${value}건`, name]}
/>}
/>
<Pie
data={pieChartData}
cx="50%"
cy="50%"
outerRadius={50}
innerRadius={20} // 도넛 차트로 만들어 공간 절약
fill="#8884d8"
dataKey="value"
>
{pieChartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
</RechartsPieChart>
</ChartContainer>
</div>
{/* 통계 목록 */}
<div className="flex-1 grid grid-cols-1 gap-3">
<div className="flex items-center gap-3">
<div className="w-3 h-3 rounded-full bg-gray-500"></div>
<span className="text-sm">대기: {totalPending}건</span>
</div>
<div className="flex items-center gap-3">
<div className="w-3 h-3 rounded-full bg-blue-500"></div>
<span className="text-sm">진행중: {totalInProgress}건</span>
</div>
<div className="flex items-center gap-3">
<div className="w-3 h-3 rounded-full bg-green-500"></div>
<span className="text-sm">완료: {totalCompleted}건</span>
</div>
</div>
</div>
</CardContent>
</Card>
</div>
);
}
|