'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"; import { Button } from "@/components/ui/button"; import { ExternalLink } from "lucide-react"; import Link from "next/link"; const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }); interface MasaStudiAktifData { tahun_angkatan: number; rata_rata_masa_studi_aktif_tahun: number; rata_rata_masa_studi_lulus_tahun: number; } interface Props { selectedYear: string; } interface MasaStudiLulusChartProps extends Props { height?: string; showDetailButton?: boolean; } export default function MasaStudiLulusChart({ selectedYear, height = "h-[300px] sm:h-[350px] md:h-[300px]", showDetailButton = true }: MasaStudiLulusChartProps) { const { theme } = useTheme(); const [mounted, setMounted] = useState(false); const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { setMounted(true); }, []); useEffect(() => { const fetchData = async () => { try { setLoading(true); setError(null); let url = '/api/mahasiswa/masa-studi-aktif'; if (selectedYear && selectedYear !== 'all') { url += `?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: MasaStudiAktifData, b: MasaStudiAktifData) => 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]); // Chart options and series const chartOptions: ApexOptions = { chart: { type: selectedYear === 'all' ? 'line' : 'bar', toolbar: { show: true, tools: { download: true, selection: true, zoom: true, zoomin: true, zoomout: true, pan: true, reset: true } }, background: theme === 'dark' ? '#0F172B' : '#fff', zoom: { enabled: true, type: 'x', autoScaleYaxis: true } }, plotOptions: { bar: { horizontal: false, columnWidth: '55%', borderRadius: 4, }, }, stroke: { curve: 'straight', width: 3, lineCap: 'round' }, markers: { size: 5, strokeWidth: 2, strokeColors: theme === 'dark' ? ['#fff'] : ['#3B82F6'], colors: ['#3B82F6'], hover: { size: 7 } }, dataLabels: { enabled: true, formatter: function (val: number) { return val.toFixed(1); }, style: { fontSize: '14px', fontWeight: 'bold', colors: [theme === 'dark' ? '#fff' : '#000'] }, background: { enabled: false }, offsetY: -10 }, xaxis: { categories: data.map(item => item.tahun_angkatan.toString()), 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: 'Rata-rata Masa Studi (tahun)', style: { fontSize: '14px', fontWeight: 'bold', color: theme === 'dark' ? '#fff' : '#000' } }, min: 0, max: 8, labels: { style: { fontSize: '12px', colors: theme === 'dark' ? '#fff' : '#000' }, formatter: function (val: number) { return val.toFixed(1); } }, axisBorder: { show: true, color: theme === 'dark' ? '#374151' : '#E5E7EB' } }, grid: { borderColor: theme === 'dark' ? '#374151' : '#E5E7EB', strokeDashArray: 4, padding: { top: 20, right: 0, bottom: 0, left: 0 } }, colors: ['#3B82F6'], tooltip: { theme: theme === 'dark' ? 'dark' : 'light', y: { formatter: function (val: number) { return val.toFixed(1) + ' tahun'; } }, marker: { show: true } }, legend: { show: true, position: 'top', horizontalAlign: 'right', labels: { colors: theme === 'dark' ? '#fff' : '#000' } } }; const series = [{ name: 'Rata-rata Masa Studi Lulus (tahun)', data: data.map(item => item.rata_rata_masa_studi_lulus_tahun) }]; if (!mounted) { return null; } if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return (
Rata-rata Masa Studi Mahasiswa Lulus Berdasarkan Tahun Angkatan {selectedYear !== 'all' ? ` Angkatan ${selectedYear}` : ''} {showDetailButton && ( )}
{typeof window !== 'undefined' && ( )}
); }