ECharts 是一个使用 JavaScript 实现的开源可视化库,它提供了一整套图表的解决方案,可以轻松实现各种数据可视化需求。本文将带你轻松上手 ECharts 图表插件,包括下载指南和实战案例解析。
下载指南
1. 访问官方网站
首先,你需要访问 ECharts 的官方网站 http://echarts.baidu.com/。
2. 选择合适的版本
在官网首页,你可以看到不同版本的 ECharts,包括基础版、增强版、地图版等。根据你的需求选择合适的版本。
3. 下载
选择版本后,点击“下载”按钮,你可以选择下载 zip 包或直接使用 CDN 链接。
4. 引入 ECharts
如果你选择下载 zip 包,解压后将其引入到你的项目中。如果你使用 CDN 链接,只需在 HTML 文件中添加以下代码即可:
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
实战案例解析
1. 折线图
以下是一个简单的折线图示例:
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('container'));
var option = {
title: {
text: '折线图示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'line',
data: [5, 20, 36, 10, 10, 20]
}]
};
myChart.setOption(option);
</script>
</body>
</html>
2. 雷达图
以下是一个雷达图示例:
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('container'));
var option = {
title: {
text: '雷达图示例'
},
tooltip: {},
legend: {
data:['预算分配(Allocated Budget)','实际开销(Actual Spending)']
},
xAxis: {
type: 'category',
data: ['销售(sales)','管理(admin)','信息技术(IT)','客服(customer support)','研发(R&D)']
},
yAxis: {
type: 'value'
},
series: [{
name: '预算分配(Allocated Budget)',
type: 'line',
smooth: true,
data: [430, 500, 860, 930, 1290]
}, {
name: '实际开销(Actual Spending)',
type: 'line',
smooth: true,
data: [820, 932, 901, 934, 1290]
}]
};
myChart.setOption(option);
</script>
</body>
</html>
3. 地图
以下是一个中国地图示例:
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/map/js/china.js"></script>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('container'));
var option = {
title: {
text: '中国地图示例'
},
tooltip: {
trigger: 'item'
},
series: [
{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
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);
</script>
</body>
</html>
以上是 ECharts 图表插件的下载指南和实战案例解析。希望对你有所帮助!
