Chronic Pain & TENS Therapy Tracker

Chronic Pain & TENS Therapy Tracker

How are you feeling right now?

5

Add New TENS Program

Saved Programs

Your saved TENS programs will appear here.

Your Pain Trends

Log History

Your logged entries will appear here.

Download Your Pain Report

Generate a comprehensive PDF of your pain log history and trends. This can be useful for personal records or for sharing with your healthcare provider.

âš¡ TENS: ${tensSession.name}

` : ''} ${entry.notes ? `

${entry.notes}

` : ''} `; logHistoryContainer.appendChild(entryDiv); }); } function renderChart() { if (state.chart) state.chart.destroy(); if (state.entries.length === 0) return; const tensData = state.entries .filter(e => e.tensSessionId) .map(e => ({ x: e.datetime, y: e.intensity })); const ctx = chartCanvas.getContext('2d'); state.chart = new Chart(ctx, { type: 'line', data: { datasets: [{ label: 'Pain Intensity', data: state.entries.map(e => ({ x: e.datetime, y: e.intensity })), borderColor: '#3b82f6', backgroundColor: 'rgba(59, 130, 246, 0.1)', tension: 0.1, fill: true, }, { type: 'scatter', label: 'TENS Session', data: tensData, backgroundColor: '#8b5cf6', // purple-500 radius: 6, pointStyle: 'triangle', rotation: 180, }] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'time', time: { unit: 'day', tooltipFormat: 'MMM d, yyyy h:mm a' }, title: { display: true, text: 'Date' } }, y: { beginAtZero: true, max: 10, title: { display: true, text: 'Intensity (0-10)' } } } } }); } // --- PDF Generation --- async function generatePDF() { if (state.entries.length === 0) { alert("No entries to report. Please add a pain log entry first."); return; } document.getElementById('pdf-report-date').textContent = new Date().toLocaleString(undefined, { dateStyle: 'long' }); const chartContainer = document.getElementById('pdf-chart-container'); chartContainer.innerHTML = ''; if (state.chart) { const chartImage = new Image(); chartImage.src = state.chart.toBase64Image(); chartImage.style.width = '100%'; chartImage.style.height = '100%'; chartContainer.appendChild(chartImage); } const pdfLogContainer = document.getElementById('pdf-log-history'); pdfLogContainer.innerHTML = ` ${state.entries.map(entry => { const tensSession = state.tensPrograms.find(p => p.id === entry.tensSessionId); return ``; }).join('')}
Date & TimeIntensityLocationTENS SessionNotes
${entry.datetime.toLocaleString(undefined, { dateStyle: 'short', timeStyle: 'short' })} ${entry.intensity} ${entry.location || ''} ${tensSession ? tensSession.name : ''} ${entry.notes || ''}
`; const { jsPDF } = window.jspdf; const originalButtonText = downloadPdfBtn.textContent; downloadPdfBtn.textContent = 'Generating...'; downloadPdfBtn.disabled = true; const content = document.getElementById('pdf-content'); const clone = content.cloneNode(true); clone.style.position = 'absolute'; clone.style.left = '-9999px'; clone.style.top = '0'; clone.style.display = 'block'; document.body.appendChild(clone); try { const canvas = await html2canvas(clone, { 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('Chronic-Pain-Report.pdf'); } catch (error) { console.error("PDF Generation Error:", error); } finally { document.body.removeChild(clone); downloadPdfBtn.textContent = 'Download PDF Report'; downloadPdfBtn.disabled = false; } } // --- Event Listeners --- addEntryBtn.addEventListener('click', addEntry); addTensProgramBtn.addEventListener('click', addTensProgram); downloadPdfBtn.addEventListener('click', generatePDF); // --- Initial Call --- loadData(); initializeForm(); renderTensPrograms(); updateDashboard(); showTab(0); });
Scroll to Top