Vue3使用echarts tree高度自适应支持滚动查看

最近使用echarts tree写了一个组件,写了固定的高,发现当展开的节点特别多的时候,节点就会重叠在一起(如下图),显示效果不好,就做了一些展示优化。

思路

一开始tree的高度是固定写死的,现在根据节点的数量高度自适应,节点就不会重叠到一起。支持滚动查看。

实现(主要代码)

ini 复制代码
<template>
  <div style="overflow: auto; height: calc(80vh)">
      <div id="treechart" style="height: 800px"></div>
  </div>
</template>

<script setup lang="ts">
import * as Echarts from "echarts";
import { ref,onMounted } from "vue";
onMounted(()=>{
    let chartDom = Echarts.init(document.getElementById("treechart"));
    let option={
        ....
    };
    chartDom.setOption(option);
    chartDom.on("click",function(event){
        let treeDom = document.getElementById("treechart");//获取元素
        if(event.componentType === "series"){
            let nodeList = Array.from(new Set(chartDom._chartsViews[0]._data._graphicEls));//计算节点数量
            let height = 800;//默认高度
            let currentHeight = 30*(nodeList.length-1)||100;//动态高度
            let newHeight = Math.max(currentHeight,height);
            treeDom.style.height= newHeight+"px";
            chartDom.resize();
        }
    });
})
</script>

实现效果

相关推荐
崔庆才丨静觅8 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60619 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了9 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅9 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅10 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅10 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment10 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅10 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊11 小时前
jwt介绍
前端
爱敲代码的小鱼11 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax