262 lines
6.6 KiB
TypeScript
262 lines
6.6 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 JenisPendaftaranLulusData {
|
|
tahun_angkatan: number;
|
|
jenis_pendaftaran: string;
|
|
jumlah_lulus_tepat_waktu: number;
|
|
}
|
|
|
|
interface Props {
|
|
selectedYear: string;
|
|
}
|
|
|
|
export default function JenisPendaftaranLulusChart({ selectedYear }: Props) {
|
|
const { theme } = useTheme();
|
|
const [data, setData] = useState<JenisPendaftaranLulusData[]>([]);
|
|
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-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();
|
|
|
|
if (!Array.isArray(result)) {
|
|
throw new Error('Invalid data format received from server');
|
|
}
|
|
|
|
// Sort data by tahun_angkatan
|
|
const sortedData = result.sort((a: JenisPendaftaranLulusData, b: JenisPendaftaranLulusData) =>
|
|
a.tahun_angkatan - b.tahun_angkatan
|
|
);
|
|
|
|
setData(sortedData);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'An error occurred while fetching data');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchData();
|
|
}, [selectedYear]);
|
|
|
|
// Process data for series
|
|
const processSeriesData = () => {
|
|
if (!data.length) return [];
|
|
|
|
const years = [...new Set(data.map(item => item.tahun_angkatan))].sort();
|
|
const pendaftaranTypes = [...new Set(data.map(item => item.jenis_pendaftaran))];
|
|
|
|
return pendaftaranTypes.map(type => ({
|
|
name: type,
|
|
data: years.map(year => {
|
|
const item = data.find(d => d.tahun_angkatan === year && d.jenis_pendaftaran === type);
|
|
return item ? item.jumlah_lulus_tepat_waktu : 0;
|
|
})
|
|
}));
|
|
};
|
|
|
|
const chartOptions: ApexOptions = {
|
|
chart: {
|
|
type: 'bar',
|
|
stacked: false,
|
|
toolbar: {
|
|
show: true,
|
|
tools: {
|
|
download: true,
|
|
selection: true,
|
|
zoom: true,
|
|
zoomin: true,
|
|
zoomout: true,
|
|
pan: true,
|
|
reset: true
|
|
}
|
|
},
|
|
background: theme === 'dark' ? '#0F172B' : '#fff',
|
|
},
|
|
plotOptions: {
|
|
bar: {
|
|
horizontal: false,
|
|
columnWidth: '55%',
|
|
borderRadius: 1,
|
|
},
|
|
},
|
|
dataLabels: {
|
|
enabled: true,
|
|
formatter: function (val: number) {
|
|
return val.toString();
|
|
},
|
|
style: {
|
|
fontSize: '12px',
|
|
colors: [theme === 'dark' ? '#fff' : '#000']
|
|
}
|
|
},
|
|
stroke: {
|
|
show: true,
|
|
width: 2,
|
|
colors: ['transparent']
|
|
},
|
|
xaxis: {
|
|
categories: [...new Set(data.map(item => item.tahun_angkatan))].sort(),
|
|
title: {
|
|
text: 'Tahun Angkatan',
|
|
style: {
|
|
fontSize: '14px',
|
|
fontWeight: 'bold',
|
|
color: theme === 'dark' ? '#fff' : '#000'
|
|
}
|
|
},
|
|
labels: {
|
|
style: {
|
|
fontSize: '12px',
|
|
colors: theme === 'dark' ? '#fff' : '#000'
|
|
}
|
|
},
|
|
axisBorder: {
|
|
show: true,
|
|
color: theme === 'dark' ? '#374151' : '#E5E7EB'
|
|
},
|
|
axisTicks: {
|
|
show: true,
|
|
color: theme === 'dark' ? '#374151' : '#E5E7EB'
|
|
}
|
|
},
|
|
yaxis: {
|
|
title: {
|
|
text: 'Jumlah Mahasiswa',
|
|
style: {
|
|
fontSize: '14px',
|
|
fontWeight: 'bold',
|
|
color: theme === 'dark' ? '#fff' : '#000'
|
|
}
|
|
},
|
|
labels: {
|
|
style: {
|
|
fontSize: '12px',
|
|
colors: theme === 'dark' ? '#fff' : '#000'
|
|
}
|
|
},
|
|
axisBorder: {
|
|
show: true,
|
|
color: theme === 'dark' ? '#374151' : '#E5E7EB'
|
|
}
|
|
},
|
|
fill: {
|
|
opacity: 1
|
|
},
|
|
colors: ['#008FFB', '#00E396', '#FEB019', '#FF4560', '#775DD0'],
|
|
tooltip: {
|
|
theme: theme === 'dark' ? 'dark' : 'light',
|
|
y: {
|
|
formatter: function (val: number) {
|
|
return val + " mahasiswa";
|
|
}
|
|
}
|
|
},
|
|
legend: {
|
|
position: 'top',
|
|
fontSize: '14px',
|
|
markers: {
|
|
size: 12,
|
|
},
|
|
itemMargin: {
|
|
horizontal: 10,
|
|
},
|
|
labels: {
|
|
colors: theme === 'dark' ? '#fff' : '#000'
|
|
}
|
|
},
|
|
grid: {
|
|
borderColor: theme === 'dark' ? '#374151' : '#E5E7EB',
|
|
strokeDashArray: 4,
|
|
padding: {
|
|
top: 20,
|
|
right: 0,
|
|
bottom: 0,
|
|
left: 0
|
|
}
|
|
}
|
|
};
|
|
|
|
const series = 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 Lulus Tepat Waktu
|
|
{selectedYear !== 'all' ? ` Angkatan ${selectedYear}` : ''}
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="h-[300px] sm:h-[350px] md:h-[400px] w-full max-w-5xl mx-auto">
|
|
{typeof window !== 'undefined' && series.length > 0 && (
|
|
<Chart
|
|
options={chartOptions}
|
|
series={series}
|
|
type="bar"
|
|
height="100%"
|
|
width="90%"
|
|
/>
|
|
)}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|