'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"; // Dynamically import ApexCharts to avoid SSR issues const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }); interface JenisPendaftaranLulusData { tahun_angkatan: number; jenis_pendaftaran: string; jumlah_lulus_tepat_waktu: number; } interface Props { selectedYear: string; } export default function JenisPendaftaranLulusPieChart({ 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); setError(null); const url = `/api/mahasiswa/jenis-pendaftaran-lulus?tahun_angkatan=${selectedYear}`; const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch data: ${response.status} ${response.statusText}`); } const result = await response.json(); setData(result); } catch (err) { console.error('Error in fetchData:', err); setError(err instanceof Error ? err.message : 'An error occurred while fetching data'); } finally { setLoading(false); } }; fetchData(); }, [selectedYear]); const chartOptions: ApexOptions = { chart: { type: 'pie', toolbar: { show: true, tools: { download: true, selection: true, zoom: true, zoomin: true, zoomout: true, pan: true, reset: true } }, background: theme === 'dark' ? '#0F172B' : '#fff', }, labels: [], colors: ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899'], legend: { position: 'bottom', fontSize: '14px', markers: { size: 12, strokeWidth: 0 }, itemMargin: { horizontal: 10 }, labels: { colors: theme === 'dark' ? '#fff' : '#000', } }, dataLabels: { enabled: true, formatter: function (val: number) { return `${val.toFixed(0)}%`; }, style: { fontSize: '14px', fontFamily: 'Inter, sans-serif', fontWeight: '500' }, offsetY: 0, dropShadow: { enabled: false } }, tooltip: { theme: theme === 'dark' ? 'dark' : 'light', y: { formatter: function (val: number) { return val + " mahasiswa" } } } }; // Process data for series const processSeriesData = () => { const jenisPendaftaran = [...new Set(data.map(item => item.jenis_pendaftaran))].sort(); const jumlahData = jenisPendaftaran.map(jenis => { const item = data.find(d => d.jenis_pendaftaran === jenis); return item ? item.jumlah_lulus_tepat_waktu : 0; }); return { series: jumlahData, labels: jenisPendaftaran }; }; const { series, labels } = processSeriesData(); const totalMahasiswa = data.reduce((sum, item) => sum + item.jumlah_lulus_tepat_waktu, 0); if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return ( Jenis Pendaftaran Mahasiswa Lulus Tepat Waktu {selectedYear !== 'all' ? ` Angkatan ${selectedYear}` : ''}
Total Mahasiswa: {totalMahasiswa}
{typeof window !== 'undefined' && ( )}
); }