欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现ECharts双Y轴左右刻度线一致

程序员文章站 2022-06-27 13:47:18
...

实现ECharts双Y轴左右刻度线一致

实现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>
相关标签: vue2.0 echarts