Total Interest
$${totalInterest.toLocaleString('en-US', {maximumFractionDigits: 2})}
Total Paid
$${totalPayment.toLocaleString('en-US', {maximumFractionDigits: 2})}
`;
// Chart
html += `
`;
// Amortization Table
html += `
Amortization Schedule
| Month |
Principal |
Interest |
Remaining Balance |
${amortization.map(row => `
| ${row.month} |
$${row.principal.toFixed(2)} |
$${row.interest.toFixed(2)} |
$${row.balance.toFixed(2)} |
`).join('')}
`;
html += `
`;
outputDiv.innerHTML = html;
// Render Chart
const ctx = document.getElementById('breakdownChart').getContext('2d');
new Chart(ctx, {
type: 'pie',
data: {
labels: ['Principal', 'Total Interest'],
datasets: [{
data: [loanAmount, totalInterest],
backgroundColor: ['#10B981', '#EF4444'],
}]
},
options: {
responsive: true,
plugins: {
legend: { position: 'top' },
title: { display: true, text: 'Principal vs. Interest' }
}
}
});
}
// --- PDF Download ---
function downloadPDF() {
const { jsPDF } = window.jspdf;
const loader = document.getElementById('loader');
loader.style.display = 'block';
const content = document.getElementById('pdf-content');
html2canvas(content, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgWidth = pdfWidth - 20;
const imgHeight = canvas.height * imgWidth / canvas.width;
let heightLeft = imgHeight;
let position = 10;
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdf.internal.pageSize.getHeight() - 20);
while (heightLeft > 0) {
position = heightLeft - imgHeight + 10;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdf.internal.pageSize.getHeight() - 20);
}
pdf.save('Mortgage_Breakdown.pdf');
loader.style.display = 'none';
});
}