Payment Calculator
body { font-family: Arial, sans-serif; background: #f7f7f7; margin: 0; }
.container { max-width: 750px; margin: 30px auto; background: #fff; padding: 30px 20px 40px 20px; border-radius: 8px; box-shadow: 0 0 8px #ccc; }
h2 { margin-top: 0; }
.tabs { display: flex; margin-bottom: 20px; }
.tab { padding: 10px 20px; cursor: pointer; background: #eaeaea; border-radius: 5px 5px 0 0; margin-right: 5px; }
.tab.active { background: #2b7ddb; color: #fff; }
.form-row { margin-bottom: 16px; display: flex; align-items: center; }
.form-row label { width: 140px; }
.form-row input { width: 150px; padding: 5px; }
.btn { padding: 8px 18px; border: none; background: #2b7ddb; color: #fff; border-radius: 4px; cursor: pointer; }
.btn.clear { background: #aaa; margin-left: 10px; }
#result { background: #eaf6ff; padding: 18px; border-radius: 6px; margin-top: 20px; }
.charts-vertical { display: flex; flex-direction: column; align-items: center; gap: 18px; margin-top: 30px; }
.bar-chart-container { width: 100%; display: flex; justify-content: center; }
.pie-chart-container { width: 80%; margin: 0 auto; display: flex; justify-content: center; }
.amort-tabs { margin-top: 30px; }
.amort-tabs button { margin-right: 10px; }
table { border-collapse: collapse; width: 100%; margin-top: 10px; }
th, td { border: 1px solid #ddd; padding: 6px 10px; text-align: right; }
th { background: #2b7ddb; color: #fff; }
tr:nth-child(even) { background: #f2f2f2; }
.center { text-align: center; }
.h2result { color: #fff; background: #4CAF50; padding: 10px 0; border-radius: 4px; font-size: 20px; margin: 20px 0 10px 0; }
.panel2 { background: #f4f8fb; border-radius: 6px; padding: 15px 10px 10px 10px; margin-bottom: 10px; }
.topmenu { margin-bottom: 12px; }
.topmenu ul { list-style: none; padding: 0; margin: 0; display: flex; }
.topmenu li { margin-right: 10px; }
.topmenu li a { text-decoration: none; color: #2b7ddb; font-weight: bold; }
.topmenu li#menuon a { color: #fff; background: #2b7ddb; border-radius: 5px 5px 0 0; padding: 8px 18px; }
@media (max-width: 700px) {
.container { padding: 12px 2vw; }
.form-row label, .form-row input { width: 100%; }
.form-row { flex-direction: column; align-items: flex-start; }
.bar-chart-container, .pie-chart-container { width: 100%; }
}
Payment Calculator
%
let amortData = { annual: [], monthly: [] };
function switchTab(tab) {
document.getElementById('tab-term').classList.remove('active');
document.getElementById('tab-payment').classList.remove('active');
document.getElementById('form-term').style.display = 'none';
document.getElementById('form-payment').style.display = 'none';
if (tab === 'term') {
document.getElementById('tab-term').classList.add('active');
document.getElementById('form-term').style.display = '';
} else {
document.getElementById('tab-payment').classList.add('active');
document.getElementById('form-payment').style.display = '';
}
clearForm();
}
function clearForm() {
document.getElementById('loan-amount').value = 200000;
document.getElementById('loan-term').value = 15;
document.getElementById('loan-amount2').value = 200000;
document.getElementById('monthly-pay').value = 2000;
document.getElementById('interest-rate').value = 6;
document.getElementById('result').innerHTML = '';
document.getElementById('amort-table').innerHTML = '';
pieChart.data.datasets[0].data = [1, 1];
pieChart.update();
barChart.data.labels = [];
barChart.data.datasets.forEach(ds => ds.data = []);
barChart.update();
}
function calculate() {
let isTerm = document.getElementById('tab-term').classList.contains('active');
let loan, term, rate, monthlyPay;
rate = parseFloat(document.getElementById('interest-rate').value) / 100 / 12;
if (isTerm) {
loan = parseFloat(document.getElementById('loan-amount').value);
term = parseInt(document.getElementById('loan-term').value) * 12;
monthlyPay = loan * rate / (1 - Math.pow(1 + rate, -term));
} else {
loan = parseFloat(document.getElementById('loan-amount2').value);
monthlyPay = parseFloat(document.getElementById('monthly-pay').value);
term = Math.log(monthlyPay / (monthlyPay - loan * rate)) / Math.log(1 + rate);
term = Math.ceil(term);
}
let totalPay = monthlyPay * term;
let totalInterest = totalPay - loan;
document.getElementById('result').innerHTML =
`
Monthly Payment: $${monthlyPay.toFixed(2)}
You will need to pay $${monthlyPay.toFixed(2)} every month for ${Math.round(term/12*100)/100} years to payoff the debt.
| Total of ${term} Payments | $${totalPay.toFixed(2)} |
| Total Interest | $${totalInterest.toFixed(2)} |
`;
// Pie Chart
pieChart.data.datasets[0].data = [loan, totalInterest];
pieChart.update();
// Amortization
amortData = buildAmortization(loan, rate, term, monthlyPay);
// Bar/Line Chart
barChart.data.labels = amortData.annual.map(row => row.year);
barChart.data.datasets[0].data = amortData.annual.map(row => row.balance);
barChart.data.datasets[1].data = amortData.annual.map(row => row.interest);
barChart.data.datasets[2].data = amortData.annual.map(row => row.payment);
barChart.update();
showAmort('annual');
}
function buildAmortization(loan, rate, term, monthlyPay) {
let balance = loan, dataAnn = [], dataMon = [];
let totalInterest = 0;
for (let i = 1; i <= term; i++) {
let interest = balance * rate;
let principal = monthlyPay - interest;
if (balance 0 ? balance : 0
});
// Annual summary
if (i % 12 === 0 || i === term) {
let annPay = monthlyPay * (i % 12 === 0 ? 12 : i % 12);
let annInt = dataMon.slice(i - (i % 12 === 0 ? 12 : i % 12), i).reduce((s, r) => s + r.interest, 0);
let annPrin = dataMon.slice(i - (i % 12 === 0 ? 12 : i % 12), i).reduce((s, r) => s + r.principal, 0);
dataAnn.push({
year: Math.ceil(i / 12),
payment: annPay,
principal: annPrin,
interest: annInt,
balance: balance > 0 ? balance : 0
});
}
if (balance <= 0) break;
}
return { annual: dataAnn, monthly: dataMon };
}
function showAmort(type) {
let data = amortData[type];
if (!data.length) return;
let html = `
`;
if (type === 'annual') {
html += `| Year | Interest | Principal | Ending Balance |
`;
data.forEach(row => {
html += `| ${row.year} | $${row.interest.toFixed(2)} | $${row.principal.toFixed(2)} | $${row.balance.toFixed(2)} |
`;
});
} else {
html += `Month | Interest | Principal | Ending Balance | `;
data.forEach(row => {
html += `| ${row.month} | $${row.interest.toFixed(2)} | $${row.principal.toFixed(2)} | $${row.balance.toFixed(2)} |
`;
});
}
html += `
`;
document.getElementById('amort-table').innerHTML = html;
}
// Chart.js Pie Chart
const pieChart = new Chart(document.getElementById('pieChart'), {
type: 'pie',
data: {
labels: ['Principal', 'Interest'],
datasets: [{
data: [1, 1],
backgroundColor: ['#2b7ddb', '#8bbc21'],
}]
},
options: {
plugins: {
legend: { display: true, position: 'bottom' }
}
}
});
// Chart.js Bar/Line Chart
const barChart = new Chart(document.getElementById('barChart'), {
type: 'line',
data: {
labels: [],
datasets: [
{ label: 'Balance', data: [], borderColor: '#2b7ddb', backgroundColor: '#2b7ddb22', fill: false, tension: 0.1 },
{ label: 'Interest', data: [], borderColor: '#8bbc21', backgroundColor: '#8bbc2122', fill: false, tension: 0.1 },
{ label: 'Payment', data: [], borderColor: '#f45b5b', backgroundColor: '#f45b5b22', fill: false, tension: 0.1 }
]
},
options: {
plugins: {
legend: { display: true, position: 'right' }
},
scales: {
y: { beginAtZero: true }
}
}
});