This repository was archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrendsComparison.jsx
81 lines (76 loc) · 2.54 KB
/
TrendsComparison.jsx
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
import React from 'react';
import { Bar } from 'react-chartjs-2';
import PropTypes from 'prop-types';
// test data for display
const TrendsComparison = props => {
const [m1Data, setm1Data] = React.useState({});
const [m2Data, setm2Data] = React.useState({});
const [categories, setCategories] = React.useState({});
const [graphData, setGraphData] = React.useState({});
React.useEffect(() => {
const cat1 = {};
const cat2 = {};
const totalCats = {};
props.data.accountData.accounts.forEach(account => {
if (account.transactions[props.y1]) {
if (account.transactions[props.y1][props.m1]) {
account.transactions[props.y1][props.m1].forEach(transaction => {
if (cat1[transaction.category]) {
cat1[transaction.category] += Number(transaction.amount);
} else {
cat1[transaction.category] = Number(transaction.amount);
totalCats[transaction.category] = 1;
}
});
}
}
if (account.transactions[props.y2]) {
if (account.transactions[props.y2][props.m2]) {
account.transactions[props.y2][props.m2].forEach(transaction => {
if (cat2[transaction.category]) {
cat2[transaction.category] += Number(transaction.amount);
} else {
cat2[transaction.category] = Number(transaction.amount);
totalCats[transaction.category] = 1;
}
});
}
}
});
setm1Data(cat1);
setm2Data(cat2);
setCategories(totalCats);
}, [props.data.accountData.accounts, props.m1, props.m2, props.y1, props.y2]);
React.useEffect(() => {
setGraphData({
labels: Object.keys(categories).sort(),
datasets: [
{
label: 'Month 1',
data: Object.keys(categories)
.sort()
.map(cat => m1Data[cat] || 0),
backgroundColor: ['#FF6384', '#36A2EB', '#FFCE56'],
hoverBackgroundColor: ['#FF6384', '#36A2EB', '#FFCE56']
},
{
label: 'Month 2',
data: Object.keys(categories)
.sort()
.map(cat => m2Data[cat] || 0),
backgroundColor: ['#FF6384', '#36A2EB', '#FFCE56'],
hoverBackgroundColor: ['#FF6384', '#36A2EB', '#FFCE56']
}
]
});
}, [m1Data, m2Data, categories]);
return <Bar data={graphData} />;
};
export default TrendsComparison;
TrendsComparison.propTypes = {
data: PropTypes.object,
m1: PropTypes.string,
m2: PropTypes.string,
y1: PropTypes.string,
y2: PropTypes.string
};