ECharts 关闭非必要特效性能优化指南
文档类型: 实战指南
难度等级: ⭐⭐
源码版本: ECharts 5.x
本文行数: 约400行
📋 目录
🎯 特效性能开销分析
主要性能杀手
| 特效 | 性能开销 | 影响场景 | 建议 |
|---|---|---|---|
| 阴影(shadow) | ⭐⭐⭐⭐⭐ | 大数据量 | ❌ 关闭 |
| 模糊(blur) | ⭐⭐⭐⭐⭐ | 动画过程 | ❌ 关闭 |
| 渐变(gradient) | ⭐⭐⭐⭐ | 大量元素 | ⚠️ 简化 |
| 光泽(glow) | ⭐⭐⭐⭐ | 实时数据 | ❌ 关闭 |
| 复杂tooltip | ⭐⭐⭐ | 频繁交互 | ⚠️ 优化 |
| emphasis高亮 | ⭐⭐ | 悬停效果 | ✅ 保留 |
⚙️ 关键配置项详解
1. 关闭阴影
typescript
// ❌ 性能差 - 启用阴影
series: [{
type: 'bar',
itemStyle: {
shadowBlur: 10,
shadowColor: 'rgba(0, 0, 0, 0.5)',
shadowOffsetX: 5,
shadowOffsetY: 5
}
}]
// ✅ 性能好 - 关闭阴影
series: [{
type: 'bar',
itemStyle: {
shadowBlur: 0 // 关键: 设置为0
}
}]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
性能提升: 30-50% (大数据量场景)
2. 关闭模糊效果
typescript
// ❌ 性能差 - 启用模糊
series: [{
type: 'scatter',
blurSize: 20,
minOpacity: 0.2
}]
// ✅ 性能好 - 关闭模糊
series: [{
type: 'scatter',
blurSize: 0
}]1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
3. 简化渐变
typescript
// ❌ 性能差 - 复杂渐变
series: [{
type: 'bar',
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#83bff6' },
{ offset: 0.5, color: '#188df0' },
{ offset: 1, color: '#188df0' }
])
}
}]
// ✅ 性能好 - 纯色
series: [{
type: 'bar',
itemStyle: {
color: '#188df0'
}
}]
// ⚠️ 折中方案 - 简单渐变
series: [{
type: 'bar',
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#83bff6' },
{ offset: 1, color: '#188df0' }
])
}
}]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
性能提升: 20-40% (大量柱状图场景)
4. 禁用悬停动画
typescript
// ❌ 性能差 - 启用悬停动画
series: [{
type: 'scatter',
emphasis: {
scale: true, // 放大效果
focus: 'series' // 聚焦系列
}
}]
// ✅ 性能好 - 禁用悬停动画
series: [{
type: 'scatter',
emphasis: {
disabled: true // 完全禁用
}
}]
// ⚠️ 折中方案 - 简化悬停效果
series: [{
type: 'scatter',
emphasis: {
scale: false, // 不放大
focus: 'none', // 不聚焦
itemStyle: {
opacity: 0.8 // 仅调整透明度
}
}
}]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
性能提升: 15-25% (频繁交互场景)
5. 优化Tooltip
typescript
// ❌ 性能差 - 复杂tooltip
tooltip: {
trigger: 'axis',
formatter: (params: any) => {
let html = '<div style="padding: 20px;">';
params.forEach((param: any) => {
html += `
<div style="margin: 10px 0;">
<span style="color: ${param.color}">●</span>
${param.seriesName}: ${param.value}
</div>
`;
});
html += '</div>';
return html;
},
backgroundColor: 'rgba(255, 255, 255, 0.9)',
extraCssText: 'box-shadow: 0 0 10px rgba(0,0,0,0.3);'
}
// ✅ 性能好 - 简化tooltip
tooltip: {
trigger: 'axis',
confine: true, // 限制在图表区域内
renderMode: 'html', // 使用HTML渲染
formatter: '{b0}: {c0}' // 简单字符串模板
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
性能提升: 10-20% (频繁hover场景)
🚀 场景化优化方案
场景1: 实时监控大屏
typescript
const realtimeOption = {
// 全局关闭动画
animation: false,
series: [{
type: 'line',
data: realtimeData,
// 关闭所有特效
itemStyle: {
shadowBlur: 0
},
lineStyle: {
width: 1 // 细线条
},
// 禁用悬停
emphasis: {
disabled: true
},
// 简化tooltip
tooltip: {
show: false // 实时监控通常不需要tooltip
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
预期FPS: 55-60
场景2: 电商数据大屏
typescript
const ecommerceOption = {
animation: true,
animationDuration: 1000,
series: [{
type: 'bar',
data: salesData,
// 保留必要的视觉效果
itemStyle: {
color: '#5470C6',
shadowBlur: 5, // 轻微阴影
shadowColor: 'rgba(84, 112, 198, 0.3)'
},
// 简化悬停效果
emphasis: {
scale: false,
itemStyle: {
color: '#91cc75' // 仅变色
}
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
预期FPS: 45-55
场景3: 金融K线图
typescript
const financialOption = {
animation: false, // 关闭动画
series: [{
type: 'candlestick',
data: klineData,
// 极简样式
itemStyle: {
color: '#ef232a', // 涨
color0: '#14b143', // 跌
borderColor: undefined,
borderWidth: 1
},
// 禁用所有特效
emphasis: {
disabled: true
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
预期FPS: 58-60
场景4: 移动端H5页面
typescript
const mobileOption = {
animation: true,
animationDuration: 500, // 缩短动画时间
series: [{
type: 'pie',
data: pieData,
// 移动端优化
itemStyle: {
shadowBlur: 0 // 关闭阴影
},
// 简化交互
emphasis: {
scale: false,
itemStyle: {
opacity: 0.9
}
}
}],
// 简化tooltip
tooltip: {
confine: true,
textStyle: {
fontSize: 12 // 小字体
}
}
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
预期FPS: 50-60
📊 性能监控与测试
性能监控工具
typescript
class ChartPerformanceMonitor {
private frameCount = 0;
private lastTime = performance.now();
private fpsHistory: number[] = [];
start() {
const measure = () => {
this.frameCount++;
const currentTime = performance.now();
if (currentTime - this.lastTime >= 1000) {
const fps = this.frameCount;
this.fpsHistory.push(fps);
console.log(`FPS: ${fps} | Avg: ${this.getAverageFPS()}`);
if (fps < 30) {
console.warn('⚠️ FPS过低,建议关闭更多特效');
}
this.frameCount = 0;
this.lastTime = currentTime;
}
requestAnimationFrame(measure);
};
requestAnimationFrame(measure);
}
getAverageFPS(): number {
if (this.fpsHistory.length === 0) return 0;
const sum = this.fpsHistory.reduce((a, b) => a + b, 0);
return Math.round(sum / this.fpsHistory.length);
}
getReport(): string {
return `
性能报告:
- 当前FPS: ${this.frameCount}
- 平均FPS: ${this.getAverageFPS()}
- 采样次数: ${this.fpsHistory.length}
`;
}
}
// 使用
const monitor = new ChartPerformanceMonitor();
monitor.start();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
性能对比测试
typescript
interface TestConfig {
name: string;
options: any;
}
const tests: TestConfig[] = [
{
name: '完整特效',
options: {
series: [{
type: 'bar',
itemStyle: {
shadowBlur: 10,
shadowColor: 'rgba(0,0,0,0.5)'
},
emphasis: {
scale: true
}
}]
}
},
{
name: '优化后',
options: {
series: [{
type: 'bar',
itemStyle: {
shadowBlur: 0
},
emphasis: {
disabled: true
}
}]
}
}
];
// 运行测试
tests.forEach(test => {
console.group(test.name);
chart.setOption(test.options);
setTimeout(() => {
console.log(monitor.getReport());
console.groupEnd();
}, 3000);
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
典型测试结果 (1000个柱状图):
| 配置 | FPS | 内存(MB) | 提升 |
|---|---|---|---|
| 完整特效 | 35 | 180 | baseline |
| 优化后 | 58 | 120 | 65%↑ |
🎯 终极优化清单
必须关闭的特效 (高性能场景)
- [ ]
shadowBlur: 0- 关闭阴影 - [ ]
emphasis.disabled: true- 禁用悬停 - [ ]
animation: false- 关闭动画 (实时数据) - [ ]
tooltip.show: false- 隐藏tooltip (可选)
可以保留的特效 (中等性能场景)
- [ ] 简单渐变 (2色以内)
- [ ] 轻微阴影 (
shadowBlur <= 5) - [ ] 简化悬停 (仅变色/透明度)
推荐保留的特效 (低数据量场景)
- [ ] 弹性动画 (首次渲染)
- [ ] 完整的tooltip
- [ ] emphasis高亮效果
🔗 相关资源
上一篇: 渐进式渲染
下一篇: 内存管理-dispose
