echarts的样式调整 / echarts柱状图不紧挨y轴开始

博客介绍了如何通过JavaScript操作ECharts图表,特别是调整横向柱状图的起始位置,使其不紧贴零刻度线。通过获取canvas元素并设置样式实现偏移,但这样可能导致数据与坐标轴对应关系错位。解决方案可能涉及重新计算坐标或调整ECharts配置选项以保持正确对齐。
摘要由CSDN通过智能技术生成

在这里插入图片描述
如图需要制作一个横向的柱状图,且起点不紧挨着零刻度线。

从元素中可以看到一个柱状图中是有两个canvas构成的,第一个canvas是横竖坐标,第二个canvas就是横向的柱状图。那么需要通过找到第二个canvas然后给它绑定样式属性。
在这里插入图片描述
虽然我们不知道这个元素的id,但我们知道这个元素一定是canvas标签。
所以我们可以通过document.getElementByTagName的方式去找到对应的canvas。

在这里插入图片描述
找到对应的canvas后就可以通过绑定,将样式重写到这个canvas里面了,在这里需要离开横轴坐标一点的话需要调整left的值。

let canvas = document.getElementsByTagName("canvas");
canvas[2].style="position:absolute;left:20px;"

注:
目前能达到效果。但这样调整以后,柱状图的数据和x轴坐标肯定不是正确对应了。或许可以通过别的调整来达到对应的关系,需要另外调整。

附上完整代码

<div class="charts2" id="charts2"></div>
charts2() {
      let charts2 = this.$echarts.init(document.getElementById("charts2"));
      var getName = [
        "a",
        "b",
        "c",
        "d",
        "e",
        "f"
      ];
      var getValue = [128, 170, 118, 159, 128, 126];

      let option = {
        grid: {
          left: "5%",
          right: "10%",
          bottom: "18%",
          top: "2%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "none"
          },
          formatter: function(params) {
            return params[0].name + " : " + params[0].value;
          }
        },
        xAxis: {
          show: true,
          type: "value",
          name: "单位(分钟)",
          nameTextStyle: {
            color: "rgb(106,206,244)"
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(40, 81, 110, 1)",
              width: 2
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: "rgb(106,206,244)"
            }
          },
          splitNumber: 7
        },
        yAxis: [
          {
            type: "category",
            inverse: true,
            offset: 100,
            axisLabel: {
              show: true,
              align: "left",
              textStyle: {
                color: "rgb(106,206,244)"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(40, 81, 110, 1)",
                width: 5
              }
            },
            data: getName
          }
        ],
        series: [
          {
            name: "值",
            type: "bar",
            zlevel: 1,
            label: {
              show: true,
              position: "right",
              color: "#fff",
              fontSize: 14,
              offset: [5, 0]
            },
            itemStyle: {
              normal: {
                barBorderRadius: 30,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: "rgba(63,139,254,1)"
                  },
                  {
                    offset: 1,
                    color: "rgba(170,243,255,1)"
                  }
                ])
              }
            },
            barWidth: 10,
            data: getValue
          }
        ]
      };

      let option2 = {
        grid: {
          left: "15%",
          right: "10%",
          bottom: "18%",
          top: "2%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "none"
          },
          formatter: function(params) {
            return params[0].name + " : " + params[0].value;
          }
        },
        xAxis: {
          show: true,
          type: "value",
          name: "单位(分钟)",
          nameTextStyle: {
            color: "rgb(106,206,244)"
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(40, 81, 110, 1)",
              width: 2
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: "rgb(106,206,244)"
            }
          },
          splitNumber: 7
        },
        yAxis: [
          {
            type: "category",
            inverse: true,
            offset: 100,
            axisLabel: {
              show: true,
              align: "left",
              textStyle: {
                color: "rgb(106,206,244)"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(40, 81, 110, 1)",
                width: 5
              }
            },
            data: getName
          }
        ],
        series: [
          {
            name: "值",
            type: "bar",
            zlevel: 1,
            label: {
              show: true,
              position: "right",
              color: "#fff",
              fontSize: 14,
              offset: [5, 0]
            },
            itemStyle: {
              normal: {
                barBorderRadius: 30,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: "rgba(63,139,254,1)"
                  },
                  {
                    offset: 1,
                    color: "rgba(170,243,255,1)"
                  }
                ])
              }
            },
            barWidth: 10,
            data: getValue
          }
        ]
      };
      charts2.setOption(option);
      window.addEventListener("resize", function() {
        charts2.resize();
      });
      let canvas = document.getElementsByTagName("canvas");
      canvas[2].style="position:absolute;left:20px;"
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值