forked from Definehack/Define25
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalytics.jsx
More file actions
74 lines (66 loc) · 2.79 KB
/
Analytics.jsx
File metadata and controls
74 lines (66 loc) · 2.79 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
import React, { useState, useEffect } from "react";
import { BarChart, Bar, XAxis, YAxis, Tooltip, Legend, ResponsiveContainer } from "recharts";
import { fetchGooglePayTransactions } from "../utils/product_trancation.jsx"; // Add .jsx extension
const AnalyticsChart = () => {
const [transactionData, setTransactionData] = useState([]);
const [year, setYear] = useState("2024");
useEffect(() => {
// Fetch transactions (Replace with real API call)
const loadTransactions = async () => {
const transactions = await fetchGooglePayTransactions();
setTransactionData(transactions);
};
loadTransactions();
}, []);
// Function to process transactions and get income & outcome per month
const getMonthlyData = () => {
const monthlyData = Array.from({ length: 12 }, (_, index) => ({
month: new Date(2024, index, 1).toLocaleString("en-US", { month: "short" }),
income: 0,
outcome: 0,
}));
transactionData.forEach((txn) => {
const txnDate = new Date(txn.date);
const txnYear = txnDate.getFullYear().toString();
if (txnYear === year) {
const monthIndex = txnDate.getMonth();
if (txn.type === "income") {
monthlyData[monthIndex].income += txn.amount;
} else if (txn.type === "outcome") {
monthlyData[monthIndex].outcome += txn.amount;
}
}
});
return monthlyData;
};
return (
<div className="analytics-container">
<h2>Analytics</h2>
<select
value={year}
onChange={(e) => setYear(e.target.value)}
className="year-select"
>
<option value="2024">2024</option>
<option value="2023">2023</option>
</select>
<ResponsiveContainer width="100%" height={300}>
<BarChart data={getMonthlyData()}>
<XAxis dataKey="month" stroke="rgba(255,255,255,0.7)" />
<YAxis stroke="rgba(255,255,255,0.7)" />
<Tooltip
contentStyle={{
background: '#1A1A1A',
border: '1px solid rgba(255,255,255,0.1)',
color: '#fff'
}}
/>
<Legend />
<Bar dataKey="income" fill="#774EBD" name="Income" />
<Bar dataKey="outcome" fill="#372457" name="Outcome" />
</BarChart>
</ResponsiveContainer>
</div>
);
};
export default AnalyticsChart;