import React, { useState, useEffect } from "react"; import Chart from "react-apexcharts"; import styles from "./BarChart.module.css"; // Import the CSS module const PeriodCharts = ({ type, graphFilter, aggregatedCurrentReports, aggregatedPreviousReports, colors }) => { const [selectedIndex, setSelectedIndex] = useState(-1); useEffect(() => { setSelectedIndex(-1); }, [aggregatedCurrentReports, aggregatedPreviousReports]); const monthly = ["1 - 7", "8 - 14", "15 - 21", "22 - 28", "29 - 31"]; const yearly = ["Kuartal 1", "Kuartal 2", "Kuartal 3", "Kuartal 4"]; const cat = type == 'monthly' ? monthly : yearly; // Map the data for the current reports let currentIncomeData, currentOutcomeData, currentTransactionData, previousIncomeData, previousOutcomeData, previousTransactionData = null; if (aggregatedCurrentReports) { currentIncomeData = aggregatedCurrentReports.map((report) => report.income); currentOutcomeData = aggregatedCurrentReports.map((report) => report.outcome); currentTransactionData = aggregatedCurrentReports.map((report) => report.transactions); if (type == 'monthly' && currentIncomeData.length === 4) { currentIncomeData.push(null); } if (type == 'monthly' && currentOutcomeData.length === 4) { currentOutcomeData.push(null); } if (type == 'monthly' && currentTransactionData.length === 4) { currentTransactionData.push(null); } } if (aggregatedPreviousReports) { // Map the data for the previous reports previousIncomeData = aggregatedPreviousReports.map((report) => report.income); previousOutcomeData = aggregatedPreviousReports.map((report) => report.outcome); previousTransactionData = aggregatedPreviousReports.map((report) => report.transactions); if (type == 'monthly' && previousIncomeData.length === 4) { previousIncomeData.push(null); } if (type == 'monthly' && previousOutcomeData.length === 4) { previousOutcomeData.push(null); } if (type == 'monthly' && previousTransactionData.length === 4) { previousTransactionData.push(null); } } let globalMax = null; if (aggregatedCurrentReports || aggregatedPreviousReports) { // Find the global maximum for the y-axis globalMax = Math.max( ...(graphFilter === 'income' ? [...currentIncomeData, ...previousIncomeData] : graphFilter === 'outcome' ? [...currentOutcomeData, ...previousOutcomeData] : [...currentTransactionData, ...previousTransactionData]) ); } return (
{aggregatedPreviousReports && (
selectedIndex === -1 ? setSelectedIndex(0) : setSelectedIndex(-1) } style={{ color: 'black', position: 'relative' }} >
{type == 'monthly' ? 'bulan lalu' : 'tahun lalu'}
selectedIndex === 0 ? setSelectedIndex(-1) : setSelectedIndex(1) }>
{type == 'monthly' ? 'bulan ini' : 'tahun ini'}
)} {aggregatedCurrentReports && (
selectedIndex === 1 ? setSelectedIndex(-1) : setSelectedIndex(0) }>
{type == 'monthly' ? 'bulan lalu' : 'tahun lalu'}
selectedIndex === -1 ? setSelectedIndex(1) : setSelectedIndex(-1) } style={{ color: 'black', position: 'relative' }} >
{type == 'monthly' ? 'bulan ini' : 'tahun ini'}
)}
); }; export default PeriodCharts;