'use client'; import { useEffect, useState } from 'react'; import dynamic from 'next/dynamic'; import { ApexOptions } from 'apexcharts'; import { useTheme } from 'next-themes'; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }); interface StatusMahasiswaPerAngkatanData { status_kuliah: string; jumlah: number; } interface Props { selectedYear: string; } export default function StatusMahasiswaPieChartPerangkatan({ selectedYear }: Props) { const { theme } = useTheme(); const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { setLoading(true); const response = await fetch( `/api/mahasiswa/status?tahun_angkatan=${selectedYear}` ); if (!response.ok) { throw new Error('Failed to fetch data'); } const result = await response.json(); // Filter data for selected year and group by status const yearData = result.filter((item: any) => item.tahun_angkatan.toString() === selectedYear ); // Group by status_kuliah and sum jumlah const groupedData = yearData.reduce((acc: { [key: string]: number }, item: any) => { const status = item.status_kuliah || 'Tidak Diketahui'; acc[status] = (acc[status] || 0) + item.jumlah; return acc; }, {}); // Convert to array format const chartData = Object.entries(groupedData).map(([status_kuliah, jumlah]) => ({ status_kuliah, jumlah: jumlah as number })); // Sort by jumlah descending const sortedData = chartData.sort((a, b) => (b.jumlah as number) - (a.jumlah as number)); setData(sortedData); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; fetchData(); }, [selectedYear]); // Prepare data for pie chart const series = data.map(item => item.jumlah); const labels = data.map(item => item.status_kuliah); const chartOptions: ApexOptions = { chart: { type: 'pie', toolbar: { show: true, }, background: theme === 'dark' ? '#0F172B' : '#fff', }, labels: labels, dataLabels: { enabled: true, formatter: function (val: number) { return `${val.toFixed(0)}%`; }, style: { fontSize: '14px', fontFamily: 'Inter, sans-serif', fontWeight: '500' } }, legend: { position: 'bottom', fontSize: '12px', markers: { size: 12, }, itemMargin: { horizontal: 10, vertical: 5, }, labels: { colors: theme === 'dark' ? '#fff' : '#000' } }, colors: [ '#00E396', // Aktif - Green '#008FFB', // Lulus - Blue '#FEB019', // Cuti - Orange '#FF4560', // Non-Aktif - Red '#775DD0', // Lainnya - Purple ], tooltip: { theme: theme === 'dark' ? 'dark' : 'light', y: { formatter: function (val: number) { return val + ' mahasiswa'; } } }, plotOptions: { pie: { donut: { size: '0%', }, offsetY: 0, }, }, states: { hover: { filter: { type: 'darken', }, }, }, }; if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return ( Status Mahasiswa {selectedYear !== 'all' ? ` Angkatan ${selectedYear}` : ''}
{typeof window !== 'undefined' && ( )}
); }