[ 登录注册 ]

yuyan

echarts-X轴不从0刻度开始

2016-08-25 22:35:40 admin 返回上一页

1、问题背景

     统一一周内植树的数量,利用柱状图来实现


2、实现源码

(1)X轴从零刻度开始

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>echarts-X轴不从0刻度开始</title>
		<link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">
		<script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>
		<script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>
		<style>
			body,html{
				width: 99%;
				height: 99%;
				font-family: "微软雅黑";
				font-size: 12px;
			}
			#bar{
				width: 100%;
				height: 100%;
			}
		</style>
		<script>
			$(function(){
				var chart = document.getElementById('bar');  
                var echart = echarts.init(chart); 
                
                var option = {
				    title: {
				        text: ''
				    },
				    tooltip: {
				        trigger: 'axis'
				    },
				    legend: {
				        data:['植树数量']
				    },
				    grid: {
				        left: '3%',
				        right: '4%',
				        bottom: '3%',
				        containLabel: true
				    },
				    toolbox: {
				        feature: {
				            saveAsImage: {}
				        }
				    },
				    xAxis: {
				        type: 'category',
				        boundaryGap: false,
				        data: ['周一','周二','周三','周四','周五','周六','周日']
				    },
				    yAxis: {
				        type: 'value'
				    },
				    series: [
				        {
				            name:'植树数量',
				            type:'bar',
				            stack: '植树数量',
				            data:[34, 12, 56, 27, 42, 87, 62]
				        }
				    ]
				};
                  
                echart.setOption(option);  
			});
		</script>
	</head>
	<body>
		<div id="bar"></div>
	</body>
</html>

(2)X轴不从零刻度开始

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>echarts-X轴不从0刻度开始</title>
		<link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">
		<script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>
		<script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>
		<style>
			body,html{
				width: 99%;
				height: 99%;
				font-family: "微软雅黑";
				font-size: 12px;
			}
			#bar{
				width: 100%;
				height: 100%;
			}
		</style>
		<script>
			$(function(){
				var chart = document.getElementById('bar');  
                var echart = echarts.init(chart); 
                
                var option = {
				    title: {
				        text: ''
				    },
				    tooltip: {
				        trigger: 'axis'
				    },
				    legend: {
				        data:['植树数量']
				    },
				    grid: {
				        left: '3%',
				        right: '4%',
				        bottom: '3%',
				        containLabel: true
				    },
				    toolbox: {
				        feature: {
				            saveAsImage: {}
				        }
				    },
				    xAxis: {
				        type: 'category',
				        boundaryGap: true,
				        data: ['周一','周二','周三','周四','周五','周六','周日']
				    },
				    yAxis: {
				        type: 'value'
				    },
				    series: [
				        {
				            name:'植树数量',
				            type:'bar',
				            stack: '植树数量',
				            data:[34, 12, 56, 27, 42, 87, 62]
				        }
				    ]
				};
                  
                echart.setOption(option);  
			});
		</script>
	</head>
	<body>
		<div id="bar"></div>
	</body>
</html>

3、实现结果

(1)X轴从零刻度开始


(2)X轴不从零刻度开始


4、问题说明

     在xAxis中的boundaryGap属性,设置为false代表是零刻度开始,设置为true代表离零刻度间隔一段距离


文章来源:http://www.bozhiyue.com/web/yuyan/2016/0825/433356.html
返回上一页    返回分类 上一篇:   下一篇:
相关