'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 StatusData { tahun_angkatan: string; status_kuliah: string; jumlah: number; } export default function StatusMahasiswaChart() { const { theme, systemTheme } = useTheme(); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [data, setData] = useState([]); const [series, setSeries] = useState([]); const [options, setOptions] = useState({ chart: { type: 'bar', stacked: false, toolbar: { show: true, }, }, plotOptions: { bar: { horizontal: false, columnWidth: '55%', }, }, dataLabels: { enabled: true, formatter: function (val: number) { return val.toString(); }, style: { fontSize: '12px', colors: ['#000'] } }, stroke: { show: true, width: 2, colors: ['transparent'], }, xaxis: { categories: [], title: { text: 'Tahun Angkatan', style: { fontSize: '14px', fontWeight: 'bold', color: '#000' } }, labels: { style: { fontSize: '12px', colors: '#000' } } }, yaxis: { title: { text: 'Jumlah Mahasiswa', style: { fontSize: '14px', fontWeight: 'bold', color: '#000' } }, labels: { style: { fontSize: '12px', colors: '#000' } } }, fill: { opacity: 1, }, legend: { position: 'top', fontSize: '14px', markers: { size: 12, }, itemMargin: { horizontal: 10, }, labels: { colors: '#000' } }, colors: ['#008FFB', '#00E396', '#FEB019', '#EF4444'], tooltip: { theme: 'light', y: { formatter: function (val: number) { return val + " mahasiswa"; } } } }); useEffect(() => { const fetchData = async () => { try { setLoading(true); setError(null); const response = await fetch('/api/mahasiswa/status'); 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'); } setData(result); // Process data to create series const tahunAngkatan = [...new Set(result.map(item => item.tahun_angkatan))].sort(); const statuses = ['Aktif', 'Lulus', 'Cuti', 'DO']; const seriesData = statuses.map(status => ({ name: status, data: tahunAngkatan.map(tahun => { const item = result.find(d => d.tahun_angkatan === tahun && d.status_kuliah === status); return item ? item.jumlah : 0; }), })); setSeries(seriesData); setOptions(prev => ({ ...prev, xaxis: { ...prev.xaxis, categories: tahunAngkatan, }, })); } catch (err) { console.error('Error in fetchData:', err); setError(err instanceof Error ? err.message : 'An error occurred while fetching data'); } finally { setLoading(false); } }; fetchData(); }, []); // Update theme when it changes useEffect(() => { const currentTheme = theme === 'system' ? systemTheme : theme; const textColor = currentTheme === 'dark' ? '#fff' : '#000'; const tooltipTheme = currentTheme === 'dark' ? 'dark' : 'light'; setOptions(prev => ({ ...prev, chart: { ...prev.chart, background: currentTheme === 'dark' ? '#0F172B' : '#fff', }, dataLabels: { ...prev.dataLabels, style: { ...prev.dataLabels?.style, colors: [textColor] } }, xaxis: { ...prev.xaxis, title: { ...prev.xaxis?.title, style: { ...prev.xaxis?.title?.style, color: textColor } }, labels: { ...prev.xaxis?.labels, style: { ...prev.xaxis?.labels?.style, colors: textColor } } }, yaxis: { ...(prev.yaxis as ApexYAxis), title: { ...(prev.yaxis as ApexYAxis)?.title, style: { ...(prev.yaxis as ApexYAxis)?.title?.style, color: textColor } }, labels: { ...(prev.yaxis as ApexYAxis)?.labels, style: { ...(prev.yaxis as ApexYAxis)?.labels?.style, colors: textColor } } }, legend: { ...prev.legend, labels: { ...prev.legend?.labels, colors: textColor } }, tooltip: { ...prev.tooltip, theme: tooltipTheme } })); }, [theme, systemTheme]); if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return ( Status Mahasiswa
{typeof window !== 'undefined' && ( )}
); }