-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (118 loc) · 3.88 KB
/
script.js
File metadata and controls
132 lines (118 loc) · 3.88 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
document.addEventListener('DOMContentLoaded', function () {
// Get references to the elements
const incomeInput = document.getElementById('income');
const expenseInput = document.getElementById('expense');
const calculateBtn = document.getElementById('calculate-btn');
const balanceDisplay = document.getElementById('balance');
// Setup chart.js contexts
const lineChartCtx = document.getElementById('income-expenses-line-chart').getContext('2d');
const barChartCtx = document.getElementById('income-expenses-stacked-bar-chart').getContext('2d');
const polarChartCtx = document.getElementById('income-expenses-polar-chart').getContext('2d');
// Data arrays for the graphs (for dynamic updating)
let incomeData = [];
let expenseData = [];
let balanceData = [];
// Create Chart.js charts
const lineChart = new Chart(lineChartCtx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Income',
data: [],
borderColor: 'green',
backgroundColor: 'rgba(0,255,0,0.3)',
fill: true,
}, {
label: 'Expense',
data: [],
borderColor: 'red',
backgroundColor: 'rgba(255,0,0,0.3)',
fill: true,
}]
},
options: {
responsive: true,
scales: {
x: { title: { display: true, text: 'Time' }},
y: { title: { display: true, text: 'Amount ($)' }}
}
}
});
const barChart = new Chart(barChartCtx, {
type: 'bar',
data: {
labels: [],
datasets: [
{
label: 'Income',
data: [],
backgroundColor: 'green',
},
{
label: 'Expense',
data: [],
backgroundColor: 'red',
}
]
},
options: {
responsive: true,
scales: {
x: { title: { display: true, text: 'Time' }},
y: { title: { display: true, text: 'Amount ($)' }}
}
}
});
const polarChart = new Chart(polarChartCtx, {
type: 'polarArea',
data: {
labels: ['Income', 'Expense'],
datasets: [{
data: [0, 0],
backgroundColor: ['green', 'red'],
}]
},
options: {
responsive: true
}
});
// Handle the "Calculate Balance" button click
calculateBtn.addEventListener('click', function () {
// Get the values of income and expense
const income = parseFloat(incomeInput.value) || 0;
const expense = parseFloat(expenseInput.value) || 0;
// Calculate the balance
const result = income-expense;
if(result>0){
balance=result
}
else balance=0;
// Update the balance display
balanceDisplay.textContent = balance.toFixed(2);
// Update graph data dynamically
updateGraphs(income, expense, balance);
});
// Function to update graphs with new data
function updateGraphs(income, expense, balance) {
// Time as a label (for simplicity, just using the count of data entries)
const timeLabel = `Month ${incomeData.length + 1}`;
// Add new data to the arrays
incomeData.push(income);
expenseData.push(expense);
balanceData.push(balance);
// Update the line chart
lineChart.data.labels.push(timeLabel);
lineChart.data.datasets[0].data.push(income);
lineChart.data.datasets[1].data.push(expense);
lineChart.update();
// Update the bar chart
barChart.data.labels.push(timeLabel);
barChart.data.datasets[0].data.push(income);
barChart.data.datasets[1].data.push(expense);
barChart.update();
// Update the polar chart
polarChart.data.datasets[0].data = [incomeData.reduce((a, b) => a + b, 0), expenseData.reduce((a, b) => a + b, 0)];
polarChart.update();
}
});