echart 坐标轴字体部分倾斜

在项目中遇到了报表的模块,针对这一块使用了echart 插件。但是在客户有一个特殊需求

如下的图片,坐标轴部分倾斜

echart 坐标轴字体部分倾斜
 

让上图 印度和美国变成红色好处理,echarts中有对应方法,先给出变红色的html代码

<!DOCTYPE html>  
<html>  
<head>  
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Hello, World</title>  

<style>
	#toolTipContent{
		overflow: auto;
		overflow-x: hidden;
		white-space: normal !important;
	}

</style>

<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="echarts.js"></script>

<script type="text/javascript" >
 var colorList = [
                              '#DC143C','#B5C334','#FCCE10','#E87C25','#27727B',
                               '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
                               '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0'
  ];
$(function () {
	option = {
		title : {
			text: '世界人口总量',
			left: 'center',
			subtext: '数据来自网络',
			//textAlign: 'center',
			//textBaseline:'middle',
		},
		tooltip : {
			trigger: 'item',
			borderColor : '#FFFFFF',
			backgroundColor:'#FFFFFF',
			textStyle: {
				color: '#000',
			},
			borderColor: '#FF0000',
			borderWidth: 1,
			enterable: true,
			triggerOn: 'click',
			position:function(point, params, dom, size,){
				dom.style.borderColor = params.color;
				return [point[0], point[1]];
			},
			
			formatter: function(params,ticket,callback) {

				var res = 'Function formatter : <br/>' + params.seriesName + '&nbsp;'+ params.name+'&nbsp;' + params.data;
				res += '<br/>'+'This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This		is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. ' 
				
				//document.getElementById('toolTipsDiv').innerHTML = res;

				$("#toolTipContent").html(res);
				var toolTips = document.getElementById('toolTipsDiv').innerHTML;
				//toolTips = toolTips.tostring();
				setTimeout(function(){
					// 仅为了模拟异步回调
					callback(ticket, toolTips);
				}, 0)
				return 'loading';
			}
			
		},
		legend: {
			
			show: false,
		},
		toolbox: {
			show : false,
			
		},
		calculable : true,
		xAxis : [
			{
				type : 'value',
				axisLabel: {
					formatter: function (value, index) {
						if (index == 0) {
							return '';
						}
						return value;
					}
				},
				min:-1,
				max:4,
			}
		],
		
		yAxis : [
			{
				type : 'category',
				axisLine: {
					onZero: false
				},
				axisLabel: {

					formatter: function (value, index) {
						
						value = value.split(",,");
						return value[0];

						//var myValue = '\<i\>'+value[0]+'\</i\>';
						//return myValue;
						
					},
					textStyle: {
//此处就是让印度,美国两个名称变成红色,
						color: function(value,index ){
							value = value.split(",,")
							if(value[1] == 'true'){
								return 'red';
							}else{
								return 'black';
							}
						},
						
						
						
					}
				},
				
				data : ['巴西,,false','印尼,,false','美国,,true','印度,,true','中国,,false','世界人口(万),,false']
				//data : ['巴西','印尼','aaa','印度','中国','世界人口(万)']
			}
		],
		series : [
			
			{
				
				
				name:'2011年',
				type:'bar',
				barGap: 0,
				markPoint: {
					label: {
						normal: {
							formatter : function(){
								var aa = ""
							}
						},
					},
				},
				itemStyle: {
					normal: {
						 color: function(params) {
                            // build a color map as your need.
                           
							if(params.data == 2){
								return colorList[1];
							}else{
								return colorList[2];
							}
                            
                        },

					}
				},

				data:[0, 2, 4, 2, 2, 3]
			},
			{
				name:'2012年',
				type:'bar',
				barGap: 0,
				data:[3, 1, 2, 2, 3, 2]
			}
			
		]
	};
	var contant = echarts.init(document.getElementById('container'))
    contant.setOption(option);
	

});


</script>


</head>  
 
<body>  
<div id="container" style="min-width:400px;height:400px"></div>

<div id="toolTipsDiv" style="width:200px;height:200px">
	<div id="toolTipContent" style="width:200px;height:200px"> </div>
</div>

<div id="yLable" style="font-size: 15px">
</div>
</body>  
</html>

 

但是没有设置部分倾斜的方法。无奈只能找源码,进行修改。


在源码中的 function buildAxisLabel(axisBuilder, axisModel, opt)  这个function中可以看到



echart 坐标轴字体部分倾斜
 
但是运行后你会看到提示fontStyle 未定义,就是自己加点这个fontStyle未定义。


echart 坐标轴字体部分倾斜
 
没关系,没定义,我们定义就是了


echart 坐标轴字体部分倾斜
 



至此,完成。
修改完源码后在加入需要设置的fontStyle即可,再给出,最终的html代码

<!DOCTYPE html>  
<html>  
<head>  
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Hello, World</title>  

<style>
	#toolTipContent{
		overflow: auto;
		overflow-x: hidden;
		white-space: normal !important;
	}

</style>

<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="echarts.js"></script>

<script type="text/javascript" >
 var colorList = [
                              '#DC143C','#B5C334','#FCCE10','#E87C25','#27727B',
                               '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
                               '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0'
  ];
$(function () {
	option = {
		title : {
			text: '世界人口总量',
			left: 'center',
			subtext: '数据来自网络',
			//textAlign: 'center',
			//textBaseline:'middle',
		},
		tooltip : {
			trigger: 'item',
			borderColor : '#FFFFFF',
			backgroundColor:'#FFFFFF',
			textStyle: {
				color: '#000',
			},
			borderColor: '#FF0000',
			borderWidth: 1,
			enterable: true,
			triggerOn: 'click',
			position:function(point, params, dom, size,){
				dom.style.borderColor = params.color;
				return [point[0], point[1]];
			},
			
			formatter: function(params,ticket,callback) {

				var res = 'Function formatter : <br/>' + params.seriesName + '&nbsp;'+ params.name+'&nbsp;' + params.data;
				res += '<br/>'+'This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This		is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. This is test. ' 
				
				//document.getElementById('toolTipsDiv').innerHTML = res;

				$("#toolTipContent").html(res);
				var toolTips = document.getElementById('toolTipsDiv').innerHTML;
				//toolTips = toolTips.tostring();
				setTimeout(function(){
					// 仅为了模拟异步回调
					callback(ticket, toolTips);
				}, 0)
				return 'loading';
			}
			
		},
		legend: {
			
			show: false,
		},
		toolbox: {
			show : false,
			
		},
		calculable : true,
		xAxis : [
			{
				type : 'value',
				axisLabel: {
					formatter: function (value, index) {
						if (index == 0) {
							return '';
						}
						return value;
					}
				},
				min:-1,
				max:4,
			}
		],
		
		yAxis : [
			{
				type : 'category',
				axisLine: {
					onZero: false
				},
				axisLabel: {

					formatter: function (value, index) {
						
						value = value.split(",,");
						return value[0];

						//var myValue = '\<i\>'+value[0]+'\</i\>';
						//return myValue;
						
					},
					textStyle: {
						color: function(value,index ){
							value = value.split(",,")
							if(value[1] == 'true'){
								return 'red';
							}else{
								return 'black';
							}
						},
						//此处仿照 color: function(value,index ),完成相应的功能
						fontStyle:function(value, index){
							value = value.split(",,")
							if(value[1] == 'true'){
								//return 'red';
								return 'italic';
							}else{
								return 'normal';
								//return 'black';
							}
						}
						
						
					}
				},
				
				data : ['巴西,,false','印尼,,false','美国,,true','印度,,true','中国,,false','世界人口(万),,false']
				//data : ['巴西','印尼','aaa','印度','中国','世界人口(万)']
			}
		],
		series : [
			
			{
				
				
				name:'2011年',
				type:'bar',
				barGap: 0,
				markPoint: {
					label: {
						normal: {
							formatter : function(){
								var aa = ""
							}
						},
					},
				},
				itemStyle: {
					normal: {
						 color: function(params) {
                            // build a color map as your need.
                           
							if(params.data == 2){
								return colorList[1];
							}else{
								return colorList[2];
							}
                            
                        },

					}
				},

				data:[0, 2, 4, 2, 2, 3]
			},
			{
				name:'2012年',
				type:'bar',
				barGap: 0,
				data:[3, 1, 2, 2, 3, 2]
			}
			
		]
	};
	var contant = echarts.init(document.getElementById('container'))
    contant.setOption(option);
	

});


</script>


</head>  
 
<body>  
<div id="container" style="min-width:400px;height:400px"></div>

<div id="toolTipsDiv" style="width:200px;height:200px">
	<div id="toolTipContent" style="width:200px;height:200px"> </div>
</div>

<div id="yLable" style="font-size: 15px">
</div>
</body>  
</html>

 

另 :附上修改后的echart.js