如何实现echarts双Y轴左右刻度线一致

在这里插入图片描述

在这里插入图片描述

关键代码

  1. 需要计算双Y轴的最大值 最小值
  2. interval, splitNumber
 min: 0,
 splitNumber: 5,
 interval: (YLeftMax - 0) / 5,
<template>
  <div class="trend">
    <div id="homeEchart" style="width:300px;height: 400px"></div>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  name: "home",
  data() {
    return {
      myChart: null
    };
  },

  mounted() {
    this.$nextTick(() => {
      let leftList = [123, 343, 532, 675, 864, 732];
      let rightList = [123, 442, 444, 222, 111, 888];
      let xList = [1, 2, 3, 4, 5, 6];
      let YLeftMax = leftList.sort((v1, v2) => v2 - v1)[0];
      let yRightMax = rightList.sort((v1, v2) => v2 - v1)[0];
      this.init(leftList, rightList, xList, YLeftMax, yRightMax);
    });
  },
  methods: {
    init(leftList, rightList, xList, YLeftMax, yRightMax) {
      var that = this;
      that.myChart = echarts.init(document.getElementById("homeEchart"));
      let option = {
        xAxis: {
          type: "category",
          boundaryGap: false, // 是不是从 0 开始
          data: xList,
          axisTick: {
            //X轴刻度线
            show: false
          }
        },

        yAxis: [
          {
            name: "左边",
            max: YLeftMax,
            min: 0,
            splitNumber: 5,
            interval: (YLeftMax - 0) / 5,
            splitLine: {
              show: true
            },
            axisTick: {
              //y轴刻度线
              show: false
            }
          },
          {
            name: "右边",
            type: "value",
            max: yRightMax,
            splitNumber: 5,
            interval: (yRightMax - 0) / 5,
            splitLine: {
              show: true
            },
            axisTick: {
              //y轴刻度线
              show: false
            }
          }
        ],
        series: [
          {
            data: leftList,
            yAxisIndex: 1,
            type: "line",
            symbol: "none"
          },
          {
            data: rightList,
            yAxisIndex: 0,
            type: "line",
            symbol: "none"
          }
        ]
      };
      that.myChart.setOption(option);
    }
  }
};
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值