-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplePieCharts.tsx
More file actions
50 lines (41 loc) · 1.7 KB
/
Copy pathMultiplePieCharts.tsx
File metadata and controls
50 lines (41 loc) · 1.7 KB
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
import { PieChart, Pie, Cell, Legend, ResponsiveContainer } from 'recharts';
import { TicketOption, TicketOptionAnswer } from '../../ticket/model/ticketInformation';
const COLORS = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF', '#FF9F40'];
type MultiplePieChartsProps = {
responses: TicketOption[];
};
const MultiplePieCharts = ({ responses }: MultiplePieChartsProps) => {
const aggregateAnswers = (answers: TicketOptionAnswer[]) => {
const countMap: Record<string, number> = {};
answers.forEach(({ answer }) => {
countMap[answer] = (countMap[answer] || 0) + 1;
});
return Object.entries(countMap).map(([name, value]) => ({ name, value }));
};
if (responses.length === 0) {
return <p className="text-center text-gray-500 py-8">응답 데이터가 없습니다.</p>;
}
return (
<div className="grid grid-cols-1 gap-6 p-4 bg-white">
{responses.map((option, index) => {
const data = aggregateAnswers(option.ticketOptionAnswers);
return (
<div key={`${option.optionId}-${index}`} className="flex flex-col items-center">
<h3 className="mb-2 font-semibold text-lg">{option.optionName}</h3>
<ResponsiveContainer width="100%" height={350}>
<PieChart>
<Pie data={data} dataKey="value" nameKey="name" cx="50%" cy="50%" outerRadius={90} label stroke="none">
{data.map((_, i) => (
<Cell key={`cell-${i}`} fill={COLORS[i % COLORS.length]} />
))}
</Pie>
<Legend />
</PieChart>
</ResponsiveContainer>
</div>
);
})}
</div>
);
};
export default MultiplePieCharts;