echarts柱状图,一个轴两根柱子数值大小互不影响长度解决方法

本文介绍了一种使用Echarts创建双轴柱状图的方法,解决了在柱状图中百分比和数量数据相差悬殊导致的显示问题。通过设置两个X轴并隐藏其中一个,使每个柱子对应不同的坐标轴,从而确保刻度长度不会相互影响。此外,还展示了具体的Echarts配置代码,包括数据、样式和动画设置,实现了柱状图的动态效果和自适应调整。

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

今天遇到一个需求,需要做一个柱状图的排行榜,横过来的,然后需要一个刻度那显示两个柱子,第一个柱子显示百分比,第二个显示数量。然后我就写了两个柱子,发现百分比最大100.但是数量可能有几万几十万的。那这就有个问题,我的百分比数字太小,被挤得根本看不到了。

网上看了半天没有找到我要的解决办法,后来无意间想到了多轴的功能。试了一下发现果然可以。

效果图

在这里插入图片描述

代码

重点解决办法:我写了两个X轴,只不过我给隐藏了。然后我在series配置项内给其中一个柱子设置
xAxisIndex: 1,
这个 xAxisIndex: 1,意思就是让这根柱子对标第一个X轴,那另外的柱子就自动是对标生下的轴了。那就可以实现不管数值大小都不会互相比较影响刻度长度显示,因为我们根本不在同一个坐标上。各玩各的

    barGraph(val) {
      console.log(val, "--");
      //初始化图标
      var myCharts = this.$echarts.init(this.$refs["echart-right"]);
      //Y轴的数据,和数据值位置一一对应
      var cate = val.gtcodeArray;
      //数据值,顺序和Y轴的名字一一对应totalCount
      var option = {
        title: {
          text: this.rightname + "合格率排行榜top10",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          // trigger: "item",
          // formatter: function (params) {
          //   return `百分比:${params.data.value}<br/>数量:${params.data.totalCount}`;
          // },
        },
        //图表位置
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        //X轴
        xAxis: [
          {
            type: "value",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            //不显示X轴刻度线和数字
            splitLine: { show: false },
            axisLabel: { show: false },
            boundaryGap: [0, 0.001],
          },
          {
            type: "value",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            //不显示X轴刻度线和数字
            splitLine: { show: false },
            axisLabel: { show: false },
            boundaryGap: [0, 0.001],
          },
        ],
        yAxis: {
          type: "category",
          data: cate,
          //升序
          inverse: true,
          splitLine: { show: false },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          //key和图间距
          offset: 10,
          //动画部分
          animationDuration: 300,
          animationDurationUpdate: 300,
          //key文字大小
          nameTextStyle: {
            fontSize: 5,
          },
        },
        series: [
          {
            //柱状图自动排序,排序自动让Y轴名字跟着数据动
            realtimeSort: true,
            xAxisIndex: 1,
            name: "百分比",
            type: "bar",
            data: val.percentageArray,
            barWidth: 12,
            smooth: true,
            valueAnimation: true,
            //Y轴数字显示部分
            label: {
              normal: {
                show: true,
                position: "right",
                valueAnimation: true,
                offset: [5, -2],
                textStyle: {
                  color: "#333",
                  fontSize: 13,
                },
              },
            },
            itemStyle: {
              emphasis: {
                barBorderRadius: 7,
              },
              //颜色样式部分
              normal: {
                barBorderRadius: 7,
                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  { offset: 0, color: "#3977E6" },
                  { offset: 1, color: "#37BBF8" },
                ]),
              },
            },
          },
          {
            //柱状图自动排序,排序自动让Y轴名字跟着数据动
            name: "数量",
            type: "bar",
            data: val.totalCountArray,
            barWidth: 12,
            smooth: true,
            valueAnimation: true,
            //Y轴数字显示部分
            label: {
              normal: {
                show: true,
                position: "right",
                valueAnimation: true,
                offset: [5, -2],
                textStyle: {
                  color: "#333",
                  fontSize: 13,
                },
              },
            },
            itemStyle: {
              emphasis: {
                barBorderRadius: 7,
              },
              //颜色样式部分
              normal: {
                barBorderRadius: 7,
                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  { offset: 0, color: "#8BC6EC" },
                  { offset: 1, color: "#9599E2" },
                ]),
              },
            },
          },
        ],
        //动画部分

        animationDuration: 0,
        animationDurationUpdate: 3000,
        animationEasing: "linear",
        animationEasingUpdate: "linear",
      };
      myCharts.setOption(option, true);
      //图表大小变动从新渲染,动态自适应
      window.addEventListener("resize", function () {
        myCharts.resize();
      });
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

接口写好了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值