|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { PieChart, Pie, Cell, Tooltip, Legend } from 'recharts'; |
| 3 | +import axios from 'axios'; |
| 4 | +import styles from './ExpenditureChart.module.css'; |
| 5 | + |
| 6 | +const COLORS = ['#6777EF', '#A0CD61', '#F5CD4B']; |
| 7 | +const CATEGORIES = ['Labor', 'Equipment', 'Materials']; |
| 8 | + |
| 9 | +function normalizeData(data) { |
| 10 | + return CATEGORIES.map(cat => { |
| 11 | + const found = data.find(d => d.category === cat); |
| 12 | + return found || { category: cat, amount: 0 }; |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +const renderCustomLabel = ({ cx, cy, midAngle, outerRadius, percent, name }) => { |
| 17 | + const RADIAN = Math.PI / 180; |
| 18 | + const radius = outerRadius * 0.6; |
| 19 | + const x = cx + radius * Math.cos(-midAngle * RADIAN); |
| 20 | + const y = cy + radius * Math.sin(-midAngle * RADIAN); |
| 21 | + |
| 22 | + return ( |
| 23 | + <text |
| 24 | + x={x} |
| 25 | + y={y} |
| 26 | + fill="white" |
| 27 | + textAnchor="middle" |
| 28 | + dominantBaseline="central" |
| 29 | + fontSize={10} |
| 30 | + fontWeight="600" |
| 31 | + > |
| 32 | + {`${name}: ${(percent * 100).toFixed(1)}%`} |
| 33 | + </text> |
| 34 | + ); |
| 35 | +}; |
| 36 | + |
| 37 | +function ExpenditureChart({ projectId }) { |
| 38 | + const [actual, setActual] = useState([]); |
| 39 | + const [planned, setPlanned] = useState([]); |
| 40 | + const [loading, setLoading] = useState(true); |
| 41 | + const [error, setError] = useState(null); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + const fetchData = async () => { |
| 45 | + if (!projectId) return; |
| 46 | + setLoading(true); |
| 47 | + setError(null); |
| 48 | + try { |
| 49 | + const res = await axios.get( |
| 50 | + `${process.env.REACT_APP_APIENDPOINT}/bm/expenditure/${projectId}/pie`, |
| 51 | + ); |
| 52 | + setActual(res.data.actual); |
| 53 | + setPlanned(res.data.planned); |
| 54 | + } catch (err) { |
| 55 | + setError('Failed to load expenditure data'); |
| 56 | + } finally { |
| 57 | + setLoading(false); |
| 58 | + } |
| 59 | + }; |
| 60 | + fetchData(); |
| 61 | + }, [projectId]); |
| 62 | + |
| 63 | + const renderChart = (data, title) => ( |
| 64 | + <div className={styles.expenditure - chart - card}> |
| 65 | + <h4 className={styles.expenditure - chart - title}>{title}</h4> |
| 66 | + <PieChart width={280} height={280}> |
| 67 | + <Pie |
| 68 | + data={data} |
| 69 | + dataKey="amount" |
| 70 | + nameKey="category" |
| 71 | + cx="50%" |
| 72 | + cy="50%" |
| 73 | + outerRadius={120} |
| 74 | + label={renderCustomLabel} |
| 75 | + labelLine={false} |
| 76 | + stroke="none" |
| 77 | + > |
| 78 | + {data.map((entry, index) => ( |
| 79 | + <Cell key={entry.category || index} fill={COLORS[index % COLORS.length]} /> |
| 80 | + ))} |
| 81 | + </Pie> |
| 82 | + <Tooltip /> |
| 83 | + <Legend layout="horizontal" verticalAlign="bottom" align="center" height={20} /> |
| 84 | + </PieChart> |
| 85 | + </div> |
| 86 | + ); |
| 87 | + |
| 88 | + if (loading) |
| 89 | + return <div className={styles.expenditure - chart - loading}>Loading expenditure data...</div>; |
| 90 | + if (error) return <div className={styles.expenditure - chart - error}>{error}</div>; |
| 91 | + |
| 92 | + return ( |
| 93 | + <div className={styles.expenditure - chart - wrapper}> |
| 94 | + {renderChart(normalizeData(actual), 'Actual Expenditure')} |
| 95 | + {renderChart(normalizeData(planned), 'Planned Expenditure')} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +export default ExpenditureChart; |
0 commit comments