|
| 1 | +/* eslint-disable jsx-a11y/label-has-associated-control */ |
| 2 | +/* eslint-disable object-shorthand */ |
| 3 | +/* eslint-disable func-names */ |
| 4 | +/* eslint-disable prettier/prettier */ |
| 5 | +/* eslint-disable react/function-component-definition */ |
| 6 | +import { useEffect, useState } from 'react'; |
| 7 | +import { Pie } from 'react-chartjs-2'; |
| 8 | +import { useDispatch, useSelector } from 'react-redux'; |
| 9 | +import ChartDataLabels from 'chartjs-plugin-datalabels'; |
| 10 | +import { Chart as ChartJS } from 'chart.js'; |
| 11 | +import { fetchOptStatusBreakdown } from '../../actions/optStatusBreakdownAction'; |
| 12 | +import { roleOptions } from './filter'; |
| 13 | +import 'chart.js/auto'; |
| 14 | +import './OptStatusPieChart.css'; |
| 15 | + |
| 16 | +ChartJS.register(ChartDataLabels); |
| 17 | + |
| 18 | +const COLORS = { |
| 19 | + 'OPT started': '#f44336', |
| 20 | + 'CPT not eligible': '#f4e34cfc', |
| 21 | + 'OPT not yet started': '#2196f3', |
| 22 | + Citizen: '#4caf50', |
| 23 | + 'N/A': '#ff9800', |
| 24 | +}; |
| 25 | + |
| 26 | +const OptStatusPieChart = () => { |
| 27 | + const dispatch = useDispatch(); |
| 28 | + const { optStatusBreakdown } = useSelector(state => state.optStatusBreakdown); |
| 29 | + // const { optStatusBreakdown = [] } = useSelector(state => state.optStatus); |
| 30 | + const [startDate, setStartDate] = useState(''); |
| 31 | + const [endDate, setEndDate] = useState(''); |
| 32 | + const [role, setRole] = useState(''); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + dispatch(fetchOptStatusBreakdown(startDate, endDate, role)); |
| 36 | + }, [startDate, endDate, role, dispatch]); |
| 37 | + |
| 38 | + const labels = optStatusBreakdown.map(d => d.optStatus); |
| 39 | + const dataCounts = optStatusBreakdown.map(d => d.count); |
| 40 | + const total = dataCounts.reduce((sum, value) => sum + value, 0); |
| 41 | + const backgroundColors = labels.map(label => COLORS[label] || '#ccc'); |
| 42 | + |
| 43 | + const chartData = { |
| 44 | + labels, |
| 45 | + datasets: [ |
| 46 | + { |
| 47 | + data: dataCounts, |
| 48 | + backgroundColor: backgroundColors, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }; |
| 52 | + |
| 53 | + const options = { |
| 54 | + responsive: true, |
| 55 | + maintainAspectRatio: false, |
| 56 | + plugins: { |
| 57 | + legend: { display: false }, |
| 58 | + datalabels: { |
| 59 | + color: '#000', |
| 60 | + font: { weight: 'bold' }, |
| 61 | + // eslint-disable-next-line no-unused-vars |
| 62 | + formatter: (value, context) => { |
| 63 | + const percent = ((value / total) * 100).toFixed(1); |
| 64 | + return `${percent}%\n(${value})`; |
| 65 | + }, |
| 66 | + }, |
| 67 | + tooltip: { |
| 68 | + callbacks: { |
| 69 | + label: function(context) { |
| 70 | + const count = context.raw; |
| 71 | + const percent = ((count / total) * 100).toFixed(1); |
| 72 | + const { label } = context; |
| 73 | + return `${label}: ${percent}% (${count})`; |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="opt-status-container"> |
| 82 | + <h2 className="opt-status-title">Breakdown by OPT Status</h2> |
| 83 | + |
| 84 | + <div className="chart-filter-layout"> |
| 85 | + <div className="pie-chart-wrapper"> |
| 86 | + <Pie data={chartData} options={options} /> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="filters"> |
| 90 | + <label> |
| 91 | + <span> |
| 92 | + <strong>Dates</strong> |
| 93 | + </span> |
| 94 | + <div className="date-inputs"> |
| 95 | + <input type="date" value={startDate} onChange={e => setStartDate(e.target.value)} /> |
| 96 | + <input type="date" value={endDate} onChange={e => setEndDate(e.target.value)} /> |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + className="reset-btn" |
| 100 | + onClick={() => { |
| 101 | + setStartDate(''); |
| 102 | + setEndDate(''); |
| 103 | + }} |
| 104 | + > |
| 105 | + Reset |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + </label> |
| 109 | + |
| 110 | + <label> |
| 111 | + <span> |
| 112 | + <strong>Role</strong> |
| 113 | + </span> |
| 114 | + <select value={role} onChange={e => setRole(e.target.value)}> |
| 115 | + {roleOptions.map(option => ( |
| 116 | + <option key={option.value} value={option.value}> |
| 117 | + {option.label} |
| 118 | + </option> |
| 119 | + ))} |
| 120 | + </select> |
| 121 | + </label> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default OptStatusPieChart; |
0 commit comments