- 需求:解决x轴数据过多导致页面拥挤
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2de5bf210aa0a4150b9ab9a5835e4d0c.jpeg)
- 解决:添加滚动轴 dataZoom
dataZoom:[
{
type: 'slider',
xAxisIndex: [0],
show:true,
height:8,
bottom:0,
start: 0,
end: 80,
zoomLock: true,
minValueSpan:0,
maxValueSpan:7,
realtime:true,
showDetail: false,
filterMode:'empty'
},
],
- 效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/07de44ca878a834fe16282a7bb37e1bd.jpeg)
- 完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 450px;height:300px;"></div>
</body>
<!-- 引入 echarts.js -->
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
<script type="text/javascript">
var xArr = ['横坐标1','横坐标2','横坐标3','横坐标4','横坐标5','横坐标6','横坐标7','横坐标8','横坐标9','横坐标10','横坐标13','横坐标14','横坐标15','横坐标16','横坐标17','横坐标18','横坐标19','横坐标26','横坐标27','横坐标28']
var dataArr = [100, 600, 300, 100, 250, 500,400,449,600, 600, 300, 100, 250, 500,400,149,201,332,123,455];
var myChart = echarts.init(document.getElementById('main'));
var option = {
backgroundColor: '#ffffff',
title: {
text: '测试统计表',
subtext: '这是一个测试统计表',
x: 'center'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
dataZoom:[
{
type: 'slider',
xAxisIndex: [0],
show:true,
height:8,
bottom:0,
start: 0,
end: 80,
zoomLock: true,
minValueSpan:0,
maxValueSpan:7,
realtime:true,
showDetail: false,
filterMode:'empty'
},
],
xAxis: [{
type: 'category',
data: xArr,
axisTick: {
alignWithLabel: true
},
axisLabel: {
margin: 10,
color: '#a4a4a4',
interval: 0,
},
axisLine: {
show: false
},
axisTick: {
show: false
}
}],
yAxis: [{
type: 'value',
axisLabel: {
color: '#a4a4a4',
},
axisLine: {
show: true
},
axisTick: {
show: true
},
splitLine: {
show: true
}
}],
series: [{
name: '',
type: 'bar',
barWidth: '20px',
data: dataArr,
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 1,
color: 'rgba(102,159,239,1)'
}, {
offset: 0,
color: 'rgba(170,202,246,1)'
}], )
}
},
}]
};
myChart.setOption(option);
</script>
</html>