Caffeine Consumption & Sleep Impact Analyzer
Step 1: Log Your Caffeine Intake and Sleep
Caffeine Intake
Sleep Details
Average is 4-6 hours. Adjust if you feel you metabolize caffeine faster or slower.
Step 2: Your Personalized Analysis
Please fill out your intake and sleep details on the previous tab and click "Analyze" to see your results.
Caffeine Decay Over Time
Step 3: Customize Caffeine Data
Adjust the caffeine content (in mg) for each drink type to match your specific beverages. These values are used in the calculations.
${advice}
`; } /** * Generates and displays the caffeine decay chart. * @param {number} halfLife - The user's caffeine half-life. * @param {string} bedtime - The user's bedtime. */ function generateChart(halfLife, bedtime) { const chartContainer = document.getElementById('results-chart'); if (!chartContainer) return; chartContainer.style.display = 'block'; const ctx = document.getElementById('caffeineChart').getContext('2d'); const labels = []; const dataPoints = []; const totalHours = 24; const timeStep = 1; // 1 hour interval for (let h = 0; h <= totalHours; h += timeStep) { let totalCaffeineNow = 0; state.userIntake.forEach(intake => { const intakeDate = new Date(`1970-01-01T${intake.time}`); const currentDate = new Date(intakeDate.getTime() + h * 60 * 60 * 1000); const hoursSinceThisIntake = (currentDate - intakeDate) / (1000 * 60 * 60); if (hoursSinceThisIntake >= 0) { totalCaffeineNow += intake.amount * Math.pow(0.5, hoursSinceThisIntake / halfLife); } }); const firstIntakeTime = new Date(`1970-01-01T${state.userIntake[0].time}`); const labelTime = new Date(firstIntakeTime.getTime() + h * 60 * 60 * 1000); labels.push(labelTime.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })); dataPoints.push(totalCaffeineNow.toFixed(2)); } if (state.chartInstance) { state.chartInstance.destroy(); } state.chartInstance = new Chart(ctx, { type: 'line', data: { labels: labels, datasets: [{ label: 'Caffeine in System (mg)', data: dataPoints, borderColor: '#3B82F6', backgroundColor: 'rgba(59, 130, 246, 0.1)', fill: true, tension: 0.4, }] }, options: { responsive: true, maintainAspectRatio: false, scales: { y: { beginAtZero: true, title: { display: true, text: 'Caffeine (mg)' } }, x: { title: { display: true, text: 'Time' } } }, plugins: { tooltip: { mode: 'index', intersect: false, }, title: { display: true, text: 'Caffeine Decay from First Drink' } } } }); } /** * Generates and triggers the download of a beautifully formatted PDF report. * This function is completely rewritten to handle page breaks correctly. */ window.downloadPDF = async () => { const { jsPDF } = window.jspdf; if (!state.chartInstance || state.userIntake.length === 0) { alert("Please run an analysis first to generate a report."); return; } // 1. Get all necessary data for the report const bedtime = document.getElementById('bedtime').value; const halfLife = document.getElementById('caffeine-half-life').value; const chartImage = state.chartInstance.toBase64Image(); const summaryNode = document.getElementById('results-summary').cloneNode(true); const summaryTitle = summaryNode.querySelector('h3'); if (summaryTitle) summaryTitle.classList.remove('font-bold'); // 2. Generate HTML table rows from intake data const intakeRows = state.userIntake.map(intake => `Caffeine & Sleep Analysis Report
Generated: ${new Date().toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'short' })}
User Parameters
Typical Bedtime: ${new Date('1970-01-01T' + bedtime).toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })}
Caffeine Half-Life: ${halfLife} hours
Analysis Findings
${summaryNode.innerHTML}Caffeine Intake Log
| Drink Type | Amount | Time Consumed |
|---|
Caffeine Decay Visualization
This report is an estimation based on provided data and average metabolic rates. It is not a substitute for professional medical advice.
Report generated by the Caffeine Consumption & Sleep Impact Analyzer.
