forked from androidvitb/Spendwise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
264 lines (231 loc) · 7.47 KB
/
Copy pathscript.js
File metadata and controls
264 lines (231 loc) · 7.47 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const hamburger = document.getElementById("hamburger");
const navigator = document.getElementById("Navigator");
const bar1 = document.getElementById("bar1");
const bar2 = document.getElementById("bar2");
const bar3 = document.getElementById("bar3");
const ul = document.getElementById("ul");
const addBtn = document.getElementById("add");
const titleInput = document.getElementById("expense-title");
const amountInput = document.getElementById("expense-amount");
const dateInput = document.getElementById("expense-date");
const timeInput = document.getElementById("expense-time");
const categoryInput = document.getElementById("expense-category");
const totalAmountCell = document.getElementById("total-amount");
const expensesTableBody = document.getElementById("tableBody");
// Initialize expenses array from localStorage
let expenses = JSON.parse(localStorage.getItem("expenses")) || [];
let totalAmount = 0;
// Function to render expenses
function renderExpenses() {
expensesTableBody.innerHTML = "";
totalAmount = 0;
expenses.forEach((expense, index) => {
const newRow = expensesTableBody.insertRow();
const titleCell = newRow.insertCell();
const amountCell = newRow.insertCell();
const dateCell = newRow.insertCell();
const categoryCell = newRow.insertCell();
const deleteCell = newRow.insertCell();
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.classList.add(
"mx-auto",
"my-[7px]",
"p-2",
"bg-[#377f8e]",
"text-white",
"text-base",
"font-normal"
);
titleCell.classList.add("border-[1px]", "border-r-[#377f8e]");
amountCell.classList.add("border-[1px]", "border-r-[#377f8e]");
dateCell.classList.add("border-[1px]", "border-r-[#377f8e]");
categoryCell.classList.add("border-[1px]", "border-r-[#377f8e]");
deleteCell.classList.add(
"border-[1px]",
"border-r-[#377f8e]",
"flex",
"justify-center",
"items-center"
);
titleCell.textContent = expense.title;
amountCell.textContent = expense.amount;
dateCell.textContent = `${expense.date} ${expense.time}`;
categoryCell.textContent = expense.category;
deleteCell.appendChild(deleteBtn);
totalAmount += expense.amount;
deleteBtn.addEventListener("click", () => {
expenses.splice(index, 1);
localStorage.setItem("expenses", JSON.stringify(expenses));
renderExpenses();
});
});
totalAmountCell.textContent = totalAmount.toFixed(2);
}
// Add new expense
addBtn.addEventListener("click", () => {
const title = titleInput.value.trim();
const amount = parseFloat(amountInput.value);
const date = dateInput.value;
const time = timeInput.value;
const category = categoryInput.value;
// Validation
if (!title || isNaN(amount) || amount <= 0 || !date || !time || !category) {
alert("Please fill all fields correctly.");
return;
}
const newExpense = { title, amount, date, time, category };
expenses.push(newExpense);
localStorage.setItem("expenses", JSON.stringify(expenses));
renderExpenses();
drawChart(window.innerWidth);
titleInput.value = "";
amountInput.value = "";
dateInput.value = "";
timeInput.value = "";
categoryInput.value = "";
});
// Render expenses on page load
renderExpenses();
window.addEventListener("resize", function () {
var windowWidth = window.innerWidth;
if (windowWidth <= 1023) {
navigator.classList.remove("flex");
ul.classList.remove("flex");
} else {
navigator.classList.add("flex");
ul.classList.add("flex");
}
drawChart(windowWidth);
});
// Hamburger Menu
function hamburgers() {
if (bar1.classList.contains("rotate-45")) {
bar1.classList.remove("translate-y-[10px]", "rotate-45");
bar1.classList.add("translate-y-0", "rotate-0");
bar2.classList.remove("opacity-0");
bar3.classList.remove("translate-y-[-6px]", "-rotate-45");
bar3.classList.add("translate-y-0", "rotate-0");
navigator.classList.add("max-lg:hidden");
} else {
bar1.classList.remove("translate-y-0", "rotate-0");
bar1.classList.add("translate-y-[10px]", "rotate-45");
bar2.classList.add("opacity-0");
bar3.classList.remove("translate-y-0", "rotate-0");
bar3.classList.add("translate-y-[-6px]", "-rotate-45");
navigator.classList.remove("max-lg:hidden");
}
}
// Render Charts and Graphs
function drawChart(windowWidth) {
if (expenses.length === 0) {
document.getElementById("bar").innerHTML = "<p class='my-6'>No Data to Show !!</p>";
return;
}
// Initialize chart data
const categoryData = expenses.reduce(
(acc, expense) => {
const categoryIndex = acc.findIndex(
(item) => item[0] === expense.category
);
if (categoryIndex === -1) {
acc.push([expense.category, expense.amount]);
} else {
acc[categoryIndex][1] += expense.amount;
}
return acc;
},
[["Category", "Amount"]]
);
const data = google.visualization.arrayToDataTable(categoryData);
let barOptions, pieOptions;
if (windowWidth > 800) {
barOptions = {
title: "Weekly Expenses",
width: 800,
height: 400,
colors: ["#377f8e"],
};
pieOptions = {
width: 800,
height: 400,
colors: ["#377f8e", "#1f3c42", "#7fb0ba"],
is3D: true,
};
} else if (windowWidth > 500) {
barOptions = {
title: "Weekly Expenses",
width: 600,
height: 400,
colors: ["#377f8e"],
};
pieOptions = {
width: 600,
height: 400,
colors: ["#377f8e", "#1f3c42", "#7fb0ba"],
is3D: true,
};
} else {
barOptions = {
title: "Weekly Expenses",
width: 460,
height: 400,
colors: ["#377f8e"],
};
pieOptions = {
width: 460,
height: 400,
colors: ["#377f8e", "#1f3c42", "#7fb0ba"],
is3D: true,
};
}
// Create chart instances
const barChart = new google.visualization.BarChart(
document.getElementById("bar")
);
const pieChart = new google.visualization.PieChart(
document.getElementById("pie")
);
// Draw the charts
barChart.draw(data, barOptions);
pieChart.draw(data, pieOptions);
}
// Call the drawChart function when the page loads and on window resize
google.charts.load("current", { packages: ["corechart"] });
google.charts.setOnLoadCallback(() => drawChart(window.innerWidth));
// Listen to window resize and redraw the charts
window.addEventListener("resize", function () {
drawChart(window.innerWidth);
});
// Get the button element
const scrollButton = document.getElementById("scrollButton");
// Show the button when the user scrolls down 200px from the top
window.onscroll = () => {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
scrollButton.style.display = "block";
} else {
scrollButton.style.display = "none";
}
};
// Scroll to the top when the button is clicked
scrollButton.onclick = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};
// Select the button
const darkModeToggle = document.getElementById('dark-mode-toggle');
// Add an event listener to the button for dark mode toggle
darkModeToggle.addEventListener('click', (event) => {
// Prevent event bubbling
event.stopPropagation();
// Toggle the 'dark' class on the body
document.body.classList.toggle('dark');
// Change the button text based on the current mode
if (document.body.classList.contains('dark')) {
darkModeToggle.textContent = 'Light Mode';
} else {
darkModeToggle.textContent = 'Dark Mode';
}
});