-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.js
98 lines (83 loc) · 3.54 KB
/
tracker.js
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
document.addEventListener('DOMContentLoaded', () => {
const totalIncomeEl = document.getElementById('total-income');
const totalExpenseEl = document.getElementById('total-expense');
const remainingBalanceEl = document.getElementById('remaining-balance');
const transactionForm = document.getElementById('transaction-form');
const transactionTableBody = document.querySelector('#transaction-table tbody');
const spendingChartEl = document.getElementById('spending-chart');
// Load transactions from local storage
let transactions = JSON.parse(localStorage.getItem('transactions')) || [];
// Function to update the remaining balance
function updateRemainingBalance() {
const totalIncome = parseFloat(totalIncomeEl.value) || 0;
const totalExpense = parseFloat(totalExpenseEl.value) || 0;
const remainingBalance = totalIncome - totalExpense;
remainingBalanceEl.value = remainingBalance.toFixed(2);
}
// Add event listeners for manual input changes to update remaining balance
totalIncomeEl.addEventListener('input', updateRemainingBalance);
totalExpenseEl.addEventListener('input', updateRemainingBalance);
function updateTransactionTable() {
transactionTableBody.innerHTML = '';
transactions.forEach(t => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${t.type}</td>
<td>$${t.amount.toFixed(2)}</td>
<td>${t.date}</td>
<td>${t.description}</td>
`;
transactionTableBody.appendChild(row);
});
}
function updateChart() {
const ctx = spendingChartEl.getContext('2d');
const labels = transactions.map(t => t.description || t.date);
const data = transactions.map(t => t.amount);
if (window.spendingChart) {
window.spendingChart.destroy();
}
window.spendingChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Spending Overview',
data: data,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
transactionForm.addEventListener('submit', (e) => {
e.preventDefault();
const type = document.getElementById('type').value;
const amount = parseFloat(document.getElementById('amount').value);
const date = document.getElementById('date').value;
const description = document.getElementById('description').value;
if (isNaN(amount) || amount <= 0) {
alert('Please enter a valid amount.');
return;
}
const newTransaction = { type, amount, date, description };
transactions.push(newTransaction);
// Save transactions to local storage
localStorage.setItem('transactions', JSON.stringify(transactions));
transactionForm.reset();
updateTransactionTable();
updateChart();
});
// Initialize the dashboard
updateTransactionTable();
updateChart();
updateRemainingBalance(); // Initial balance calculation
});