echarts多y轴样式重叠问题

1、主要属性设置

javascript 复制代码
yAxis: [
  {//y轴1
    nameTextStyle: {
      align: "right",
      padding: 0
    }
  },
  {//y轴2
    nameTextStyle: {
      align: "left",
      padding: 0
    }
  },
  {//y轴3
    axisLabel: {
      margin: 50
    },
    nameTextStyle: {
      align: "left",
      padding: [0, 0, 0, 50]
    },
    axisPointer: {
      label: {
        margin: 50,
      }
    }
  }
],

2、页面效果

3、完整代码

vue3 + element plus例子

javascript 复制代码
<template>
  <div ref="chartPage" class="container">
    <el-row :gutter="24">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        相关文档:
        <a href="https://echarts.apache.org/zh/index.html" target="_blank">echarts官方文档</a>
      </el-col>
    </el-row>

    <el-divider content-position="left">echarts</el-divider>

    <div ref="refChart" style="width: 100%; height: 400px"></div>
  </div>
</template>

<script lang="ts" setup name="DemoCharts">
import { useElementSize } from "@vueuse/core";
import * as echarts from "echarts";
import { onMounted, nextTick, ref, onBeforeUnmount, watch } from "vue";

const refChart = ref();
let myChart: echarts.EChartsType;
const initChart = () => {
  if (refChart.value) {
    //初始化echarts实例
    myChart = echarts.init(refChart.value);
    // 指定图表的配置项和数据
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          crossStyle: {
            color: '#999'
          }
        }
      },
      grid: { right: 100 },
      toolbox: {
        feature: {
          dataView: { show: true, readOnly: false },
          magicType: { show: true, type: ['line', 'bar'] },
          restore: { show: true },
          saveAsImage: { show: true }
        }
      },
      legend: {
        data: ['蒸发', '降水', '温度', '降水同比']
      },
      xAxis: [
        {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisPointer: {
            type: 'shadow'
          }
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '蒸发降水',
          min: 0,
          max: 250,
          interval: 50,
          axisLabel: {
            formatter: '{value} ml'
          },
          nameTextStyle: {
            align: "right",
            padding: 0
          },
          axisPointer: {
            label: {
              formatter: function ({ value }: { value: number }) {
                return Math.ceil(value) + "ml"
              },
            }
          }
        },
        {
          type: 'value',
          name: '温度',
          min: 0,
          max: 25,
          interval: 5,
          axisLabel: {
            formatter: '{value} °C'
          },
          nameTextStyle: {
            align: "left",
            padding: 0
          },
          axisPointer: {
            label: {
              formatter: function ({ value }: { value: number }) {
                return Math.ceil(value) + "°C"
              },
            }
          }
        },
        {
          type: 'value',
          name: '降水同比',
          min: 0,
          max: 200,
          interval: 40,
          axisLabel: {
            formatter: '{value} %',
            margin: 50
          },
          nameTextStyle: {
            align: "left",
            padding: [0, 0, 0, 50]
          },
          axisPointer: {
            label: {
              margin: 50,
              formatter: function ({ value }: { value: number }) {
                return Math.ceil(value) + "%"
              },
            }
          }
        }
      ],
      series: [
        {
          name: '蒸发',
          type: 'bar',
          yAxisIndex: 0,
          tooltip: {
            valueFormatter: function (value: number) {
              return value + ' ml';
            }
          },
          data: [
            2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
          ]
        },
        {
          name: '降水',
          type: 'bar',
          yAxisIndex: 0,
          tooltip: {
            valueFormatter: function (value: number) {
              return value + ' ml';
            }
          },
          data: [
            2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
          ]
        },
        {
          name: '温度',
          type: 'line',
          yAxisIndex: 1,
          tooltip: {
            valueFormatter: function (value: number) {
              return value + ' °C';
            }
          },
          data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
        },
        {
          name: '降水同比',
          type: 'line',
          yAxisIndex: 2,
          tooltip: {
            valueFormatter: function (value: number) {
              return value + ' %';
            }
          },
          data: [
            90, 70, 80, 60, 110, 100, 130, 150, 70, 85, 86, 89
          ]
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
  }
};

//重新渲染图表
const resizeChart = () => {
  // 折叠板延迟时间
  setTimeout(() => {
    myChart?.resize();
  }, 400);
};

// 监听页面大小变化
const chartPage = ref();
const { width: pageWidth } = useElementSize(chartPage);
watch(pageWidth, () => {
  resizeChart();
});
// 样式变化重新渲染
// const store = userStore();
// const collapse = computed(() => store.meunIsCollapsed);
// watch(collapse, () => {
//   resizeChart();
// });

// 初始化渲染
onMounted(() => {
  nextTick(() => {
    initChart();
    window.addEventListener("resize", resizeChart);
  });
});

//销毁
onBeforeUnmount(() => {
  window.removeEventListener("resize", resizeChart);
});
</script>
<style lang="less" scoped></style>

4、项目代码

源码地址:https://gitee.com/huanglgln/vue-sys-manage-el

相关推荐
你挚爱的强哥17 分钟前
【sgCreateCallAPIFunctionParam】自定义小工具:敏捷开发→调用接口方法参数生成工具
前端·javascript·vue.js
喝旺仔la23 分钟前
Element 表格相关操作
javascript·vue.js·elementui
米老鼠的摩托车日记26 分钟前
【vue element-ui】关于删除按钮的提示框,可一键复制
前端·javascript·vue.js
forwardMyLife26 分钟前
element-plus的菜单组件el-menu
javascript·vue.js·elementui
猿饵块1 小时前
cmake--get_filename_component
java·前端·c++
好多吃的啊1 小时前
背景图鼠标放上去切换图片过渡效果
开发语言·javascript·ecmascript
大表哥61 小时前
在react中 使用redux
前端·react.js·前端框架
Passion不晚1 小时前
打造民国风格炫酷个人网页:用HTML和CSS3传递民国风韵
javascript·html·css3
十月ooOO1 小时前
【解决】chrome 谷歌浏览器,鼠标点击任何区域都是 Input 输入框的状态,能看到输入的光标
前端·chrome·计算机外设
qq_339191141 小时前
spring boot admin集成,springboot2.x集成监控
java·前端·spring boot