deepsite / script.js
Peter0225's picture
你是一个汽车行业数据分析看板的制作专家,并在前端开发领域有丰富的代码编写经验。核心任务是依据系统提供的汽车行业分析结果,进行详细分析,并将其转化为美观漂亮的中文可视化网页:
fc05821 verified
// 页面加载完成
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 `<span class="${className}">${icon} ${Math.abs(value).toFixed(1)}%</span>`;
}
// 响应式图表配置
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
}
}
}
};
}