Files
hats-app/app/(dashboard)/components/PieChart.tsx
T
Ichsan Kurniawan a160eccddf fixing typescript error during production build
Adjust chart components (Overview, PieChart) for sizing and safer typings/casting; add v1.2.0 release note. Remove several obsolete dummy/old task pages and the useDummyTaskData hook. Also include minor fixes to FormClient and DonutChart/performance pages.
2026-04-23 21:36:42 +07:00

182 lines
7.2 KiB
TypeScript

'use client'
import { FC, useState } from 'react';
import { type ApexOptions } from 'apexcharts';
import { useTheme } from 'next-themes';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import ViewAvatar from '@/components/ViewAvatar';
import dynamic from 'next/dynamic';
import LazyLoading from '@/components/loader/LazyLoading';
const ReactApexChart = dynamic(() => import('react-apexcharts'), {
loading: () => <LazyLoading />,
ssr: false
});
type Props = {
data: any
};
const PieChart: FC<Props> = ({ data }) => {
const [selectedWorkingStatus, setSelectedWorkingStatus] = useState('')
// const { windowWidth } = useMobileView()
const { theme, systemTheme } = useTheme()
const options: ApexOptions = {
chart: {
width: 1000,
height: 1000,
// width: windowWidth > 800 ? 1000 : 400,
// height: windowWidth > 800 ? 1000 : 400,
events: {
click(e, chart, options) {
const { selectedDataPoints, labels } = options?.globals
if (selectedDataPoints.length > 0) {
const workingStatus = labels[selectedDataPoints[0]]
setSelectedWorkingStatus(workingStatus?.toUpperCase())
}
if (selectedDataPoints.length > 0) {
chart?.toggleDataPointSelection(selectedDataPoints[0])
}
},
mounted: (chart) => {
(chart as any)?.windowResizeHandler();
}
},
},
plotOptions: {
pie: {
expandOnClick: true,
donut: {
size: '55%',
labels: {
show: true,
name: {
show: true,
color: theme === 'dark' ? '#fff' : theme === 'system' && systemTheme === 'dark' ? '#fff' : '#000',
fontSize: '20px',
fontWeight: 'bold'
},
value: {
show: true,
color: theme === 'dark' ? '#fff' : theme === 'system' && systemTheme === 'dark' ? '#fff' : '#000',
fontSize: '32px',
fontWeight: 'bold'
},
total: {
show: true,
showAlways: true,
color: theme === 'dark' ? '#fff' : theme === 'system' && systemTheme === 'dark' ? '#fff' : '#000',
label: 'Total',
formatter: function (w) {
return w.globals.seriesTotals.reduce((a: number, b: number) => {
return a + b
}, 0)
},
},
},
},
}
},
legend: {
show: true,
position: 'right',
fontSize: '16px',
// position: windowWidth > 800 ? 'right' : 'bottom',
// fontSize: windowWidth > 800 ? '16px' : '12px',
labels: {
colors: theme === 'dark' ? '#fff' : theme === 'light' ? '#000' : '#a1a1aa',
// useSeriesColors: true
},
},
stroke: {
width: 0
},
dataLabels: {
enabled: true,
formatter: function (val: any, opts) {
// console.log(opts)
return opts?.w.globals.labels[opts?.seriesIndex!] + ': ' + opts?.w.globals.series[opts.seriesIndex]
// return opts.w.globals.series[opts.seriesIndex]
},
},
fill: {
type: 'gradient',
},
labels: ['WFO', 'WFS', 'WFH', 'Cuti', 'Izin', 'Sakit', 'Belum Absen'],
colors: ['#3b82f6', '#10b981', '#db2777', '#8b5cf6', '#06b6d4', '#f59e0b', '#84cc16'],
responsive: [{
breakpoint: 800,
options: {
chart: {
width: 400
},
legend: {
fontSize: '12px',
position: 'bottom'
}
}
}]
}
const series = [data.total_wfo, data.total_wfs, data.total_wfh, data.total_ct01, data.total_iz01, data.total_skt, data.total_belum_absen]
const handleGetListEmployee = () => {
if (!selectedWorkingStatus) return []
if (selectedWorkingStatus === 'BELUM ABSEN') {
return data.list.filter((item: any) => item.status_presence?.includes('Belum'))
} else {
return data.list.filter((item: any) => item.working_status?.toUpperCase().includes(selectedWorkingStatus))
}
}
return (
<>
<div className='h-75 sm:h-87.5 flex items-center justify-center'>
<ReactApexChart
type='donut'
options={options}
series={series}
// width={windowWidth > 800 ? 525 : 400}
width={525}
/>
</div>
{selectedWorkingStatus &&
<Dialog open={selectedWorkingStatus !== ''} onOpenChange={() => setSelectedWorkingStatus('')}>
<DialogContent className='overflow-y-auto max-h-[90vh]'>
<DialogHeader>
<DialogTitle>List Employee</DialogTitle>
<DialogDescription>
The following is a list of employees who are <span className='font-bold'>{selectedWorkingStatus}</span>.
</DialogDescription>
</DialogHeader>
<div className='space-y-5 mt-2'>
{handleGetListEmployee().length > 0 ? handleGetListEmployee().map((item: any, key: number) =>
<div className="flex items-center col-span-4" key={key}>
<ViewAvatar avatar={item.avatar} />
<div className="ml-4 space-y-0.5 max-w-[70%] 2xl:max-w-[80%]">
<p className="text-xs sm:text-sm font-medium leading-none">{item.fullname}</p>
<p className="text-xs md:text-sm text-muted-foreground">
{item.position_name || '--'}
</p>
</div>
</div>
) :
<span>No result</span>
}
</div>
<DialogFooter>
<Button variant='secondary' onClick={() => setSelectedWorkingStatus('')}>Close</Button>
</DialogFooter>
</DialogContent>
</Dialog>
}
</>
);
}
export default PieChart;