-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
193 lines (151 loc) · 5.33 KB
/
app.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
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
// // DOM elements
// const expenseForm = document.getElementById('expense-form');
// const expenseNameInput = document.getElementById('expense-name');
// const expenseAmountInput = document.getElementById('expense-amount');
// const expenseDateInput = document.getElementById('expense-date');
// const expenseList = document.getElementById('expense-list');
// const totalExpenseDisplay = document.getElementById('total-expense');
// // Expenses array to store data
// let expenses = JSON.parse(localStorage.getItem('expenses')) || [];
// // Event listener for form submission
// expenseForm.addEventListener('submit', function(e) {
// e.preventDefault();
// const name = expenseNameInput.value;
// const amount = parseFloat(expenseAmountInput.value);
// const date = expenseDateInput.value;
// // Create a new expense object
// const expense = { id: Date.now(), name, amount, date };
// // Add expense to array and localStorage
// expenses.push(expense);
// localStorage.setItem('expenses', JSON.stringify(expenses));
// // Reset form inputs
// expenseNameInput.value = '';
// expenseAmountInput.value = '';
// expenseDateInput.value = '';
// // Update UI
// displayExpenses();
// calculateTotal();
// });
// // Function to display expenses in the table
// function displayExpenses() {
// expenseList.innerHTML = ''; // Clear existing list
// expenses.forEach(expense => {
// const row = document.createElement('tr');
// row.innerHTML = `
// <td>${expense.name}</td>
// <td>$${expense.amount.toFixed(2)}</td>
// <td>${expense.date}</td>
// <td><button onclick="deleteExpense(${expense.id})">Delete</button></td>
// `;
// expenseList.appendChild(row);
// });
// }
// // Function to calculate total expense
// function calculateTotal() {
// const total = expenses.reduce((sum, expense) => sum + expense.amount, 0);
// totalExpenseDisplay.textContent = total.toFixed(2);
// }
// // Function to delete an expense
// function deleteExpense(id) {
// expenses = expenses.filter(expense => expense.id !== id);
// localStorage.setItem('expenses', JSON.stringify(expenses));
// displayExpenses();
// calculateTotal();
// }
// // Initialize app
// displayExpenses();
// calculateTotal();
// Select Elements
const expenseForm = document.getElementById("expense-form");
const expenseNameInput = document.getElementById("expense-name");
const expenseAmountInput = document.getElementById("expense-amount");
const expenseDateInput = document.getElementById("expense-date");
const expenseList = document.getElementById("expense-list");
const totalExpenseDisplay = document.getElementById("total-expense");
// Expense Data
let expenses = JSON.parse(localStorage.getItem("expenses")) || [];
// Chart.js Initialization
let expenseChart;
const ctx = document.getElementById("expense-chart").getContext("2d");
// Update Chart Function
function updateChart() {
const names = expenses.map(exp => exp.name);
const amounts = expenses.map(exp => exp.amount);
if (expenseChart) {
expenseChart.destroy(); // Destroy existing chart before updating
}
expenseChart = new Chart(ctx, {
type: "pie", // Change to 'bar' for bar graph
data: {
labels: names,
datasets: [{
label: "Expense Distribution",
data: amounts,
backgroundColor: [
"#FF6384", "#36A2EB", "#FFCE56", "#4BC0C0", "#9966FF", "#FF9F40"
],
hoverOffset: 4
}]
},
options: {
responsive: true
}
});
}
// Add Expense Function
function addExpense(event) {
event.preventDefault();
const name = expenseNameInput.value;
const amount = parseFloat(expenseAmountInput.value);
const date = expenseDateInput.value;
if (name && amount && date) {
const newExpense = { name, amount, date };
expenses.push(newExpense);
// Save to Local Storage
localStorage.setItem("expenses", JSON.stringify(expenses));
// Update DOM
renderExpenses();
updateChart();
updateTotal();
// Reset Form
expenseForm.reset();
}
}
// Render Expenses in Table
function renderExpenses() {
expenseList.innerHTML = "";
expenses.forEach((exp, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${exp.name}</td>
<td>$${exp.amount.toFixed(2)}</td>
<td>${exp.date}</td>
<td><button onclick="deleteExpense(${index})">Delete</button></td>
`;
expenseList.appendChild(row);
});
}
// Update Total Expense
function updateTotal() {
const total = expenses.reduce((sum, exp) => sum + exp.amount, 0);
totalExpenseDisplay.textContent = total.toFixed(2);
}
// Delete Expense
function deleteExpense(index) {
expenses.splice(index, 1);
// Update Local Storage
localStorage.setItem("expenses", JSON.stringify(expenses));
renderExpenses();
updateChart();
updateTotal();
}
// Load Data from Local Storage on Page Load
function loadExpenses() {
renderExpenses();
updateChart();
updateTotal();
}
// Event Listener
expenseForm.addEventListener("submit", addExpense);
// Initial Load
loadExpenses();