'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 JenisPendaftaranStatusData { jenis_pendaftaran: string; tahun_angkatan: number; status_kuliah: string; total_mahasiswa: number; } interface Props { selectedYear: string; selectedStatus: string; } export default function JenisPendaftaranStatusChart({ selectedYear, selectedStatus }: 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); console.log('Fetching data with params:', { selectedYear, selectedStatus }); const response = await fetch( `/api/mahasiswa/jenis-pendaftaran-status?tahun_angkatan=${selectedYear}&status_kuliah=${selectedStatus}` ); if (!response.ok) { throw new Error('Failed to fetch data'); } const result = await response.json(); console.log('Received data:', result); // Sort data by tahun_angkatan const sortedData = result.sort((a: JenisPendaftaranStatusData, b: JenisPendaftaranStatusData) => Number(a.tahun_angkatan) - Number(b.tahun_angkatan) ); setData(sortedData); } catch (err) { console.error('Error in fetchData:', err); setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; fetchData(); }, [selectedYear, selectedStatus]); // Log data changes useEffect(() => { console.log('Current data state:', data); }, [data]); // Get unique years and sort them const years = [...new Set(data.map(item => item.tahun_angkatan))].sort((a, b) => Number(a) - Number(b)); console.log('Sorted years:', years); // Get unique jenis pendaftaran const jenisPendaftaran = [...new Set(data.map(item => item.jenis_pendaftaran))]; console.log('Jenis pendaftaran:', jenisPendaftaran); const chartOptions: ApexOptions = { chart: { type: 'bar', stacked: false, toolbar: { show: true, }, background: theme === 'dark' ? '#0F172B' : '#fff', }, plotOptions: { bar: { horizontal: false, columnWidth: '55%', }, }, 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: years, title: { text: 'Tahun Angkatan', style: { fontSize: '14px', fontWeight: 'bold', color: theme === 'dark' ? '#fff' : '#000' } }, labels: { style: { fontSize: '12px', colors: theme === 'dark' ? '#fff' : '#000' } } }, yaxis: { title: { text: 'Jumlah Mahasiswa', style: { fontSize: '14px', fontWeight: 'bold', color: theme === 'dark' ? '#fff' : '#000' } }, labels: { style: { fontSize: '12px', colors: theme === 'dark' ? '#fff' : '#000' } } }, fill: { opacity: 1, }, legend: { position: 'top', fontSize: '14px', markers: { size: 12, }, itemMargin: { horizontal: 10, }, labels: { colors: theme === 'dark' ? '#fff' : '#000' } }, colors: ['#008FFB', '#00E396', '#FEB019', '#FF4560', '#775DD0', '#8B5CF6', '#EC4899', '#06B6D4', '#F97316'], tooltip: { theme: theme === 'dark' ? 'dark' : 'light', y: { formatter: function (val: number) { return val + " mahasiswa"; } } } }; // Process data for series const processSeriesData = () => { return jenisPendaftaran.map(jenis => { const seriesData = new Array(years.length).fill(0); data.forEach(item => { if (item.jenis_pendaftaran === jenis) { const yearIndex = years.indexOf(item.tahun_angkatan); if (yearIndex !== -1) { seriesData[yearIndex] = item.total_mahasiswa; } } }); return { name: jenis, data: seriesData }; }); }; const series = processSeriesData(); console.log('Processed series data:', series); // Calculate the maximum value from all series for y-axis padding const maxValue = Math.max( ...series.flatMap(s => s.data) ); // Add 20% padding to the maximum value const yAxisMax = Math.ceil(maxValue * 1.2); // Update chart options with y-axis max const updatedChartOptions: ApexOptions = { ...chartOptions, yaxis: { ...chartOptions.yaxis, max: yAxisMax } }; if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return ( Jenis Pendaftaran Mahasiswa {selectedStatus} {selectedYear !== 'all' ? ` Angkatan ${selectedYear}` : ''}
{typeof window !== 'undefined' && ( )}
); }