`;
}).join('');
driversEl.innerHTML = driversHTML;
if(window.lucide) window.lucide.createIcons();
}
// --- TAB 3: ANALYSIS ---
function populateCountrySelector() {
const selectEl = document.getElementById('country-select');
if(!selectEl) return;
Object.keys(fxData).forEach(key => {
selectEl.innerHTML += ``;
});
selectEl.addEventListener('change', (e) => renderCorrelationChart(e.target.value));
}
function renderCorrelationChart(countryKey) {
const ctx = document.getElementById('correlation-chart')?.getContext('2d');
if (!ctx) return;
const country = fxData[countryKey];
const reserveData = country.histReserves.map(p => ({ x: new Date(p.t), y: p.y }));
const vixData = stabilityData.histVix.map(p => ({ x: new Date(p.t), y: p.y }));
if (correlationChart) correlationChart.destroy();
correlationChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{ label: `${country.name} Reserves (USD Billions)`, data: reserveData, yAxisID: 'yReserves', borderColor: '#059669', backgroundColor: '#05966933' },
{ label: 'VIX Index', data: vixData, yAxisID: 'yVix', borderColor: '#f59e0b', backgroundColor: '#f59e0b33' }
]
},
options: {
responsive: true, maintainAspectRatio: false,
scales: {
x: { type: 'time', time: { unit: 'year' } },
yReserves: { type: 'linear', position: 'left', title: { display: true, text: 'FX Reserves ($B)', color: '#059669' } },
yVix: { type: 'linear', position: 'right', title: { display: true, text: 'VIX Level', color: '#f59e0b' }, grid: { drawOnChartArea: false } }
}
}
});
}
// --- PDF DOWNLOAD ---
window.downloadPDF = async () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'l', unit: 'mm', format: 'a4' });
const contentEl = document.getElementById(`tab-${currentTab}`);
if (!contentEl) return;
const btn = document.querySelector('button[onclick="downloadPDF()"]');
const originalHTML = btn.innerHTML;
btn.innerHTML = 'Generating...';
btn.disabled = true;
try {
const canvas = await html2canvas(contentEl, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, imgHeight);
pdf.save(`FX-Reserves-Stability-${currentTab}.pdf`);
} catch (error) {
console.error("PDF generation error:", error);
alert("Could not generate PDF. Please try again.");
} finally {
btn.innerHTML = originalHTML;
btn.disabled = false;
if(window.lucide) window.lucide.createIcons();
}
};
// --- KICK IT OFF ---
init();
});