import ApexChart from 'react-apexcharts'; import { ApexOptions } from 'apexcharts'; import { useEffect, useState } from 'react'; import { fCurrency } from '@/utils/FormatNumber'; interface Series { name: string; data: any[]; } const Chart = ({ title, toolbar, chartType, chartLegend, subtitle, number, count, chartData = [] }: any) => { const [series, setSeries] = useState([]); const [categories, setCategories] = useState([]); let legendOpt = null; if (chartLegend == 'true') { legendOpt = true; } else { legendOpt = false; } const options: ApexOptions = { chart: { type: 'area', toolbar: { show: false } }, plotOptions: { bar: { horizontal: false, columnWidth: '50%' } }, dataLabels: { enabled: true, offsetY: -10, offsetX: chartType === 'bar' ? 1.5 : 0, formatter: (value: any) => { if (count == 'sum') { return fCurrency(value); } else { return value; } } }, markers: { size: 0, shape: 'circle' }, xaxis: { categories: categories, labels: { style: { colors: 'var(--tw-gray-500)', fontSize: '12px' } } }, yaxis: { labels: { style: { colors: 'var(--tw-gray-500)', fontSize: '12px' }, formatter: (value: any) => { if (count == 'sum') { return fCurrency(value); } else { return value; } } } }, grid: { borderColor: 'var(--tw-gray-200)', strokeDashArray: 5, padding: { top: 0, right: 0, bottom: 20, left: 0 } }, tooltip: { enabled: true, shared: true, intersect: false, y: { formatter: (value: any) => { if (count == 'sum') { return fCurrency(value); } else { return value; } } } }, stroke: { show: true, curve: 'smooth', lineCap: 'butt', colors: undefined, width: 3, dashArray: 0 }, legend: { show: legendOpt, position: 'right', floating: false } }; useEffect(() => { if (chartData && chartData.length !== 0) { const categories = chartData.map((item: any) => item.month); const statusNames = Object.keys(chartData[0]).filter((key) => key !== 'month'); const series = statusNames.map((status) => { const cleanName = status .replace('_count', '') .replace(/_/g, ' ') .replace(/(^|\s)\S/g, (match) => match.toUpperCase()); return { name: cleanName, data: chartData.map((item: any) => item[status]) }; }); setCategories(categories); setSeries(series); } }, [chartData]); return (

{title}

{toolbar &&
{toolbar}
}
{subtitle} {number}
); }; export { Chart };