-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart-script.js
More file actions
55 lines (49 loc) · 1.53 KB
/
chart-script.js
File metadata and controls
55 lines (49 loc) · 1.53 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
document.addEventListener('DOMContentLoaded', () => {
const localStorageTransactions = JSON.parse(localStorage.getItem('transactions'));
let transactions = localStorage.getItem('transactions') !== null ? localStorageTransactions : [];
const spendingChartCanvas = document.getElementById('spendingChart');
const monthlyExpenses = new Array(12).fill(0);
const monthlyIncome = new Array(12).fill(0);
transactions.forEach(transaction => {
const month = new Date(transaction.date).getMonth();
if (transaction.amount < 0) {
monthlyExpenses[month] += Math.abs(transaction.amount);
} else {
monthlyIncome[month] += transaction.amount;
}
});
const data = {
labels: [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
],
datasets: [
{
label: 'Expenses',
data: monthlyExpenses,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
},
{
label: 'Income',
data: monthlyIncome,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}
]
};
new Chart(spendingChartCanvas, {
type: 'bar',
data: data,
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
});