Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit b1e34ea

Browse files
committed
modified: src/pages/ngo/StatPage/LineChart.jsx
1 parent b55c9d8 commit b1e34ea

File tree

1 file changed

+25
-50
lines changed

1 file changed

+25
-50
lines changed

src/pages/ngo/StatPage/LineChart.jsx

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import {
77
PointElement,
88
Title,
99
Tooltip,
10-
} from 'chart.js'
11-
import {
12-
Line,
13-
} from 'react-chartjs-2';
10+
} from 'chart.js';
11+
import { Line } from 'react-chartjs-2';
1412

1513
ChartJS.register(
1614
LineElement,
@@ -24,54 +22,31 @@ ChartJS.register(
2422
const LineChart = ({ data }) => {
2523
const [view, setView] = useState('day');
2624

27-
// Null checking for data prop
28-
if (!data || !Array.isArray(data) || data.length === 0) {
29-
console.error("Invalid or empty data provided to LineChart component");
30-
return <div>No data available for the chart.</div>;
31-
}
32-
3325
const convertDataForLineChart = useMemo(() => {
3426
const countsPerDay = {};
27+
const validDates = [];
3528

36-
// Ensure data is an array before forEach
37-
if (Array.isArray(data)) {
38-
data.forEach((report) => {
39-
if (report && typeof report === 'object' && 'reported_time' in report) {
40-
try {
41-
const reportDate = new Date(report.reported_time);
42-
if (!isNaN(reportDate.getTime())) {
43-
const reportedDate = reportDate.toLocaleDateString('en-GB', {
44-
day: '2-digit',
45-
month: '2-digit'
46-
});
47-
countsPerDay[reportedDate] = (countsPerDay[reportedDate] || 0) + 1;
48-
} else {
49-
console.warn("Invalid date encountered:", report.reported_time);
50-
}
51-
} catch (error) {
52-
console.warn("Error processing report date:", error);
53-
}
54-
} else {
55-
console.warn("Encountered a report with missing or invalid reported_time");
56-
}
57-
});
58-
} else {
59-
console.error("Data is not an array in convertDataForLineChart");
60-
return { dates: [], counts: [] };
61-
}
62-
63-
const dates = [];
64-
const validDates = Array.isArray(data) ? data
65-
.filter(report => report && typeof report === 'object' && 'reported_time' in report)
66-
.map(report => {
29+
data.forEach((report, index) => {
30+
if (report && typeof report === 'object' && 'reported_time' in report) {
6731
try {
68-
const date = new Date(report.reported_time);
69-
return isNaN(date.getTime()) ? null : date;
70-
} catch {
71-
return null;
32+
const reportDate = new Date(report.reported_time);
33+
if (!isNaN(reportDate.getTime())) {
34+
const reportedDate = reportDate.toLocaleDateString('en-GB', {
35+
day: '2-digit',
36+
month: '2-digit'
37+
});
38+
countsPerDay[reportedDate] = (countsPerDay[reportedDate] || 0) + 1;
39+
validDates.push(reportDate);
40+
} else {
41+
console.warn(`Invalid date encountered at index ${index}:`, report.reported_time);
42+
}
43+
} catch (error) {
44+
console.warn(`Error processing report date at index ${index}:`, error);
7245
}
73-
})
74-
.filter(date => date !== null) : [];
46+
} else {
47+
console.warn(`Encountered a report with missing or invalid reported_time at index ${index}`);
48+
}
49+
});
7550

7651
if (validDates.length === 0) {
7752
console.error("No valid dates found in the data");
@@ -80,6 +55,7 @@ const LineChart = ({ data }) => {
8055

8156
const minDate = new Date(Math.min(...validDates));
8257
const maxDate = new Date(Math.max(...validDates));
58+
const dates = [];
8359

8460
for (let currentDate = new Date(minDate); currentDate <= maxDate; currentDate.setDate(currentDate.getDate() + (view === 'day' ? 1 : view === 'week' ? 7 : view === 'month' ? 30 : 365))) {
8561
const formattedDate = currentDate.toLocaleDateString('en-GB', {
@@ -99,8 +75,7 @@ const LineChart = ({ data }) => {
9975

10076
const { dates, counts } = convertDataForLineChart;
10177

102-
// Check if we have valid data after conversion
103-
if (!dates || !counts || !Array.isArray(dates) || !Array.isArray(counts) || dates.length === 0 || counts.length === 0) {
78+
if (!dates.length || !counts.length) {
10479
console.error("No valid data after conversion in LineChart component");
10580
return <div>Unable to generate chart due to insufficient data.</div>;
10681
}
@@ -189,4 +164,4 @@ const LineChart = ({ data }) => {
189164
)
190165
}
191166

192-
export default LineChart;
167+
export default LineChart;

0 commit comments

Comments
 (0)