-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 2.17 KB
/
script.js
File metadata and controls
71 lines (61 loc) · 2.17 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
const transactionList = document.getElementById('transactionList');
const addTransactionBtn = document.getElementById('addTransaction');
const descriptionInput = document.getElementById('description');
const amountInput = document.getElementById('amount');
const categoryInput = document.getElementById('category');
const ctx = document.getElementById('myChart').getContext('2d');
let transactions = [];
addTransactionBtn.addEventListener('click', addTransaction);
function addTransaction() {
const description = descriptionInput.value;
const amount = parseFloat(amountInput.value);
const category = categoryInput.value;
if (description && !isNaN(amount)) {
transactions.push({ description, amount, category });
updateTransactionList();
updateChart();
descriptionInput.value = '';
amountInput.value = '';
} else {
alert("Please fill in both fields.");
}
}
function updateTransactionList() {
transactionList.innerHTML = '';
transactions.forEach((transaction, index) => {
const li = document.createElement('li');
li.textContent = `${transaction.description} - $${transaction.amount} (${transaction.category})`;
transactionList.appendChild(li);
});
}
function updateChart() {
const expenses = transactions.filter(t => t.category === 'expense');
const incomes = transactions.filter(t => t.category === 'income');
const expenseAmount = expenses.reduce((total, t) => total + t.amount, 0);
const incomeAmount = incomes.reduce((total, t) => total + t.amount, 0);
const data = {
labels: ['Income', 'Expenses'],
datasets: [{
label: 'Amount',
data: [incomeAmount, expenseAmount],
backgroundColor: ['#28a745', '#dc3545'],
borderColor: ['#28a745', '#dc3545'],
borderWidth: 1,
}]
};
const config = {
type: 'bar',
data: data,
options: {
scales: {
y: {
beginAtZero: true
}
}
}
};
if (window.myChart) {
window.myChart.destroy();
}
window.myChart = new Chart(ctx, config);
}