echarts设置刻度值的背景色,隐藏xy轴,使柱状图每根柱的颜色不相同

本文介绍了如何使用Echarts进行个性化配置,包括隐藏X、Y轴,设置刻度值背景色以及确保每个柱状图柱子颜色各不相同。此外,还提到了当刻度值为中文时,需要在axisLabel的formatter中转换为英文以获得预期效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、隐藏xy轴

show:false

2、设置刻度值的背景色

axisLabel:{
     backgroundColor: "#ccc",
}

3、柱状图每根柱的颜色不相同

itemStyle:{
	normal:{
		color:function(params) { 
			return "#0000FF"
		}
	}
},

完整demo:

option = {
    xAxis: {
        data: ['aa', 'bb', 'cc', 'dd'],
        axisLine:{
            show:false
        },
        axisTick:{
            show:false
        },
axisLabel:{
    color:"#333",
    fontSize:16,
    formatter:function(value){
    	return '{' + value + '| }\n{value|' + value+ '}';
    },
    rich: {
        value: {
            lineHeight: 30,
            align: 'center',
            padding: [73, 4, 5, 6]
        },
        aa: {
            height: 40,
            width:55,
            align: 'center',
            backgroundColor: "#f07f92",
        },
        bb: {
            height: 40,
            width:55,
            align: 'center',
            backgroundColor: "#f0de7f"
        },
        cc: {
            height: 40,
            width:55,
            align: 'center',
            backgroundColor:"#7fcbf0",
            
        },
        dd:{
        	height: 40,
            width:55,
            align: 'center',
            backgroundColor:"#7fe5a1",
        }
    }
}
    },
    legend:{
        data:["a","b","c","d","e","f"],
        icon:"circle",
        width:250,
        left:20
    },
    yAxis: {
        show:false,
        splitLine: {show: false}
    },
    series: [{
        type: 'bar',
        name:"d",
        itemStyle: {
            normal: {
                color: '#ddd'
            }
        },
        silent: true,
        barGap: '-100%', // Make series be overlap
        data: [500],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#8492a6"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'top',
                color:"#333"
            }
        },
    },{
        type: 'bar',
        z: 10,
        name:"a",
        data: [300],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#fc8675"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'inside',
                color:"#fff"
            }
        },
    },{
        type: 'bar',
        name:"e",
        itemStyle: {
            normal: {
                color: '#ddd'
            }
        },
        silent: true,
        barGap: '-100%', // Make series be overlap
        data: [0,200],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#008080"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'top',
                color:"#333",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    },{
        type: 'bar',
        z: 10,
        name:"b",
        data: [0,100],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#0000FF"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'inside',
                color:"#fff",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    },{
        type: 'bar',
        name:"f",
        itemStyle: {
            normal: {
                color: '#ddd'
            }
        },
        silent: true,
        barGap: '-100%', // Make series be overlap
        data: [0,0,150],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#FFFF00"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'top',
                color:"#333",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    },{
        type: 'bar',
        z: 10,
        name:"c",
        data: [0,0,100],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#FF0000"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'inside',
                color:"#fff",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    },{
        type: 'bar',
        name:"ee",
        itemStyle: {
            normal: {
                color: '#ddd'
            }
        },
        silent: true,
        barGap: '-100%', // Make series be overlap
        data: [0,0,0,550],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#FFEFD5"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'top',
                color:"#333",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    },{
        type: 'bar',
        z: 10,
        name:"ff",
        data: [0,0,0,500],
        itemStyle:{
        	normal:{
        		color:function(params) { 
        			return "#F08080"
        		}
        	}
        },
        label: {
            normal: {
                show: true,
                position: 'inside',
                color:"#fff",
                formatter:function(params){
                    if(params.value == 0){
                        return ""
                    }
                    return params.value
                }
            }
        },
    }]
};

备注:如果刻度值是中文,要在axisLabel的formatter里做判断变为英文,不然是会出不来你要的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值