‹
ECharts 图表
概念 一句话理解
option 配置对象,所有图表的「说明书」
xAxis / yAxis 坐标轴(X 轴 / Y 轴)
series 数据系列,真正的「数据 + 图表类型」
grid 直角坐标系的容器(位置、大小)
tooltip 悬浮提示框
legend 图例
echarts.init(dom) 初始化,绑定到一个 DOM 元素
setOption() 设置 / 更新配置
resize() 容器尺寸变化时调用
dispose() 销毁实例,释放内存
import * as echarts from 'echarts/core'
import { BarChart, LineChart, PieChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'
import {
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent
} from 'echarts/components'
​
echarts.use([
BarChart, LineChart, PieChart, // 图表
GridComponent, TooltipComponent, // 组件
LegendComponent, TitleComponent,
CanvasRenderer // 渲染器
])
// 最小示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<!-- 容器必须有明确的宽高 -->
<div id="chart" style="width: 600px; height: 400px;"></div>
​
<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js" />
<script>
// 1. 初始化:绑定到 DOM
const chart = echarts.init(document.getElementById('chart'));
​
// 2. 配置
const option = {
xAxis: { type: 'category', data: ['周一', '周二', '周三'] },
yAxis: { type: 'value' },
series: [{ type: 'bar', data: [120, 200, 150] }]
};
​
// 3. 渲染
chart.setOption(option);
​
// 4. 容器变化时自适应
window.addEventListener('resize', () => chart.resize());
</script>
</body>
</html>
// 柱状图
series: [{
type: 'bar',
data: [120, 200, 150, 80, 70]
}]
series: [
{ type: 'bar', stack: 'x', data: [120, 200, 150], name: '基础' },
{ type: 'bar', stack: 'x', data: [80, 70, 60], name: '堆叠' }
]
// 横向柱状图:yAxis 是类别,xAxis 是数值
yAxis: {
type: 'category',
data: ['苹果', '香蕉', '橙子']
},
xAxis: {
type: 'value'
},
series: [{
type: 'bar',
data: [120, 200, 150],
barWidth: 20 // 柱宽(可选)
}]
series: [{
type: 'bar',
data: [120, 200, 150],
barWidth: 20,
itemStyle: {
color: 'red', // 柱体颜色
borderRadius: 5 // 圆角
},
label: {
show: true, // 显示数值标签
position: 'top', // 标签在柱顶
color: 'green',
fontSize: 12
}
}]
// 折线图
series: [{
type: 'line',
data: [120, 200, 150, 80, 70],
smooth: true, // 平滑曲线(false 就是直线折线)
lineStyle: {
width: 2, // 线宽
color: 'red' // 线颜色
}
}]
// 饼图
series: [{
type: 'pie',
center: ['50%', '50%'], // 圆心位置(默认居中)
data: [
{ value: 1, name: 'A' },
{ value: 2, name: 'B' },
{ value: 3, name: 'C' }
],
label: {
show: true
}
}]
// 坐标轴配置
xAxis: {
type: 'category', // 类目轴(离散,如月份、名称)
data: ['1月', '2月', '3月'],
name: 'X轴', // 轴名称
axisLine: { show: true }, // 轴线
axisTick: { show: true }, // 刻度线
axisLabel: { rotate: 45 }, // 标签旋转 45°(标签太长时用)
boundaryGap: true // 类目轴两端留白(柱状图要 true,折线图常 false)
},
yAxis: {
type: 'value', // 数值轴(连续)
name: 'Y轴',
min: 0, // 最小值
max: 100, // 最大值
splitLine: { show: true }, // 网格线(背景横线)
axisLine: { show: true },
axisTick: { show: true }
}
// tooltip + legend + grid
tooltip: {
trigger: 'item', // 'item' 数据项触发 / 'axis' 坐标轴触发
axisPointer: { type: 'shadow' }, // 柱状图用 shadow,折线图用 line
formatter: (params) => {
// trigger: 'axis' 时 params 是数组;'item' 时 params 是单个对象
const name = params.name // x 轴类目名
const value = params.value
const seriesName = params.seriesName
return `系列: ${seriesName}<br>类型: ${name}<br>值: ${value}`
}
legend: {
data: ['基础', '堆叠'], // 对应 series 里的 name
top: 10,
left: 'center',
orient: 'horizontal', // horizontal 水平 / vertical 垂直
icon: 'circle' // 图例图标形状
}
grid: {
top: 10,
left: 10,
right: 10,
bottom: 10,
containLabel: true // 包含坐标轴标签,避免被裁剪
}
// 生命周期
const chart = echarts.init(domElement) // 1. 初始化(DOM 必须有宽高)
chart.setOption(option) // 2. 渲染 / 更新
chart.resize() // 3. 容器尺寸变了,手动调
chart.dispose() // 4. 销毁实例,释放内存