您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 怎么用ECharts画折线图
## 一、ECharts简介
ECharts(Enterprise Charts)是百度开源的一个基于JavaScript的数据可视化库,它提供了丰富的图表类型和灵活的配置选项。自2013年开源以来,ECharts已成为国内最流行的前端可视化工具之一,具有以下核心优势:
1. **丰富的图表类型**:支持折线图、柱状图、饼图、散点图等30+种标准图表
2. **移动端优化**:完善的响应式设计和跨平台兼容性
3. **动态特性**:支持数据的实时更新和动画效果
4. **可视化设计器**:提供在线配置工具降低使用门槛
5. **活跃的社区**:持续更新维护,中文文档完善
## 二、环境准备
### 1. 安装方式
#### (1) CDN引入(推荐初学者)
```html
<!-- 引入echarts核心库 -->
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
npm install echarts --save
// 项目中引入
import * as echarts from 'echarts';
# 克隆仓库
git clone https://github.com/apache/echarts.git
cd echarts
npm install
npm run build
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts折线图示例</title>
<!-- 引入ECharts -->
<script src="echarts.min.js"></script>
<style>
#chart-container {
width: 800px;
height: 500px;
margin: 30px auto;
}
</style>
</head>
<body>
<div id="chart-container"></div>
<script src="your-script.js"></script>
</body>
</html>
// 获取DOM容器
const chartDom = document.getElementById('chart-container');
// 初始化实例
const myChart = echarts.init(chartDom);
const option = {
title: {
text: '基础折线图示例'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['销量']
},
xAxis: {
type: 'category',
data: ['1月', '2月', '3月', '4月', '5月', '6月']
},
yAxis: {
type: 'value'
},
series: [{
name: '销量',
type: 'line',
data: [150, 230, 224, 218, 135, 147]
}]
};
myChart.setOption(option);
window.addEventListener('resize', function() {
myChart.resize();
});
series: [
{
name: '产品A',
type: 'line',
data: [120, 132, 101, 134, 90, 230]
},
{
name: '产品B',
type: 'line',
data: [220, 182, 191, 234, 290, 330]
}
]
series: [{
// ...其他配置
itemStyle: {
color: '#c23531', // 折线颜色
borderColor: '#000', // 数据点边框色
borderWidth: 2
},
lineStyle: {
width: 4,
type: 'dashed' // 可选:'solid', 'dotted', 'dashed'
},
symbol: 'circle', // 数据点形状
symbolSize: 10,
smooth: true // 平滑曲线
}]
series: [{
// ...其他配置
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'rgba(58,77,233,0.8)' },
{ offset: 1, color: 'rgba(58,77,233,0.1)' }
])
}
}]
series: [{
// ...其他配置
markPoint: {
data: [
{ type: 'max', name: '最大值' },
{ type: 'min', name: '最小值' }
]
},
markLine: {
data: [
{ type: 'average', name: '平均值' }
]
}
}]
function fetchData() {
return Array(6).fill(0).map(() => Math.round(Math.random() * 300));
}
setInterval(() => {
const newData = fetchData();
myChart.setOption({
series: [{
data: newData
}]
});
}, 2000);
myChart.on('click', function(params) {
console.log('点击数据:', params);
alert(`您点击了 ${params.name},值为 ${params.value}`);
});
dataZoom: [
{
type: 'slider',
show: true,
xAxisIndex: [0],
start: 0,
end: 50
},
{
type: 'inside',
xAxisIndex: [0],
start: 0,
end: 50
}
]
// 正确做法(保留其他配置)
myChart.setOption({
series: [{
data: newData
}]
}, true); // 第二个参数表示不合并配置
large: true
选项dataSampling
进行数据采样setOption
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>高级折线图示例</title>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
<style>
#container {
width: 900px;
height: 600px;
margin: 30px auto;
}
</style>
</head>
<body>
<div id="container"></div>
<script>
const chart = echarts.init(document.getElementById('container'));
const option = {
title: {
text: '多系列动态折线图',
subtext: '数据仅供参考'
},
tooltip: {
trigger: 'axis',
formatter: '{b}<br/>{a0}: {c0}<br/>{a1}: {c1}'
},
legend: {
data: ['实际销量', '预测销量']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
yAxis: {
type: 'value',
axisLabel: {
formatter: '{value} 件'
}
},
series: [
{
name: '实际销量',
type: 'line',
smooth: true,
data: [120, 132, 101, 134, 90, 230, 210],
markPoint: {
data: [
{ type: 'max', name: '最大值' },
{ type: 'min', name: '最小值' }
]
}
},
{
name: '预测销量',
type: 'line',
smooth: true,
data: [220, 182, 191, 234, 290, 330, 310],
markLine: {
data: [{ type: 'average', name: '平均值' }]
}
}
]
};
chart.setOption(option);
// 窗口缩放自适应
window.addEventListener('resize', function() {
chart.resize();
});
</script>
</body>
</html>
提示:在实际项目中,建议结合Vue/React等框架使用封装好的ECharts组件,如
vue-echarts
或echarts-for-react
,可以更好地与现代前端工程配合。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。