-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (278 loc) · 8.62 KB
/
script.js
File metadata and controls
311 lines (278 loc) · 8.62 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"use strict";
// dashboard elements
let balnace_num = document.querySelector(".balance-number");
let expense_num = document.querySelector(".expense-number");
let income_num = document.querySelector(".income-number");
//income section elements
const income_amount_value = parseInt(document.querySelector(".amount").value);
const income_amount = document.querySelector(".amount");
const income_date = document.querySelector(".date");
const income_remarks = document.querySelector(".remarks");
const income_source = document.querySelector(".source");
const income_button = document.querySelector(".add-inc");
const income_history_ul = document.querySelector(".listTrans");
//budget form elements
const budget_amount = document.querySelector(".budget");
const budget_type = document.querySelector(".type");
const budget_button = document.querySelector("#next-btn");
const budget_list_ul = document.querySelector("#budget-list-ul");
//expense section details
const expense_amount = document.querySelector("#expense-amount");
const expense_date = document.querySelector("#expense-date");
const expense_remarks = document.querySelector("#expense-remarks");
const expense_source = document.querySelector("#expense-source");
const expense_button = document.querySelector("#expense-btn");
const expense_history_ul = document.querySelector("#expenseChk");
const income_Form = document.querySelector(".income-form");
const expense_form = document.querySelector(".expense-form");
function preventsubmission(event) {
event.preventDefault();
}
function cleareverything(amount, date, remarks, source) {
amount.value = "";
date.value = "";
remarks.innerText = "";
source.value = "select type";
}
//peventing the default behaviour of forms
income_button.addEventListener("click", preventsubmission);
expense_button.addEventListener("click", preventsubmission);
budget_button.addEventListener("click", preventsubmission);
const income_array = [];
const expense_array = [];
const balance_array = [];
//craeting history list element
function createlistelement(amount, remarks, date, source, ul) {
let li = document.createElement("li");
li.innerHTML = `<span class = "color">${amount.value}</span> from <span class="color">${remarks.value}</span> on <span class="color">${date.value}</span> as <span class="color" >${source.value}</span> `;
ul.appendChild(li);
}
let clone_source;
let clone_amount;
function addhistory(amount, date, remarks, source, ul) {
if (
amount.value === "" ||
date.value === "" ||
remarks.value === "" ||
source.value === "select type"
) {
alert("please fill out the neccesary feilds!");
} else if (amount.value < 0) {
alert("BUG dhundh rhe ho kya,nii milega..");
cleareverything(amount, date, remarks, source);
} else {
createlistelement(amount, remarks, date, source, ul);
clone_source = source.value;
clone_amount = amount.value;
console.log(clone_source);
cleareverything(amount, date, remarks, source);
}
}
function updatedashboard(array, num) {
const total = array.reduce((acc, income) => acc + income, 0);
if (total > 0) {
num.innerHTML = total;
}
}
function update_balance() {
const inc = parseInt(income_amount.value) || 0;
const exp = parseInt(expense_amount.value) || 0;
console.log(exp);
let balance = inc - exp;
console.log(balance);
console.log(typeof balance);
balnace_num.innerHTML = balance;
}
const inc_canvas = document.querySelector("#incomeChart");
const exp_canvas = document.querySelector("#expenseChart");
inc_canvas.width = "450";
inc_canvas.height = "550";
exp_canvas.width = "450";
exp_canvas.height = "550";
const budget_array = [];
//updating income
income_button.addEventListener("click", handleincome);
function handleincome() {
income_array.push(parseInt(income_amount.value));
addhistory(
income_amount,
income_date,
income_remarks,
income_source,
income_history_ul
);
updatedashboard(income_array, income_num);
update_balance();
}
// managing budget options
budget_button.addEventListener("click", handlebudget);
let temp_budget;
function handlebudget() {
budget_array.push(parseInt(budget_amount.value));
temp_budget = budget_amount.value;
makebudgetbar();
}
function makebudgetbar() {
if (
budget_amount.value === "" ||
budget_type.value === "" ||
budget_amount < 0
) {
alert("fill out the neccesary details correctly!");
} else {
let li = document.createElement("li");
li.innerHTML = `
<p class="inLi">${budget_type.value}</p>
<span style="display:inline">${budget_amount.value}</span>
<div class="bar">
<div class="bar-color"></div>
</div>
<span class="progress" style="dispaly:inline;"></span>
`;
budget_list_ul.appendChild(li);
let option = document.createElement("option");
option.innerHTML = ` <option value="${budget_type.value}">${budget_type.value}</option>`;
document.querySelector("#expense-source").appendChild(option);
cleareverything(budget_amount, budget_type);
}
}
//managing the expense section
expense_button.addEventListener("click", handleexpense);
let budgetProgress = {};
function handleexpense() {
expense_array.push(parseInt(expense_amount.value));
console.log(parseInt(expense_amount.value));
addhistory(
expense_amount,
expense_date,
expense_remarks,
expense_source,
expense_history_ul
);
updatedashboard(expense_array, expense_num);
update_balance();
updateBudgetBar();
cleareverything(
expense_amount,
expense_date,
expense_remarks,
expense_source
);
}
function updateBudgetBar() {
const bars = document.querySelectorAll("#budget-list-ul li");
bars.forEach((bar) => {
const barType = bar.querySelector(".inLi").innerText;
if (barType === clone_source) {
let barnew = bar.querySelector(".bar-color");
if (!budgetProgress[barType]) {
budgetProgress[barType] = 0;
}
budgetProgress[barType] += (parseInt(clone_amount) / temp_budget) * 100;
barnew.style.width = `${budgetProgress[barType]}%`;
console.log("if working");
}
});
}
// Initialize charts
const chartColors = [
"rgba(75, 192, 192, 0.6)",
"rgba(255, 99, 132, 0.6)",
"rgba(255, 206, 86, 0.6)",
"rgba(54, 162, 235, 0.6)",
"rgba(153, 102, 255, 0.6)",
"rgba(255, 159, 64, 0.6)",
"rgba(201, 203, 207, 0.6)",
"rgba(255, 0, 0, 0.6)",
"rgba(0, 255, 0, 0.6)",
"rgba(0, 0, 255, 0.6)",
];
// Initialize charts
let incomeCharts = [];
let expenseCharts = [];
function initializeCharts() {
const ctxIncome = inc_canvas.getContext("2d");
const ctxExpense = exp_canvas.getContext("2d");
// Create a chart for each income entry
income_array.forEach((income, index) => {
const incomeChart = new Chart(ctxIncome, {
type: "pie",
data: {
labels: [`Income ${index + 1}`],
datasets: [
{
label: "Income",
data: [income],
backgroundColor: [chartColors[index % chartColors.length]],
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
},
},
});
incomeCharts.push(incomeChart);
});
// Create a chart for each expense entry
expense_array.forEach((expense, index) => {
const expenseChart = new Chart(ctxExpense, {
type: "pie",
data: {
labels: [`Expense ${index + 1}`],
datasets: [
{
label: "Expense",
data: [expense],
backgroundColor: [chartColors[index % chartColors.length]],
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
},
},
});
expenseCharts.push(expenseChart);
});
}
function handleincome() {
const newIncome = parseInt(income_amount.value);
// Check if the income amount is valid
if (!isNaN(newIncome) && newIncome > 0) {
// Update the income_array with the new value
income_array.push(newIncome);
addhistory(
income_amount,
income_date,
income_remarks,
income_source,
income_history_ul
);
updatedashboard(income_array, income_num);
update_balance();
} else {
alert("Please enter a valid income amount.");
}
}
function handleexpense() {
expense_array.push(parseInt(expense_amount.value));
addhistory(
expense_amount,
expense_date,
expense_remarks,
expense_source,
expense_history_ul
);
updatedashboard(expense_array, expense_num);
update_balance();
updateBudgetBar();
}