echarts-横向柱状图文案在柱子上方

在这里插入图片描述
先看代码

const box = document.querySelector('.box');
const e = echarts.init(box);
const yAxisDataLeft = ['TOP1', 'TOP2', 'TOP3', 'TOP4', 'TOP5'];
const yAxisDataRight = ['服装', '矿产', '服务业', '建筑业', '金融业'];
const option = {
  backgroundColor: '#031038',
  grid: {
    left: 40,
    bottom: -10,
    right: 70,
    top: 30,
  },
  yAxis: [
    {
      inverse: true,
      data: yAxisDataLeft,
      axisLabel: {
        show: true,
        inside: true,
        textStyle: {
          color: '#8db0ff',
          fontSize: 16,
          align: 'left',
        },
        formatter: '{value}\n{a|占位}\n{a|占位}',
        rich: {
          a: {
            color: 'transparent',
            lineHeight: 24,
          }
        }
      },
      //offset: 30,
      splitLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      }
    },
    {
      inverse: false,
      data: yAxisDataRight,
      axisLabel: {
        inside: true,
        textStyle: {
          color: '#8db0ff',
          fontSize: 16,
          align: 'right',
        },
        formatter: '{value}\n{a|占位}\n{a|占位}',
        rich: {
          a: {
            color: 'transparent',
            lineHeight: 24,
            fontFamily: 'digital'
          }
        }
      },
      offset: 0,
      splitLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      }
    }
  ],
  xAxis: {
    max: 120,
    show: false,
  },
  series: [
    {
      // 辅助系列
      type: 'bar',
      barGap: '-100%',
      silent: true,
      itemStyle: {
        color: 'rgba(255, 255, 254, 0.2)',
      },
      barWidth: 15,
      data: [120, 120, 120, 120, 120]
    },
    {
      type: 'bar',
      data: [120, 100, 90, 60, 30],
      barWidth: 15,
      label: {
        position: [10, 10],
        normal: {
          position: [800, -24],
          show: true,
          textStyle: {
            color: '#8db0ff',
            fontSize: 16,
          },
        },
      },
      itemStyle: {
        normal: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            '#007AFF', '#00FFFF'
          ].map((color, offset) => ({
            color,
            offset
          })))
        }
      }
    }
  ]
};
e.setOption(option);

这个主要是利用yAxis左右两个 y 轴对应的axisLabel文案实现,当然yAxis右侧也可以放一些其它的数据指标。代码用的是echarts4.0版本,最新的5.0也是兼容的,直接复制粘贴到项目中即可!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值