-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
212 lines (186 loc) · 5.83 KB
/
index.js
File metadata and controls
212 lines (186 loc) · 5.83 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
const calc = () => {
let initialDeposit = document.querySelector("#initialDeposit");
let contributionAmount = document.querySelector("#contributionAmount");
let investmentTimespan = document.querySelector("#investmentTimespan");
let investmentTimespanText = document.querySelector(
"#investmentTimespanText"
);
let estimatedReturn = document.querySelector("#estimatedReturn");
let futureBalance = document.querySelector("#futureBalance");
function updateValue(element, action) {
let min = parseFloat(element.getAttribute("min"));
let max = parseFloat(element.getAttribute("max"));
let step = parseFloat(element.getAttribute("step")) || 1;
let oldValue = element.dataset.value || element.defaultValue || 0;
let newValue = parseFloat(element.value.replace(/\$/, ""));
if (isNaN(parseFloat(newValue))) {
newValue = oldValue;
} else {
if (action == "add") {
newValue += step;
} else if (action == "sub") {
newValue -= step;
}
newValue = newValue < min ? min : newValue > max ? max : newValue;
}
element.dataset.value = newValue;
element.value =
(element.dataset.prepend || "") +
newValue +
(element.dataset.append || "");
updateChart();
}
function getChartData() {
let principleFormInput = parseFloat(initialDeposit.dataset.value); // Principal
let estimatedReturnFormInput = parseFloat(
estimatedReturn.dataset.value / 100
); // Annual Interest Rate
let contributionFormInput = parseFloat(contributionAmount.dataset.value); // Contribution Amount
let compoundPeriodFormInput = parseInt(
document.querySelector('[name="compound_period"]:checked').value
); // Compound Period
let contributionPeriodFormInput = parseInt(
document.querySelector('[name="contribution_period"]:checked').value
); // Contribution Period
let investmentTimeSpan = parseInt(investmentTimespan.value); // Investment Time Span
let currentYear = new Date().getFullYear();
let labels = [];
for (
let year = currentYear;
year < currentYear + investmentTimeSpan;
year++
) {
labels.push(year);
}
let principalDataset = {
label: "Total Principal",
backgroundColor: "rgb(0, 123, 255)",
data: []
};
let interestDataset = {
label: "Total Interest",
backgroundColor: "rgb(23, 162, 184)",
data: []
};
for (let i = 1; i <= investmentTimeSpan; i++) {
let principal =
principleFormInput +
contributionFormInput * contributionPeriodFormInput * i;
let interest = 0;
let balance = principal;
if (estimatedReturnFormInput) {
let x = Math.pow(
1 + estimatedReturnFormInput / compoundPeriodFormInput,
compoundPeriodFormInput * i
);
let compoundInterest = principleFormInput * x;
let contributionInterest =
(contributionFormInput * (x - 1)) /
(estimatedReturnFormInput / contributionPeriodFormInput);
interest = (
compoundInterest +
contributionInterest -
principal
).toFixed(0);
balance = (compoundInterest + contributionInterest).toFixed(0);
}
futureBalance.innerHTML = "$" + balance;
principalDataset.data.push(principal);
interestDataset.data.push(interest);
}
return {
labels: labels,
datasets: [principalDataset, interestDataset]
};
}
function updateChart() {
let data = getChartData();
chart.data.labels = data.labels;
chart.data.datasets[0].data = data.datasets[0].data;
chart.data.datasets[1].data = data.datasets[1].data;
chart.update();
}
initialDeposit.addEventListener("change", function() {
updateValue(this);
});
contributionAmount.addEventListener("change", function() {
updateValue(this);
});
estimatedReturn.addEventListener("change", function() {
updateValue(this);
});
investmentTimespan.addEventListener("change", function() {
investmentTimespanText.innerHTML = this.value + " years";
updateChart();
});
investmentTimespan.addEventListener("input", function() {
investmentTimespanText.innerHTML = this.value + " years";
});
let radios = document.querySelectorAll(
'[name="contribution_period"], [name="compound_period"]'
);
for (let j = 0; j < radios.length; j++) {
radios[j].addEventListener("change", updateChart);
}
let buttons = document.querySelectorAll("[data-counter]");
for (let i = 0; i < buttons.length; i++) {
let button = buttons[i];
button.addEventListener("click", function() {
let field = document.querySelector('[name="' + this.dataset.field + '"]');
let action = this.dataset.counter;
if (field) {
updateValue(field, action);
}
});
}
let ctx = document.getElementById("myChart").getContext("2d");
let chart = new Chart(ctx, {
type: "bar",
data: getChartData(),
options: {
legend: {
display: false
},
tooltips: {
mode: "index",
intersect: false,
callbacks: {
label: function(tooltipItem, data) {
return (
data.datasets[tooltipItem.datasetIndex].label +
": $" +
tooltipItem.yLabel
);
}
}
},
responsive: true,
scales: {
xAxes: [
{
stacked: true,
scaleLabel: {
display: true,
labelString: "Year"
}
}
],
yAxes: [
{
stacked: true,
ticks: {
callback: function(value) {
return "$" + value;
}
},
scaleLabel: {
display: true,
labelString: "Balance"
}
}
]
}
}
});
};
calc();