-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (153 loc) · 4.95 KB
/
script.js
File metadata and controls
178 lines (153 loc) · 4.95 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Elements
const balance = document.getElementById("balance");
const money_plus = document.getElementById("money-plus");
const money_minus = document.getElementById("money-minus");
const list = document.getElementById("list");
const form = document.getElementById("form");
const text = document.getElementById("text");
const amount = document.getElementById("amount");
const date = document.getElementById("date");
const filterDate = document.getElementById("filter-date");
const spendingChartCanvas = document.getElementById('spendingChart');
const openChartBtn = document.getElementById('open-chart');
// Declare spendingChart variable at the top
let spendingChart;
// Retrieve transactions from local storage or set to empty array
const localStorageTransactions = JSON.parse(localStorage.getItem('transactions'));
let transactions = localStorage.getItem('transactions') !== null ? localStorageTransactions : [];
// Add transaction
function addTransaction(type) {
if (text.value.trim() === '' || amount.value.trim() === '' || date.value.trim() === '') {
alert('Please add text, amount, and date');
} else {
const transaction = {
id: generateID(),
text: text.value,
amount: type === 'income' ? +amount.value : -amount.value,
date: date.value
};
transactions.push(transaction);
addTransactionDOM(transaction);
updateValues();
updateLocalStorage();
updateSpendingChart();
text.value = '';
amount.value = '';
date.value = '';
}
}
// Generate random ID
function generateID() {
return Math.floor(Math.random() * 1000000000);
}
// Add transaction to DOM list
function addTransactionDOM(transaction) {
// Get sign
const sign = transaction.amount < 0 ? "-" : "+₹";
const item = document.createElement("li");
// Add class based on value
item.classList.add(transaction.amount < 0 ? "minus" : "plus");
item.innerHTML = `
${transaction.text} <span>${sign}${Math.abs(transaction.amount)}</span>
<span class="transaction-date">${transaction.date}</span>
<button class="delete-btn" onclick="removeTransaction(${transaction.id})">x</button>
`;
list.appendChild(item);
}
// Update the balance, income, and expense
function updateValues() {
const amounts = transactions.map(transaction => transaction.amount);
const total = amounts.reduce((acc, item) => (acc += item), 0).toFixed(2);
const income = amounts.filter(item => item > 0).reduce((acc, item) => (acc += item), 0).toFixed(2);
const expense = (amounts.filter(item => item < 0).reduce((acc, item) => (acc += item), 0) * -1).toFixed(2);
balance.innerText = `₹${total}`;
money_plus.innerText = `₹${income}`;
money_minus.innerText = `₹${expense}`;
}
// Remove transaction by ID
function removeTransaction(id) {
transactions = transactions.filter(transaction => transaction.id !== id);
updateLocalStorage();
init();
}
// Update local storage transactions
function updateLocalStorage() {
localStorage.setItem('transactions', JSON.stringify(transactions));
}
// Filter transactions by date
function filterTransactions() {
const filterDateValue = filterDate.value;
if (filterDateValue) {
const filteredTransactions = transactions.filter(transaction => transaction.date === filterDateValue);
list.innerHTML = '';
filteredTransactions.forEach(addTransactionDOM);
}
}
// Clear date filter
function clearFilter() {
filterDate.value = '';
init();
}
// Initialize app
function init() {
list.innerHTML = '';
transactions.forEach(addTransactionDOM);
updateValues();
updateSpendingChart();
}
init();
form.addEventListener('submit', (e) => e.preventDefault());
// Chart.js: Generate spending chart
function updateSpendingChart() {
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
}
]
};
if (spendingChart) {
spendingChart.destroy();
}
spendingChart = new Chart(spendingChartCanvas, {
type: 'bar',
data: data,
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
// Open new chart tab
openChartBtn.addEventListener('click', () => {
window.open('chart.html', '_blank');
});