/** * 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 { // Instance Growth Chart const instanceCanvas = document.getElementById('instanceGrowthChart'); if (!instanceCanvas) { console.error('Instance growth chart canvas not found'); return; } const instanceCtx = instanceCanvas.getContext('2d'); this.charts.instanceGrowth = new Chart(instanceCtx, { type: 'line', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' } }, scales: { y: { beginAtZero: true, title: { display: true, text: 'Total Instances' } }, x: { title: { display: true, text: 'Month' } } } } }); // Revenue Chart const revenueCanvas = document.getElementById('revenueChart'); if (!revenueCanvas) { console.error('Revenue chart canvas not found'); return; } const revenueCtx = revenueCanvas.getContext('2d'); this.charts.revenue = new Chart(revenueCtx, { type: 'line', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' } }, scales: { y: { beginAtZero: true, title: { display: true, text: 'Cumulative Revenue (CHF)' } }, x: { title: { display: true, text: 'Month' } } } } }); // Cash Flow Chart const cashFlowCanvas = document.getElementById('cashFlowChart'); if (!cashFlowCanvas) { console.error('Cash flow chart canvas not found'); return; } const cashFlowCtx = cashFlowCanvas.getContext('2d'); this.charts.cashFlow = new Chart(cashFlowCtx, { type: 'bar', data: { labels: [], datasets: [] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' } }, scales: { y: { title: { display: true, text: 'Monthly Cash Flow (CHF)' } }, x: { title: { display: true, text: 'Month' } } } } }); } 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 = `