-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathProjectStatusDonutChart.jsx
More file actions
167 lines (143 loc) · 5.44 KB
/
ProjectStatusDonutChart.jsx
File metadata and controls
167 lines (143 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer, Label } from 'recharts';
import axios from 'axios';
import styles from './ProjectStatusDonutChart.module.css';
const COLORS = ['#B39DDB', '#80DEEA', '#FFABAB'];
export default function ProjectStatusDonutChart() {
const darkMode = useSelector(state => state.theme?.darkMode || false);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [statusData, setStatusData] = useState(null);
const [startDate, setStartDate] = useState('');
const [endDate, setEndDate] = useState('');
const fetchStatus = async () => {
try {
setLoading(true);
setError(null);
const query = [];
if (startDate) query.push(`startDate=${startDate}`);
if (endDate) query.push(`endDate=${endDate}`);
const queryString = query.length ? `?${query.join('&')}` : '';
const token = localStorage.getItem('token');
const res = await axios.get(`http://localhost:4500/api/projects/status${queryString}`, {
headers: { Authorization: token },
});
setStatusData(res.data);
} catch (err) {
setError('Unable to load project status.');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchStatus();
}, []);
if (loading) return <p>Loading project status...</p>;
if (error) return <p>{error}</p>;
if (!statusData) return <p>No data available.</p>;
const pieData = [
{ name: 'Active Projects', value: statusData.activeProjects },
{ name: 'Completed Projects', value: statusData.completedProjects },
{ name: 'Delayed Projects', value: statusData.delayedProjects },
];
if (pieData.every(item => item.value === 0)) {
return (
<div className={`${styles.container} ${darkMode ? styles.darkContainer : ''}`}>
<h2 className={styles.title}>PROJECT STATUS</h2>
<p className={styles.noDataMessage}>No project status data available.</p>
</div>
);
}
const today = new Date().toLocaleDateString('en-US', {
weekday: 'short',
month: 'short',
day: 'numeric',
year: 'numeric',
});
const allZero =
!statusData.activeProjects && !statusData.completedProjects && !statusData.delayedProjects;
return (
<div className={`${styles.container} ${darkMode ? styles.darkContainer : ''}`}>
<h2 className={styles.title}>PROJECT STATUS</h2>
<div className={styles.filterRow}>
<input
type="date"
value={startDate}
max={endDate || undefined}
onChange={e => setStartDate(e.target.value)}
/>
<input
type="date"
value={endDate}
min={startDate || undefined}
onChange={e => setEndDate(e.target.value)}
/>
<button type="button" onClick={fetchStatus} className={styles.applyBtn}>
Apply
</button>
</div>
<div className={styles.chartWrapper}>
{!allZero && (
<ResponsiveContainer width="100%" height={350}>
<PieChart margin={{ top: 10, right: 10, bottom: 40, left: 10 }}>
<Pie
data={pieData}
cx="50%"
cy="50%"
innerRadius="60%"
outerRadius="85%"
paddingAngle={3}
dataKey="value"
>
{pieData.map((entry, index) => (
<Cell key={entry.name} fill={COLORS[index % COLORS.length]} />
))}
<Label
position="center"
content={({ viewBox }) => {
const { cx, cy } = viewBox;
return (
<text x={cx} y={cy} textAnchor="middle" dominantBaseline="central">
<tspan x={cx} dy="-10" className={styles.centerLabel}>
Total Projects
</tspan>
<tspan x={cx} dy="24" className={styles.centerValue}>
{statusData.totalProjects}
</tspan>
</text>
);
}}
/>
</Pie>
<Tooltip
contentStyle={{
backgroundColor: darkMode ? '#2a3f5f' : '#ffffff',
borderRadius: '8px',
border: darkMode ? '1px solid #3a506b' : '1px solid #e5e7eb',
boxShadow: '0 4px 6px rgba(0, 0, 0, 0.3)',
color: darkMode ? '#ffffff' : '#111827',
}}
itemStyle={{
color: darkMode ? '#ffffff' : '#111827',
fontWeight: '600',
textTransform: 'capitalize',
}}
/>
<Legend verticalAlign="bottom" align="center" />
</PieChart>
</ResponsiveContainer>
)}
<div className={styles.summaryBox}>
<h3>{today}</h3>
<p className={styles.label}>ACTIVE PROJECTS</p>
<span className={styles.value}>{statusData.activeProjects}</span>
<p className={styles.label}>COMPLETED PROJECTS</p>
<span className={styles.value}>{statusData.completedProjects}</span>
<p className={styles.label}>DELAYED PROJECTS</p>
<span className={styles.value}>{statusData.delayedProjects}</span>
</div>
</div>
</div>
);
}