ECharts使用

图表库ECharts使用

Posted by Starrynight on 2024-07-03
Estimated Reading Time 4 Minutes
Words 970 In Total
Viewed Times

📚 ECharts 基础 + 进阶用法详解


✅ 一、基本结构回顾

1
2
3
4
const chartDom = document.getElementById('main');
const myChart = echarts.init(chartDom);
const option = { /* 图表配置 */ };
myChart.setOption(option);

你需要的所有图表行为和外观都通过 option 对象来控制。


✅ 二、完整配置项结构详解

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
50
const option = {
title: { // 标题组件
text: '主标题',
subtext: '副标题',
left: 'center' // 可选:left, right, center, 数值px
},
tooltip: { // 提示框
trigger: 'axis' // item(每个点)或 axis(整条轴线)
},
legend: { // 图例
data: ['销量'], // 要与 series.name 对应
top: 'top'
},
toolbox: { // 工具栏,提供保存、切图、缩放等功能
feature: {
saveAsImage: {}, // 保存为图片
restore: {}, // 还原
dataView: {}, // 查看数据
magicType: { type: ['line', 'bar'] } // 图表类型切换
}
},
grid: { // 网格位置(图表边距)
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category', //类目轴 这里是离散型 还有time时间轴,log对数轴
boundaryGap: true, //会让坐标轴左右两边留白,让柱子不贴边
data: ['周一', '周二', '周三', '周四', '周五']
},
yAxis: {
type: 'value'
},
series: [
{
name: '销量',
type: 'bar',
data: [120, 200, 150, 80, 70],
itemStyle: {
color: '#3398DB' // 柱子颜色
},
label: {
show: true,
position: 'top' // 标签位置
}
}
]
};

✅ 三、系列 series 配置详解

属性 描述
name 系列名称,用于 tooltip、legend 显示
type 图表类型(bar、line、pie、scatter、radar等)
data 数据数组(数字 或 对象)
label 数据标签配置
itemStyle 样式(颜色、阴影、边框等)
stack 数据堆叠(可用于堆叠柱状图/折线图)
areaStyle 区域图用,设置为 {} 即可显示面积
smooth 折线图是否平滑

✅ 四、常见图表示例(完整可用)


📊 1. 折线图(带平滑曲线 + 数据标签)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const option = {
title: { text: '网站访问量趋势' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'category',
data: ['一月', '二月', '三月', '四月']
},
yAxis: { type: 'value' },
series: [{
name: '访问量',
type: 'line',
data: [820, 932, 901, 934],
smooth: true,
label: { show: true },
areaStyle: {}, // 显示面积图
itemStyle: {
color: '#FF7F50'
}
}]
};

📦 2. 饼图(展示比例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const option = {
title: { text: '浏览器占比', left: 'center' },
tooltip: { trigger: 'item' },
legend: { bottom: 10 },
series: [{
name: '占比',
type: 'pie',
radius: '60%',
data: [
{ value: 40, name: 'Chrome' },
{ value: 30, name: 'Firefox' },
{ value: 20, name: 'Safari' },
{ value: 10, name: '其他' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}]
};

📈 3. 堆叠柱状图

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
const option = {
tooltip: { trigger: 'axis' },
legend: { data: ['邮件营销', '联盟广告', '视频广告'] },
xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四'] },
yAxis: { type: 'value' },
series: [
{
name: '邮件营销',
type: 'bar',
stack: '总量',
data: [120, 132, 101, 134]
},
{
name: '联盟广告',
type: 'bar',
stack: '总量',
data: [220, 182, 191, 234]
},
{
name: '视频广告',
type: 'bar',
stack: '总量',
data: [150, 232, 201, 154]
}
]
};

✅ 五、响应式设计 + 动态更新

1
2
3
4
5
6
7
8
9
10
11
12
window.addEventListener('resize', () => {
myChart.resize();
});

// 动态更新数据
setTimeout(() => {
myChart.setOption({
series: [{
data: [300, 500, 600, 400]
}]
});
}, 2000);

✅ 六、图表类型汇总

类型 type 值
折线图 'line'
柱状图 'bar'
饼图 'pie'
散点图 'scatter'
雷达图 'radar'
K线图 'candlestick'
仪表盘 'gauge'
热力图 'heatmap'
地图 'map'(需额外引入地图数据)

✅ 七、常用组合配置技巧

  • tooltip.trigger: 'axis' 搭配折线图,多个数据联动显示
  • legend + series.name 联动显示
  • stack 用于实现堆叠效果(多柱或多折线累加)
  • areaStyle: {} 实现面积图
  • dataZoom 添加缩放控制


如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !