ECharts 是一个使用 JavaScript 实现的开源可视化库,它提供了一整套图表绘制解决方案,可以轻松实现各种数据可视化需求。对于新手来说,ECharts 的学习曲线相对平缓,本文将带你从基础到实战,一步步掌握 ECharts 图表制作。
一、ECharts 简介
1.1 ECharts 的特点
- 丰富的图表类型:ECharts 提供了折线图、柱状图、饼图、散点图、地图等多种图表类型,满足不同场景的需求。
- 高度可定制:ECharts 支持丰富的配置项,可以轻松调整图表的颜色、字体、大小等样式。
- 跨平台:ECharts 支持多种浏览器和操作系统,无需担心兼容性问题。
- 社区活跃:ECharts 拥有庞大的社区,可以方便地获取帮助和资源。
1.2 ECharts 的应用场景
- 数据可视化:将数据以图表的形式展示,便于用户理解和分析。
- 产品原型设计:在产品原型设计中,使用 ECharts 可以直观地展示数据变化趋势。
- 网站和移动应用:在网站和移动应用中,使用 ECharts 可以丰富用户体验。
二、ECharts 基础入门
2.1 环境搭建
- 下载 ECharts:访问 ECharts 官网(http://echarts.baidu.com/)下载最新版本的 ECharts。
- 引入 ECharts:将下载的 ECharts 文件引入到你的项目中。
- HTML 结构:创建一个 HTML 文件,并添加一个用于绘制图表的容器元素。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ECharts 实例</title>
<!-- 引入 ECharts 文件 -->
<script src="path/to/echarts.min.js"></script>
</head>
<body>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="main" style="width: 600px;height:400px;"></div>
</body>
</html>
2.2 ECharts 配置
ECharts 配置主要包括以下几部分:
- 标题(title):设置图表标题。
- 提示框(tooltip):设置鼠标悬停时显示的提示信息。
- 坐标轴(axis):设置坐标轴的配置项。
- 系列(series):设置图表的数据系列。
- 数据(data):设置图表的数据。
以下是一个简单的 ECharts 配置示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
三、ECharts 实战案例
3.1 饼图
以下是一个饼图的示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '饼图示例',
subtext: '数据来源:ECharts 官网',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left',
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: '50%',
data: [
{value: 335, name: '直接访问'},
{value: 310, name: '邮件营销'},
{value: 234, name: '联盟广告'},
{value: 135, name: '视频广告'},
{value: 1548, name: '搜索引擎'}
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
3.2 地图
以下是一个地图的示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '地图示例',
subtext: '数据来源:ECharts 官网',
left: 'center'
},
tooltip: {
trigger: 'item'
},
visualMap: {
min: 0,
max: 100,
left: 'left',
top: 'bottom',
text: ['高','低'], // 文本,默认为数值文本
calculable: true
},
series: [
{
name: '销量',
type: 'map',
mapType: 'china',
roam: true,
label: {
show: false,
position: 'center',
formatter: '{b}'
},
data: [
{name: '北京',value: Math.round(Math.random() * 1000)},
{name: '天津',value: Math.round(Math.random() * 1000)},
{name: '上海',value: Math.round(Math.random() * 1000)},
{name: '重庆',value: Math.round(Math.random() * 1000)},
{name: '河北',value: Math.round(Math.random() * 1000)},
{name: '山西',value: Math.round(Math.random() * 1000)},
{name: '辽宁',value: Math.round(Math.random() * 1000)},
{name: '吉林',value: Math.round(Math.random() * 1000)},
{name: '黑龙江',value: Math.round(Math.random() * 1000)},
{name: '江苏',value: Math.round(Math.random() * 1000)},
{name: '浙江',value: Math.round(Math.random() * 1000)},
{name: '安徽',value: Math.round(Math.random() * 1000)},
{name: '福建',value: Math.round(Math.random() * 1000)},
{name: '江西',value: Math.round(Math.random() * 1000)},
{name: '山东',value: Math.round(Math.random() * 1000)},
{name: '河南',value: Math.round(Math.random() * 1000)},
{name: '湖北',value: Math.round(Math.random() * 1000)},
{name: '湖南',value: Math.round(Math.random() * 1000)},
{name: '广东',value: Math.round(Math.random() * 1000)},
{name: '海南',value: Math.round(Math.random() * 1000)},
{name: '四川',value: Math.round(Math.random() * 1000)},
{name: '贵州',value: Math.round(Math.random() * 1000)},
{name: '云南',value: Math.round(Math.random() * 1000)},
{name: '陕西',value: Math.round(Math.random() * 1000)},
{name: '甘肃',value: Math.round(Math.random() * 1000)},
{name: '青海',value: Math.round(Math.random() * 1000)},
{name: '台湾',value: Math.round(Math.random() * 1000)},
{name: '内蒙古',value: Math.round(Math.random() * 1000)},
{name: '广西',value: Math.round(Math.random() * 1000)},
{name: '西藏',value: Math.round(Math.random() * 1000)},
{name: '宁夏',value: Math.round(Math.random() * 1000)},
{name: '新疆',value: Math.round(Math.random() * 1000)},
{name: '香港',value: Math.round(Math.random() * 1000)},
{name: '澳门',value: Math.round(Math.random() * 1000)}
]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
四、总结
通过本文的学习,相信你已经对 ECharts 图表制作有了初步的了解。ECharts 是一款功能强大、易于使用的可视化库,可以帮助你轻松实现各种数据可视化需求。希望本文能帮助你快速入门 ECharts,并在实际项目中发挥其作用。
