`;
}
function displayResults() {
const category = getScoreCategory(state.score);
const resultsHTML = `
`;
resultsContentEl.innerHTML = resultsHTML;
pdfDownloadAreaEl.classList.remove('hidden');
// Render the chart
const ctx = document.getElementById('scoreChart').getContext('2d');
if (chartInstance) chartInstance.destroy();
chartInstance = new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [{
data: [state.score, 100 - state.score],
backgroundColor: [category.color.replace('text-', '').replace('-500', ''), '#e5e7eb'],
borderColor: ['#fff'],
borderWidth: 4,
circumference: 270,
rotation: 225,
}]
},
options: {
responsive: true,
cutout: '75%',
plugins: { tooltip: { enabled: false }, legend: { display: false } }
}
});
// Add score text inside the doughnut
const scoreText = document.createElement('div');
scoreText.className = `absolute inset-0 flex flex-col items-center justify-center pointer-events-none`;
scoreText.innerHTML = `${state.score}${category.text}`;
ctx.canvas.parentNode.appendChild(scoreText);
}
function renderMarkdown(text) {
// Simple markdown to HTML conversion
return text
.replace(/^### (.*$)/gim, '')
.replace(/<\/li>/, '') // remove first junk tag
.replace(/$/, ' ') // close last tag
.replace(//g, ' ✓')
.replace(/\*\*(.*)\*\*/g, '$1')
.replace(/\n/g, '
'); } function createPdf() { const pdfArea = document.getElementById('pdf-export-area'); if (!pdfArea) { return; } const originalButtonText = downloadPdfButton.textContent; downloadPdfButton.textContent = 'Generating...'; downloadPdfButton.disabled = true; html2canvas(pdfArea, { scale: 2, useCORS: true, logging: false }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgWidth = pdfWidth - 20; const imgHeight = (canvas.height * imgWidth) / canvas.width; let heightLeft = imgHeight; let position = 15; pdf.setFont('helvetica', 'bold'); pdf.setFontSize(18); pdf.text('Personal Finance Health Report', pdfWidth / 2, 10, { align: 'center' }); pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - position - 15); while (heightLeft > 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight); heightLeft -= pdfHeight; } pdf.save('Finance_Health_Report.pdf'); }).catch(err => { console.error("PDF generation error:", err); alert("An error occurred while creating the PDF."); }).finally(() => { downloadPdfButton.textContent = originalButtonText; downloadPdfButton.disabled = false; }); } // --- Initial Setup --- updateUI(); });
Your Financial Health Score
Savings Rate:
${state.savingsRate.toFixed(1)}%
Debt-to-Income Ratio:
${state.debtToIncomeRatio.toFixed(1)}%
Emergency Fund:
${state.emergencyFundMonths.toFixed(1)} months
${renderMarkdown(state.aiRecommendations)}
This tool provides a simplified, illustrative analysis for educational purposes and is not financial advice. Consult with a qualified professional for personalized guidance.
$1
') .replace(/\n\s*-\s/g, ''); } function createPdf() { const pdfArea = document.getElementById('pdf-export-area'); if (!pdfArea) { return; } const originalButtonText = downloadPdfButton.textContent; downloadPdfButton.textContent = 'Generating...'; downloadPdfButton.disabled = true; html2canvas(pdfArea, { scale: 2, useCORS: true, logging: false }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgWidth = pdfWidth - 20; const imgHeight = (canvas.height * imgWidth) / canvas.width; let heightLeft = imgHeight; let position = 15; pdf.setFont('helvetica', 'bold'); pdf.setFontSize(18); pdf.text('Personal Finance Health Report', pdfWidth / 2, 10, { align: 'center' }); pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - position - 15); while (heightLeft > 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight); heightLeft -= pdfHeight; } pdf.save('Finance_Health_Report.pdf'); }).catch(err => { console.error("PDF generation error:", err); alert("An error occurred while creating the PDF."); }).finally(() => { downloadPdfButton.textContent = originalButtonText; downloadPdfButton.disabled = false; }); } // --- Initial Setup --- updateUI(); });