Real-Time Economic Policy Uncertainty Index Tracker

Economic Policy Uncertainty Index Tracker

Visualizing EPU trends for selected regions.

Data table container not found.

"; } pdfReportDateElem.textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); // 3. Generate PDF from the prepared HTML container // Temporarily show the container for html2canvas to render it pdfOutputContainer.style.display = 'block'; pdfOutputContainer.style.opacity = '0'; // Keep it invisible to user pdfOutputContainer.style.position = 'fixed'; pdfOutputContainer.style.left = '-9999px'; // Move off-screen try { const canvas = await html2canvas(pdfOutputContainer, { scale: 2, // Increase scale for better resolution useCORS: true, logging: false, onclone: (document) => { // Ensure styles are applied in the cloned document const clonedContainer = document.getElementById('pdfOutputContainer'); if (clonedContainer) { clonedContainer.style.opacity = '1'; } } }); const imgData = canvas.toDataURL('image/png'); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgProps = pdf.getImageProperties(imgData); const imgWidth = pdfWidth * 0.9; // Use 90% of page width const imgHeight = (imgProps.height * imgWidth) / imgProps.width; let heightLeft = imgHeight; let position = pdfHeight * 0.05; // Top margin pdf.addImage(imgData, 'PNG', pdfWidth * 0.05, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - (pdfHeight * 0.1)); // Subtract usable page height (with margins) while (heightLeft >= 0) { position = heightLeft - imgHeight + (pdfHeight * 0.05) ; // Adjust position for next page pdf.addPage(); pdf.addImage(imgData, 'PNG', pdfWidth * 0.05, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - (pdfHeight * 0.1)); } const selectedCountry = countrySelect ? countrySelect.value : "EPU"; pdf.save(`${selectedCountry}_EPU_Report_${new Date().toISOString().split('T')[0]}.pdf`); showMessage('PDF downloaded successfully!'); } catch (error) { console.error("Error generating PDF: ", error); showMessage('Failed to generate PDF. See console for details.', 'error'); } finally { // Hide the container again pdfOutputContainer.style.display = 'none'; pdfOutputContainer.style.opacity = '1'; pdfOutputContainer.style.position = 'static'; pdfOutputContainer.style.left = '0'; // Clear temporary content pdfChartImageElem.src = ""; pdfTableContainer.innerHTML = ""; } } // --- Event Listener Assignments (DOM Ready) --- document.addEventListener('DOMContentLoaded', () => { // Assign DOM elements countrySelect = document.getElementById('countrySelect'); startDateInput = document.getElementById('startDate'); endDateInput = document.getElementById('endDate'); updateChartButton = document.getElementById('updateChartButton'); downloadPdfButton = document.getElementById('downloadPdfButton'); lastDataUpdateP = document.getElementById('lastDataUpdate'); chartSummaryDiv = document.getElementById('chartSummary'); prevTabButton = document.getElementById('prevTabButton'); nextTabButton = document.getElementById('nextTabButton'); // Null checks for critical elements if (!countrySelect || !startDateInput || !endDateInput || !updateChartButton || !downloadPdfButton || !lastDataUpdateP || !prevTabButton || !nextTabButton || !chartSummaryDiv) { console.error("One or more critical UI elements could not be found. Tool may not function correctly."); showMessage("Initialization Error: UI elements missing. Please check console.", "error"); return; // Prevent further execution if essential elements are missing } // Populate controls populateCountrySelect(); setInitialDateRange(); // Update "last data update" text if (lastDataUpdateP && epuDataStore.lastUpdated) { lastDataUpdateP.textContent = `Embedded data snapshot last updated: ${epuDataStore.lastUpdated}.`; } // Initial chart load (optional, or trigger on button click) // To avoid immediate error messages if no country is pre-selected, let's make initial call conditional if (countrySelect.options.length > 0) { countrySelect.selectedIndex = 0; // Select the first country by default updateChart(); // Load chart for the first country and default dates } else { showMessage("No countries available in the data store.", "error"); } // Attach event listeners if (updateChartButton) { updateChartButton.addEventListener('click', updateChart); } else { console.error("Update chart button not found"); } if (downloadPdfButton) { downloadPdfButton.addEventListener('click', downloadPDF); } else { console.error("Download PDF button not found"); } // Initialize tab navigation state changeTab(null, tabs[0]); // Show the first tab by default });
Scroll to Top