ECharts 是一个使用 JavaScript 实现的开源可视化库,可以轻松地在网页中嵌入各种图表。它提供了丰富的图表类型,包括折线图、柱状图、饼图、地图等,并且易于使用和配置。本教程旨在帮助新手快速上手 ECharts 图表制作,并提供一些案例解析,让读者能够更好地理解和应用。
第一章:ECharts 简介
1.1 ECharts 的优势
- 丰富的图表类型:ECharts 提供了多种图表类型,可以满足不同场景下的需求。
- 易于使用:ECharts 的配置项清晰明了,易于理解和操作。
- 高度定制:可以通过丰富的配置项对图表进行高度定制,满足个性化需求。
- 跨平台支持:ECharts 支持多种浏览器和平台,包括 PC、移动端、微信小程序等。
1.2 ECharts 的安装
ECharts 是一个纯 JavaScript 库,可以通过以下几种方式安装:
- CDN 链接:直接在 HTML 文件中引入 ECharts 的 CDN 链接。
- npm 包管理器:使用 npm 安装 ECharts。
- Git 仓库:从 ECharts 的 Git 仓库克隆代码。
第二章:ECharts 基础教程
2.1 ECharts 配置项解析
ECharts 的配置项主要包括以下几部分:
- 图表类型:指定图表的类型,如折线图、柱状图等。
- 数据:提供图表所需的数据。
- 样式:配置图表的样式,如颜色、字体等。
- 交互:配置图表的交互效果,如鼠标悬停提示、点击事件等。
2.2 基础图表制作
以下是一个简单的折线图示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '折线图示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'line',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
2.3 高级图表制作
ECharts 支持多种高级图表,如散点图、柱状图、饼图、地图等。以下是一个饼图示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '饼图示例'
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c} ({d}%)"
},
legend: {
orient: 'vertical',
left: 10,
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: [
{value: 335, name: '直接访问'},
{value: 310, name: '邮件营销'},
{value: 234, name: '联盟广告'},
{value: 135, name: '视频广告'},
{value: 1548, name: '搜索引擎'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
第三章:案例解析
3.1 地图图表
以下是一个中国地图图表的示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '中国地图示例',
subtext: '数据来自 ECharts',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left',
data:['北京', '天津', '上海', '重庆', '河北', '山西', '辽宁', '吉林', '黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '台湾', '内蒙古', '广西', '西藏', '宁夏', '新疆', '香港', '澳门']
},
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);
3.2 雷达图
以下是一个雷达图示例:
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '雷达图示例'
},
tooltip: {},
legend: {
orient: 'vertical',
left: 'left',
data:['预算分配(Allocated Budget)', '实际开销(Actual Spending)']
},
radar: {
name: {
textStyle: {
color: '#000'
}
},
indicator: [
{ name: '销售(sales)', max: 6500},
{ name: '管理(admin)', max: 16000},
{ name: '信息技术(IT)', max: 30000},
{ name: '客服(customer support)', max: 38000},
{ name: '研发(R&D)', max: 52000},
{ name: '市场(marketing)', max: 25000}
]
},
series: [
{
name: '预算 vs 开销',
type: 'radar',
data : [
{
value : [4200, 3000, 20000, 35000, 50000, 18000],
name : '预算分配(Allocated Budget)'
},
{
value : [5000, 14000, 28000, 26000, 42000, 21000],
name : '实际开销(Actual Spending)'
}
]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
第四章:总结
通过本章的学习,相信你已经对 ECharts 图表制作有了初步的了解。ECharts 是一款功能强大、易于使用的可视化库,可以帮助你轻松地制作各种图表。在实际应用中,你可以根据自己的需求选择合适的图表类型和配置项,以达到最佳的效果。
在后续的学习中,你可以进一步探索 ECharts 的更多功能,如地图、3D 图表等。希望本教程能帮助你更好地掌握 ECharts 图表制作技巧,为你的数据可视化之路保驾护航。
