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>

实现效果

相关推荐
华仔啊6 分钟前
10分钟搞定!SpringBoot+Vue3 整合 SSE 实现实时消息推送
java·vue.js·后端
Mike_jia22 分钟前
DBSyncer:开源数据同步中间件全景实战指南
前端
安卓理事人23 分钟前
安卓多种通知ui更新的方式(livedata,rxjava,eventbus等)
android·ui·echarts
烛阴36 分钟前
从`new`关键字开始:精通C#类与对象
前端·c#
天若有情67340 分钟前
笑喷!乌鸦哥版demo函数掀桌怒怼主函数:难办?那就别办了!
java·前端·servlet
记得记得就15141 分钟前
【Nginx 实战系列(一)—— Web 核心概念、HTTP/HTTPS协议 与 Nginx 安装】
前端·nginx·http
天蓝色的鱼鱼41 分钟前
mescroll老用户亲测z-paging:这些功能让我果断切换!
前端·uni-app
JarvanMo1 小时前
适用于 iOS 开发者的 Flutter 3.38:Xcode 与 Swift 集成新技巧
前端
北极糊的狐1 小时前
Vue 中 vue-awesome-swiper的使用笔记(适配 Vue2/Vue3)
前端·javascript·vue.js
anyup1 小时前
🔥100+ 天,已全面支持鸿蒙!uView Pro 近期更新盘点及未来计划
前端·uni-app·harmonyos