ECharts 是一个使用 JavaScript 实现的开源可视化库,它能够帮助开发者轻松地实现各种数据可视化效果。本文将深入探讨 ECharts 的强大功能,并通过海量案例分享,帮助读者轻松玩转数据可视化世界。
一、ECharts 简介
ECharts 是由百度团队开发的一个开源可视化库,它支持多种图表类型,如折线图、柱状图、饼图、地图等,并且具有丰富的配置项,可以满足各种数据可视化的需求。
1.1 支持的图表类型
- 基础图表:折线图、柱状图、饼图、散点图、环形图、雷达图、K线图等。
- 特殊图表:力导向图、树状图、水波图、地图、词云图等。
- 混合图表:支持多种图表类型的组合,如柱状图+折线图、饼图+地图等。
1.2 ECharts 的特点
- 高性能:采用 Canvas 和 SVG 渲染,具有高渲染性能。
- 易用性:提供丰富的配置项,易于上手。
- 可扩展性:支持自定义系列、坐标系、组件等。
- 跨平台:支持多种浏览器和操作系统。
二、ECharts 实战案例分享
2.1 折线图
// 基于准备好的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.2 饼图
// 基于准备好的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);
2.3 地图
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '地图示例',
subtext: '数据来自网络',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left',
data:['北京','天津','上海','重庆','河北','山西','辽宁','吉林','黑龙江','江苏','浙江','安徽','福建','江西','山东','河南','湖北','湖南','广东','海南','四川','贵州','云南','陕西','甘肃','青海','台湾','内蒙古','广西','西藏','宁夏','新疆','香港','澳门']
},
visualMap: {
min: 0,
max: 2000,
left: 'left',
top: 'bottom',
text: ['高','低'], // 文本,默认为数值文本
calculable: true
},
series: [
{
name: '销量',
type: 'map',
mapType: 'china',
roam: true,
label: {
normal: {
show: true
},
emphasis: {
show: true
}
},
data:[
{name: '北京',value: Math.round(Math.random()*2000) },
{name: '天津',value: Math.round(Math.random()*2000) },
{name: '上海',value: Math.round(Math.random()*2000) },
{name: '重庆',value: Math.round(Math.random()*2000) },
{name: '河北',value: Math.round(Math.random()*2000) },
{name: '山西',value: Math.round(Math.random()*2000) },
{name: '辽宁',value: Math.round(Math.random()*2000) },
{name: '吉林',value: Math.round(Math.random()*2000) },
{name: '黑龙江',value: Math.round(Math.random()*2000) },
{name: '江苏',value: Math.round(Math.random()*2000) },
{name: '浙江',value: Math.round(Math.random()*2000) },
{name: '安徽',value: Math.round(Math.random()*2000) },
{name: '福建',value: Math.round(Math.random()*2000) },
{name: '江西',value: Math.round(Math.random()*2000) },
{name: '山东',value: Math.round(Math.random()*2000) },
{name: '河南',value: Math.round(Math.random()*2000) },
{name: '湖北',value: Math.round(Math.random()*2000) },
{name: '湖南',value: Math.round(Math.random()*2000) },
{name: '广东',value: Math.round(Math.random()*2000) },
{name: '海南',value: Math.round(Math.random()*2000) },
{name: '四川',value: Math.round(Math.random()*2000) },
{name: '贵州',value: Math.round(Math.random()*2000) },
{name: '云南',value: Math.round(Math.random()*2000) },
{name: '陕西',value: Math.round(Math.random()*2000) },
{name: '甘肃',value: Math.round(Math.random()*2000) },
{name: '青海',value: Math.round(Math.random()*2000) },
{name: '台湾',value: Math.round(Math.random()*2000) },
{name: '内蒙古',value: Math.round(Math.random()*2000) },
{name: '广西',value: Math.round(Math.random()*2000) },
{name: '西藏',value: Math.round(Math.random()*2000) },
{name: '宁夏',value: Math.round(Math.random()*2000) },
{name: '新疆',value: Math.round(Math.random()*2000) },
{name: '香港',value: Math.round(Math.random()*2000) },
{name: '澳门',value: Math.round(Math.random()*2000) }
]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
三、总结
ECharts 是一个功能强大的数据可视化库,它可以帮助开发者轻松地实现各种数据可视化效果。通过本文的案例分享,相信读者已经对 ECharts 的使用有了初步的了解。希望本文能够帮助您在数据可视化道路上越走越远。