echarts柱状图的label放置到柱子的顶部

常规的echarts柱状图的label都是跟随柱状图的,通过设置position可以让label跟随柱子在上、中、下进行调整,如下图:

echarts本身不支持将label固定放置在背景的上部。要想实现该功能,可以利用echarts的轴,增加一条x轴放置在上部,然后将轴的label格式化设置即可。效果如下图:

echarts的完整的options配置如下:

option = {
  grid: [
    {
      top: '10%'
    }  
  ],
  xAxis: [
    {
      type: 'category',
      position: 'bottom',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    {
      type: 'category',
      position: 'top',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisLabel: {
        show:true,
        formatter: (value,index)=>{
          console.log(value,index);
          return `{a|${value}}`
        },
        rich: {
          a: {
            backgroundColor: 'red',
            padding: [2,5],
            height:24,
            verticalAlign: 'middle'
          }
        }
      },
      axisLine: {
        show:false
      },
      axisTick: {
        show:false
      },
    },
    {
      type: 'category',
      position: 'top',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      
      axisLabel: {
        show:true,
        margin: 40,
        formatter: (value,index)=>{
          console.log(value,index);
          return `{a|${value}}`
        },
        rich: {
          a: {
            backgroundColor: 'yellow',
            padding: [2,5],
            height:20,
            verticalAlign: 'middle'
          }
        }
      },
      axisLine: {
        show:false
      },
      axisTick: {
        show:false
      },
    },
  ] ,
  yAxis: [
    {
      type: 'value'
    },
  ],
  series: [
    {
      data: [120, 200, 150, 80, 70, 110, 130],
      type: 'bar',
      showBackground: true,
      backgroundStyle: {
        color: 'rgba(180, 180, 180, 0.2)'
      }
    }
  ]
};

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值