ECharts 速查手册
📋 概述
快速查阅ECharts常用配置、API和方法的速查手册,适合日常开发时快速参考。
🎯 快速开始
最小可用示例
typescript
import * as echarts from 'echarts';
const chart = echarts.init(document.getElementById('main'));
chart.setOption({
xAxis: { type: 'category', data: ['A', 'B', 'C'] },
yAxis: { type: 'value' },
series: [{ data: [10, 20, 30], type: 'bar' }]
});1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🔧 常用图表配置
折线图
typescript
{
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed'] },
yAxis: { type: 'value' },
series: [{
data: [150, 230, 224],
type: 'line',
smooth: true, // 平滑曲线
symbol: 'circle', // 数据点形状
symbolSize: 8, // 数据点大小
lineStyle: { width: 2 }, // 线条样式
areaStyle: { // 面积图
opacity: 0.3
}
}]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
柱状图
typescript
{
xAxis: { type: 'category', data: ['A', 'B', 'C'] },
yAxis: { type: 'value' },
series: [{
data: [120, 200, 150],
type: 'bar',
barWidth: '60%', // 柱子宽度
itemStyle: {
borderRadius: [5, 5, 0, 0] // 圆角
}
}]
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
饼图
typescript
{
series: [{
type: 'pie',
radius: ['40%', '70%'], // 环形图
avoidLabelOverlap: false,
data: [
{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' }
],
label: {
show: true,
formatter: '{b}: {d}%' // 显示百分比
}
}]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
散点图
typescript
{
xAxis: {},
yAxis: {},
series: [{
type: 'scatter',
data: [[10, 20], [30, 40], [50, 60]],
symbolSize: 20,
itemStyle: {
color: '#5470c6'
}
}]
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
🎨 常用样式配置
颜色主题
typescript
// 方法1:使用内置主题
const chart = echarts.init(container, 'dark');
// 方法2:自定义配色
{
color: ['#5470c6', '#91cc75', '#fac858', '#ee6666'],
series: [{
itemStyle: {
color: '#5470c6'
}
}]
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
标题和图例
typescript
{
title: {
text: '主标题',
subtext: '副标题',
left: 'center',
textStyle: { fontSize: 18 }
},
legend: {
data: ['系列1', '系列2'],
orient: 'vertical', // 垂直排列
right: 10,
top: 'center'
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
提示框(Tooltip)
typescript
{
tooltip: {
trigger: 'axis', // 'item' | 'axis' | 'none'
axisPointer: {
type: 'cross' // 'line' | 'shadow' | 'cross'
},
formatter: (params) => {
return `${params[0].name}: ${params[0].value}`;
}
}
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
📐 布局配置
Grid(直角坐标系网格)
typescript
{
grid: {
left: '3%',
right: '4%',
bottom: '3%',
top: '10%',
containLabel: true // 包含坐标轴标签
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
DataZoom(数据区域缩放)
typescript
{
dataZoom: [
{
type: 'slider', // 滑动条
start: 0, // 起始百分比
end: 100 // 结束百分比
},
{
type: 'inside', // 内部缩放(鼠标滚轮)
start: 0,
end: 100
}
]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
🎭 动画配置
typescript
{
animation: true,
animationDuration: 1000, // 动画时长(毫秒)
animationEasing: 'cubicOut', // 缓动函数
animationDelay: 0 // 延迟时间
}1
2
3
4
5
6
2
3
4
5
6
常用缓动函数:
linearcubicIn/cubicOut/cubicInOutelasticIn/elasticOutbounceIn/bounceOut
🖱️ 交互事件
事件绑定
typescript
// 点击事件
chart.on('click', (params) => {
console.log(params.name, params.value);
});
// 悬停事件
chart.on('mouseover', (params) => {
console.log('Hover:', params);
});
// 数据区域缩放
chart.on('datazoom', (params) => {
console.log('Zoom:', params.start, params.end);
});
// 图例选择
chart.on('legendselectchanged', (params) => {
console.log('Selected:', params.selected);
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
常用事件类型
| 事件名 | 触发时机 |
|---|---|
click | 鼠标点击 |
dblclick | 双击 |
mouseover | 鼠标悬停 |
mouseout | 鼠标移出 |
datazoom | 数据缩放 |
legendselectchanged | 图例选择变化 |
restore | 重置 |
rendered | 渲染完成 |
⚙️ 常用API
实例方法
typescript
// 设置/更新配置
chart.setOption(option, { notMerge: false });
// 调整大小
chart.resize({ width: 800, height: 600 });
// 获取配置
const option = chart.getOption();
// 导出图片
const url = chart.getDataURL({
type: 'png',
pixelRatio: 2,
backgroundColor: '#fff'
});
// 下载图片
chart.saveAsImage({
type: 'png',
name: 'chart',
pixelRatio: 2
});
// 清空图表
chart.clear();
// 销毁实例
chart.dispose();
// 显示加载动画
chart.showLoading({
text: '加载中...',
color: '#c23531',
textColor: '#000',
maskColor: 'rgba(255, 255, 255, 0.8)'
});
// 隐藏加载动画
chart.hideLoading();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
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
静态方法
typescript
// 注册主题
echarts.registerTheme('my-theme', themeConfig);
// 注册地图
echarts.registerMap('china', chinaGeoJson);
// 注册图表类型
echarts.registerChart(type, clazz);
// 颜色工具
echarts.color.lift('#5470c6', 0.2); // 提亮
echarts.color.darken('#5470c6', 0.2); // 变暗1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
🗺️ 地图配置
typescript
{
geo: {
map: 'china',
roam: true, // 允许缩放拖拽
zoom: 1.2,
label: { show: true },
itemStyle: {
areaColor: '#eeeeee',
borderColor: '#999999'
},
emphasis: {
itemStyle: {
areaColor: '#fe994e'
}
}
},
series: [{
type: 'scatter',
coordinateSystem: 'geo',
data: [
{ name: '北京', value: [116.40, 39.90, 100] },
{ name: '上海', value: [121.47, 31.23, 150] }
]
}]
}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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
🌈 视觉映射
typescript
{
visualMap: {
min: 0,
max: 1000,
calculable: true, // 显示滑块
orient: 'horizontal',
left: 'center',
bottom: '5%',
inRange: {
color: ['#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
}
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
📊 响应式配置
typescript
// 监听窗口变化
window.addEventListener('resize', () => {
chart.resize();
});
// 使用媒体查询
const mediaQuery = window.matchMedia('(max-width: 768px)');
mediaQuery.addListener((e) => {
if (e.matches) {
// 移动端配置
chart.setOption(mobileOption);
} else {
// 桌面端配置
chart.setOption(desktopOption);
}
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
🎯 按需引入清单
基础图表
typescript
import * as echarts from 'echarts/core';
import {
LineChart,
BarChart,
PieChart,
ScatterChart
} from 'echarts/charts';
import {
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
echarts.use([
LineChart,
BarChart,
PieChart,
ScatterChart,
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
CanvasRenderer
]);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
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
高级图表
typescript
import {
HeatmapChart,
MapChart,
RadarChart,
GaugeChart,
FunnelChart,
SankeyChart,
GraphChart,
TreeChart,
TreemapChart,
BoxplotChart,
CandlestickChart
} from 'echarts/charts';1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
💻 TypeScript类型
typescript
import type {
EChartsOption,
LineSeriesOption,
BarSeriesOption,
PieSeriesOption,
XAXisOption,
YAXisOption
} from 'echarts';
// 使用示例
const option: EChartsOption = {
// ... 配置
};
const lineOption: LineSeriesOption = {
type: 'line',
data: [1, 2, 3]
};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
🔍 调试技巧
1. 检查配置
typescript
// 在浏览器控制台
console.log(chart.getOption());1
2
2
2. 性能分析
typescript
// 开启性能监控
echarts.init(container, null, {
renderer: 'canvas',
devicePixelRatio: window.devicePixelRatio
});
// 查看渲染时间
performance.mark('chart-start');
chart.setOption(option);
performance.mark('chart-end');
performance.measure('chart-render', 'chart-start', 'chart-end');
console.log(performance.getEntriesByName('chart-render'));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
// 确保正确销毁
componentWillUnmount() {
chart.dispose();
chart = null;
}
// 检查实例数量
console.log(echarts.getInstanceByDom(container));1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
⚡ 性能优化速查
| 优化项 | 方法 | 效果 |
|---|---|---|
| Bundle体积 | 按需引入 | ↓ 80% |
| 大数据量 | sampling: 'lttb' | ↑ 10倍 |
| 频繁更新 | throttle/debounce | ↓ CPU 50% |
| 多个图表 | 虚拟滚动 | ↓ 内存 70% |
| 动画性能 | animation: false | ↑ FPS 20 |
🆘 常见问题
Q1: 图表不显示?
typescript
// 检查容器尺寸
console.log(container.clientWidth, container.clientHeight);
// 确保调用resize
setTimeout(() => chart.resize(), 0);1
2
3
4
5
2
3
4
5
Q2: 数据更新不生效?
typescript
// 使用notMerge强制合并
chart.setOption(newOption, { notMerge: true });1
2
2
Q3: 中文乱码?
typescript
// 确保HTML编码
<meta charset="UTF-8">
// 或在配置中指定
{
textStyle: {
fontFamily: 'Microsoft YaHei'
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
📚 更多资源
- 官方文档: https://echarts.apache.org/zh/option.html
- 示例库: https://echarts.apache.org/examples/zh/index.html
- 社区论坛: https://github.com/apache/echarts/discussions
提示:本速查手册涵盖80%的常用配置,详细配置请参考官方文档。
