便利贴--44{vue3+echarts+felx布局,解决实时应用echarts组件大小-只用了高度,宽度同理}

6 篇文章 0 订阅
4 篇文章 0 订阅

便利贴--44{vue3+echarts+felx布局,解决实时应用echarts组件大小-只用了高度,宽度同理}

实现效果

通过监听等手段动态给echarts dom 修改高度
请添加图片描述

实现代码 片断

<Card
        :title="$t('assetAnalysis.monthlyNewAssets')"
        :msg="'' + $t('assetAnalysis.dwwy') + ''"
        id="ect1Main1"
      >
        <div id="ect1"></div>
      </Card>

实现逻辑:因为ect1Main1是flex:1,所以在获取其高度的时候要考虑到该dom填充需要时间,否则高度不真实,在传值到echarts高度之前,需要判断或者直接清空之前的高度,否者ect1Main1的高度永远是使用过程中最大的


onMounted(() => {
  initEct();
  addEventListen(window, "resize", usSize);
});

//初始化图形数据
const ourEct = {
  ect1: null,
  ect2: null,
};

//获取每个表的dom并初始化
const initEct = () => {
  for (let k in ourEct) {
    if (ourEct[k]) continue;
    let dom = document.getElementById(k);
    ourEct[k] = echarts.init(dom);
  }
  usSize();
};
//获取felx dom高度
const getDomHeight = (dom) => {
  if (!dom) return;
  return new Promise((re, rj) => {
    let doit = (dom) => {
      let m = document.querySelector(dom).querySelector(".main");
      console.log(m);
      if (m.clientHeight < 40 || !m) {
        setTimeout(() => {
          doit(dom);
        }, 200);
      } else {
        console.log(m.clientHeight, "m.clientHeightm.clientHeightm.clientHeight");
        re(m.clientHeight - 20);
        return;
      }
    };
    doit(dom);
  });
};
let time = null;
// 监听尺寸事件
const usSize = () => {
  if (!time) {
    //清除高度
    onResetEctSize();
    getDomHeight("#ect1Main1").then((res) => {
      console.log(res, document.getElementById("ect1"));
      getEct1(res + "px");
    });
    getDomHeight("#ect1Main2").then((res) => {
      console.log(res, document.getElementById("ect1"));
      getEct2(res + "px");
    });
    time = setTimeout(() => {
      clearTimeout(time);
      time = null;
    }, 200);
  }
};
//清除高度
const onResetEctSize = () => {
  for (let k in ourEct) {
    if (ourEct[k]?.resize) {
      ourEct[k].resize({ height: 0 });
    }
  }
};

const getEct1 = (heightChang) => {
  let option = {
    width: "auto",
    height: "auto",
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      top: "3%",
      containLabel: true,
    },
    tooltip: {
      trigger: "item",
    },
    // legend: {
    //   orient: "vertical",
    //   left: "left",
    // },
    series: [
      {
        name: "Access From",
        type: "pie",
        radius: ["68%", "90%"],
        label: {
          show: true,
          formatter: "{b} {d}%",
        },
        data: [
          { value: 5, name: "1", itemStyle: { color: "#F17474" } },
          { value: 18, name: "2", itemStyle: { color: "#EBA639" } },
          { value: 18, name: "3", itemStyle: { color: "#7E5AF5" } },
          { value: 21, name: "4", itemStyle: { color: "#66F9B0" } },
          { value: 65, name: "5", itemStyle: { color: "#3F91FB" } },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
      },
    ],
  };
  ourEct.ect1.clear(); //清除画布,重新绘制的时候就不会遗留之前的数据,解决高度数据变化echarts高度不变问题
  ourEct.ect1.setOption(option);
  ourEct.ect1.resize({ height: heightChang }); //设置图表动态高度
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值