// 页面加载完成 document.addEventListener('DOMContentLoaded', function() { // 懒加载图片 const lazyImages = document.querySelectorAll('img[data-src]'); const imageObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; img.classList.add('loaded'); observer.unobserve(img); } }); }); lazyImages.forEach(img => imageObserver.observe(img)); // 懒加载内容 const lazyElements = document.querySelectorAll('.lazy-load'); const elementObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('loaded'); observer.unobserve(entry.target); } }); }, { threshold: 0.1 }); lazyElements.forEach(el => elementObserver.observe(el)); }); // 初始化图表 function initChart(ctx, config) { return new Chart(ctx, config); } // 格式化数字 function formatNumber(num, decimals = 0) { if (num >= 100000000) { return (num / 100000000).toFixed(decimals) + '亿'; } else if (num >= 10000) { return (num / 10000).toFixed(decimals) + '万'; } return num.toLocaleString('zh-CN', { maximumFractionDigits: decimals }); } // 格式化百分比 function formatPercent(value, decimals = 1) { return (value * 100).toFixed(decimals) + '%'; } // 创建趋势指示器 function createTrendIndicator(value) { const isPositive = value >= 0; const icon = isPositive ? '↑' : '↓'; const className = isPositive ? 'stat-change positive' : 'stat-change negative'; return `${icon} ${Math.abs(value).toFixed(1)}%`; } // 响应式图表配置 function getResponsiveConfig(baseConfig) { return { ...baseConfig, options: { ...baseConfig.options, responsive: true, maintainAspectRatio: false, plugins: { ...baseConfig.options?.plugins, legend: { ...baseConfig.options?.plugins?.legend, display: window.innerWidth > 768 } } } }; }