echarts正负柱形图设置x轴和tooltip显示值都为正数,自定义颜色显示,设置y轴显示单位

option = {
  color:['#189dfe','#f0c219'],
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    },
     formatter: function (params) {
         let res = '';
          params.forEach((v,i)=>{
            res+=`${i==0?v.name:''}<div style="display:flex;align-items:center">
                    <span
                      style="display:inline-block;
                        margin-right:5px;
                        border-radius:10px;
                        width:10px;
                        height:10px;
                        background-color:${v.color}"
                    ></span>
                    <span>${v.seriesName}: ${Math.abs(v.value)}%</span>
                  </div>`
          })
		return (res)
		}
  },
 legend: {
		bottom: '2%',
		left: '3%'
	},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '10%',
    containLabel: true
  },
  title:{
    text:'(岁)',
    textStyle:{
      fontSize:'normal',
       fontStyle:'normal',
       fontWeight:'normal',
    },
    left:'7%',
    top:'3%'
  },
 xAxis:[
		{
		name: '(%)',
		type: 'value',
		axisLabel: {
		formatter: value => {
				if (value < 0) return -value;
				//这里是针对取负值
				else return value;
			}
		}
	}
],
  yAxis: [
    {
      type: 'category',
      // name:'单位',
      axisTick: {
        show: false
      },
      data: ['0-9岁', '10-19岁', '20-29岁', '30-39岁', '40-49岁', '50-59岁', '60-69-岁']
    }
  ],
  series: [
    {
      name: '男',
      type: 'bar',
      stack: 'Total',
       label: {
				normal: {//正常情况
				show: false, //是否显示标签
				position: 'inside',
				formatter: function(params){return Math.abs(params.value)}  //返回绝对值
			}
	},
 emphasis: {
        focus: 'series'
      },
      data: [-320, -302, -341, -374, -390, -450, -420]
    },
    {
      name: '女',
      type: 'bar',
      stack: 'Total',
      label: {
				normal: {//正常情况
				show: false, //是否显示标签
				position: 'inside'
			}
	},
 emphasis: {
       focus: 'series'
     },
   data: [220, 302, 341, 374, 390, 450, 420]
    }
  ]
};

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值