/** * Chart Management Module * Handles Chart.js initialization and updates */ class ChartManager { constructor(calculator) { this.calculator = calculator; this.charts = {}; } initializeCharts() { // Check if Chart.js is available if (typeof Chart === 'undefined') { console.error('Chart.js library not loaded. Charts will not be available.'); this.showChartError('Chart.js library failed to load. Please refresh the page.'); return; } try { // ROI Progression Chart (replaces Instance Growth Chart) const roiCanvas = document.getElementById('instanceGrowthChart'); if (!roiCanvas) { console.error('ROI progression chart canvas not found'); return; } const roiCtx = roiCanvas.getContext('2d'); this.charts.roiProgression = new Chart(roiCtx, { type: 'line', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' }, title: { display: true, text: 'ROI Progression Over Time' } }, scales: { y: { title: { display: true, text: 'ROI (%)' }, grid: { color: function(context) { return context.tick.value === 0 ? 'rgba(0,0,0,0.5)' : 'rgba(0,0,0,0.1)'; } } }, x: { title: { display: true, text: 'Month' } } } } }); // Net Position Chart (replaces simple Revenue Chart) const netPositionCanvas = document.getElementById('revenueChart'); if (!netPositionCanvas) { console.error('Net position chart canvas not found'); return; } const netPositionCtx = netPositionCanvas.getContext('2d'); this.charts.netPosition = new Chart(netPositionCtx, { type: 'line', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' }, title: { display: true, text: 'Net Financial Position (Break-Even Analysis)' } }, scales: { y: { title: { display: true, text: 'Net Position (CHF)' }, grid: { color: function(context) { return context.tick.value === 0 ? 'rgba(0,0,0,0.8)' : 'rgba(0,0,0,0.1)'; } } }, x: { title: { display: true, text: 'Month' } } } } }); // Model Comparison Chart (replaces generic Cash Flow Chart) const modelComparisonCanvas = document.getElementById('cashFlowChart'); if (!modelComparisonCanvas) { console.error('Model comparison chart canvas not found'); return; } const modelComparisonCtx = modelComparisonCanvas.getContext('2d'); this.charts.modelComparison = new Chart(modelComparisonCtx, { type: 'bar', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' }, title: { display: true, text: 'Investment Model Performance Comparison' } }, scales: { y: { beginAtZero: true, title: { display: true, text: 'Total Return (CHF)' } }, x: { title: { display: true, text: 'Growth Scenario' } } } } }); } catch (error) { console.error('Error initializing charts:', error); this.showChartError('Failed to initialize charts. Please refresh the page.'); } } showChartError(message) { // Show error message in place of charts const chartContainers = ['instanceGrowthChart', 'revenueChart', 'cashFlowChart']; chartContainers.forEach(containerId => { const container = document.getElementById(containerId); if (container) { container.style.display = 'flex'; container.style.justifyContent = 'center'; container.style.alignItems = 'center'; container.style.minHeight = '300px'; container.style.backgroundColor = '#f8f9fa'; container.style.border = '1px solid #dee2e6'; container.style.borderRadius = '4px'; container.innerHTML = `