188 lines
5.0 KiB
TypeScript
188 lines
5.0 KiB
TypeScript
'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 JenisPendaftaranPrestasiData {
|
|
tahun_angkatan: number;
|
|
jenis_pendaftaran: string;
|
|
jenis_pendaftaran_mahasiswa_prestasi: number;
|
|
}
|
|
|
|
interface Props {
|
|
selectedYear: string;
|
|
selectedJenisPrestasi: string;
|
|
}
|
|
|
|
export default function JenisPendaftaranPrestasiPieChart({ selectedYear, selectedJenisPrestasi }: Props) {
|
|
const { theme } = useTheme();
|
|
const [data, setData] = useState<JenisPendaftaranPrestasiData[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchData = async () => {
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
const url = `/api/mahasiswa/jenis-pendaftaran-prestasi?tahunAngkatan=${selectedYear}&jenisPrestasi=${selectedJenisPrestasi}`;
|
|
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, selectedJenisPrestasi]);
|
|
|
|
const chartOptions: ApexOptions = {
|
|
chart: {
|
|
type: 'pie',
|
|
background: theme === 'dark' ? '#0F172B' : '#fff',
|
|
toolbar: {
|
|
show: true,
|
|
tools: {
|
|
download: true,
|
|
selection: true,
|
|
zoom: true,
|
|
zoomin: true,
|
|
zoomout: true,
|
|
pan: true,
|
|
reset: true
|
|
}
|
|
}
|
|
},
|
|
labels: [],
|
|
colors: ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#06B6D4'],
|
|
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 filtered = data.filter(item => String(item.tahun_angkatan) === String(selectedYear));
|
|
const jenisPendaftaran = [...new Set(filtered.map(item => item.jenis_pendaftaran))].sort();
|
|
const jumlahData = jenisPendaftaran.map(jenis => {
|
|
const item = filtered.find(d => d.jenis_pendaftaran === jenis);
|
|
return item ? item.jenis_pendaftaran_mahasiswa_prestasi : 0;
|
|
});
|
|
|
|
return {
|
|
series: jumlahData,
|
|
labels: jenisPendaftaran
|
|
};
|
|
};
|
|
|
|
const { series, labels } = processSeriesData();
|
|
|
|
if (loading) {
|
|
return (
|
|
<Card className="bg-white dark:bg-slate-900 shadow-lg">
|
|
<CardHeader>
|
|
<CardTitle className="text-xl font-bold dark:text-white">
|
|
Loading...
|
|
</CardTitle>
|
|
</CardHeader>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<Card className="bg-white dark:bg-slate-900 shadow-lg">
|
|
<CardHeader>
|
|
<CardTitle className="text-xl font-bold text-red-500">
|
|
Error: {error}
|
|
</CardTitle>
|
|
</CardHeader>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (data.length === 0) {
|
|
return (
|
|
<Card className="bg-white dark:bg-slate-900 shadow-lg">
|
|
<CardHeader>
|
|
<CardTitle className="text-xl font-bold dark:text-white">
|
|
Tidak ada data yang tersedia
|
|
</CardTitle>
|
|
</CardHeader>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Card className="bg-white dark:bg-slate-900 shadow-lg">
|
|
<CardHeader>
|
|
<CardTitle className="text-xl font-bold dark:text-white">
|
|
Jenis Pendaftaran Mahasiswa Berprestasi {selectedJenisPrestasi} Angkatan {selectedYear}
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="h-[350px] w-full max-w-5xl mx-auto">
|
|
{typeof window !== 'undefined' && (
|
|
<Chart
|
|
options={{...chartOptions, labels}}
|
|
series={series}
|
|
type="pie"
|
|
height="100%"
|
|
width="90%"
|
|
/>
|
|
)}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|