${'⭐'.repeat(entry.mood)}
${entry.text}
`; container.appendChild(entryDiv); }); } function renderMoodChart() { if (state.chart) state.chart.destroy(); if (state.entries.length === 0) return; const ctx = document.getElementById('mood-chart').getContext('2d'); state.chart = new Chart(ctx, { type: 'line', data: { datasets: [{ label: 'Mood Rating', data: state.entries.map(e => ({ x: e.date, y: e.mood })), borderColor: '#3b82f6', backgroundColor: 'rgba(59, 130, 246, 0.1)', tension: 0.1, fill: true, }] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'time', time: { unit: 'day', tooltipFormat: 'MMM d, yyyy' } }, y: { beginAtZero: true, max: 5, min: 1, ticks: { stepSize: 1 } } } } }); } // --- PDF Generation --- async function generatePDF() { const { jsPDF } = window.jspdf; const originalButtonText = downloadPdfBtn.textContent; downloadPdfBtn.textContent = 'Generating...'; downloadPdfBtn.disabled = true; const pdfWrapper = document.createElement('div'); pdfWrapper.style.position = 'absolute'; pdfWrapper.style.left = '-9999px'; pdfWrapper.style.top = '0'; pdfWrapper.style.width = '800px'; pdfWrapper.style.backgroundColor = 'white'; pdfWrapper.className = 'p-8'; let chartImageHtml = 'No chart data available.
'; if (state.chart) { chartImageHtml = `Mental Health Journal
${new Date().toLocaleDateString()}
Mood Trend
${chartImageHtml}
Journal Entries
${document.getElementById('journal-history').innerHTML}
`;
document.body.appendChild(pdfWrapper);
try {
const canvas = await html2canvas(pdfWrapper, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const ratio = imgWidth / imgHeight;
let finalImgWidth = pdfWidth;
let finalImgHeight = pdfWidth / ratio;
if (finalImgHeight > pdfHeight) {
finalImgHeight = pdfHeight;
finalImgWidth = pdfHeight * ratio;
}
pdf.addImage(imgData, 'PNG', 0, 0, finalImgWidth, finalImgHeight);
pdf.save('Mental-Health-Journal.pdf');
} catch (error) {
console.error("PDF Generation Error:", error);
} finally {
document.body.removeChild(pdfWrapper);
downloadPdfBtn.textContent = 'Download PDF Report';
downloadPdfBtn.disabled = false;
}
}
// --- Event Listeners ---
document.getElementById('save-entry-btn').addEventListener('click', saveEntry);
downloadPdfBtn.addEventListener('click', generatePDF);
// --- Initial Call ---
loadData();
initializeEntryForm();
updateDashboard();
showTab(0);
});
