如何實現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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章