当前位置: 首页 > article >正文

echart自适应tree树图,结构组织图模板

  1. 处理数据(代码中有处理数据逻辑,可忽略,因为后端返回的格式不匹配,需要自己处理)
    [{
    name: ‘test1’,
    children: [{
    name: ‘test2’
    }]
    }]
<template>
  <div class="boxEchart">
    <div ref="echart" :style="{ width: width, height: height }"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      id: "chart",
      myChart: null,
      widthPro: "100%",
      heightPro: "100%",
      option: {
        toolbox: {
          left: 10,
          show: true,
          iconStyle: {
            // color: '#1890ff',
          },
          feature: {
            dataZoom: {
              show: false,
            },
            dataView: { show: false },
            magicType: { show: false },
            restore: {
              show: false,
            },
            saveAsImage: {
              name: "体系结构图",
            },
          },
        },
        tooltip: {
          trigger: "item",
          triggerOn: "mousemove",
          formatter: function (params, ticket, callback) {
            return params.name;
          },
        },
        series: [
          {
            name: "体系结构图",
            data: [],
            type: "tree",
            top: "1%",
            left: "10%",
            bottom: "1%",
            right: "10%",
            symbolSize: 9,
            label: {
              position: "left",
              verticalAlign: "middle",
              align: "right",
              fontSize: 11,
            },
            leaves: {
              label: {
                position: "right",
                verticalAlign: "middle",
                align: "left",
              },
            },
            emphasis: {
              focus: "descendant",
            },
            expandAndCollapse: true,
            animationDuration: 550,
            animationDurationUpdate: 750,
          },
        ],
      },
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.treeData = this.initData();
        console.log("数据处理结果---");
        console.log(this.treeData);
        this.option.series[0].data[0] = this.treeData;
        if (this.myChart) {
          console.log("-----重新渲染");
          // this.widthPro = "1200px";
          // this.heightPro = "900px";
          // 计算树的深度,来动态改变图形高度
          let deepNum = this.getDepth(this.option.series[0].data);
          const maxNode = [];
          this.countChildren(this.option.series[0].data, 0, maxNode);
          console.log("广度");
          console.log(maxNode);
          console.log("深度");
          console.log(deepNum);

          // this.widthPro = 120 * deepNum + "px";
          this.heightPro = 50 * Math.max(...maxNode) + "px";
          this.option.series[0].initialTreeDepth = deepNum;

          this.$nextTick(() => this.resize());

          this.myChart.setOption(this.option, true);
        }
      },
    },
  },
  computed: {
    width() {
      return this.widthPro ? this.widthPro : "100%";
    },
    height() {
      return this.heightPro ? this.heightPro : "100%";
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.myChart = echarts.init(this.$refs.echart, "shine");
      this.myChart.setOption(this.option);
      this.myChart.on("click", (params) => {
        this.$emit("click", params);
      });
    });
    window.addEventListener("resize", () => {
      this.resize();
    });
  },
  beforeDestroy() {
    console.log("销毁-----");

    if (!this.myChart) {
      return;
    }
    this.myChart.dispose();
    this.myChart = null;
  },
  methods: {
    resize() {
      this.myChart.resize();
    },
    initData() {
      let chatDataObj = {
        name: this.chartData.systemName,
        level: 1,
        children: [],
      };
      if (this.chartData.chidrenVoList) {
        // 处理数据
        let childrenDef = this.chartData.chidrenVoList.map((org) =>
          this.mapTree({
            level: 2,
            ...org,
          })
        );
        chatDataObj.children = childrenDef;
      }
      return chatDataObj;
    },
    mapTree(org) {
      const haveChildren =
        Array.isArray(org.childrenList) && org.childrenList.length > 0;
      return {
        name: org.systemName,
        level: org.level,
        data: { ...org },
        //判断它是否存在子集,若果存在就进行再次进行遍历操作,知道不存在子集便对其他的元素进行操作
        children: haveChildren
          ? org.childrenList.map((i) =>
              this.mapTree({
                level: org.level + 1,
                ...i,
              })
            )
          : [],
      };
    },
    getDepth(arr) {
      var depth = 0;
      while (arr.length > 0) {
        var temp = [];
        for (var i = 0; i < arr.length; i++) {
          temp.push(arr[i]);
        }
        arr = [];
        for (var i = 0; i < temp.length; i++) {
          for (var j = 0; j < temp[i].children.length; j++) {
            arr.push(temp[i].children[j]);
          }
        }
        if (arr.length >= 0) {
          depth++;
        }
      }
      return depth;
    },
    countChildren(tree, level, result) {
      if (!result[level]) {
        result[level] = 0;
      }
      result[level] += tree.length;

      for (let i = 0; i < tree.length; i++) {
        if (tree[i].children && tree[i].children.length > 0) {
          this.countChildren(tree[i].children, level + 1, result);
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.boxEchart {
  // border: 1px solid red;
  width: 100%;
  height: 100%;
  overflow: auto;
  margin: 0;
}
</style>


在这里插入图片描述


http://www.kler.cn/a/289222.html

相关文章:

  • 翼鸥教育:从OceanBase V3.1.4 到 V4.2.1,8套核心集群升级实践
  • C#程序开发,检测当前电脑已经安装的软件目录
  • 【教程】华南理工大学国际校区宿舍门锁声音设置
  • 哪款开放式耳机好用?5款实力出众的开放式耳机按头安利!
  • 985研一学习日记 - 2024.11.12
  • 游戏引擎学习第七天
  • 国赛数模C题模型(五)
  • 将泛型和函数式编程结合,竟然会让代码这么优雅!
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • 大数据系列之:OutOfMemoryError: unable to create new native thread
  • 简单好用的SD卡克隆软件:轻松克隆SD卡
  • 路径优化 minimum-snap(对A*的全局路径进行优化)
  • 使用Python写一个适用于Dify和FastGPT的JsonPath插件
  • VideoCrafter1:Open Diffusion models for high-quality video generation
  • 【Android】最好用的网络库:Retrofit
  • 深度学习中的PyTorch Tensor详解
  • IntelliJ IDEA 自定义字体大小
  • Milvus向量数据库-数据备份与恢复
  • Kotlin 流 Flow
  • pikachu文件包含漏洞靶场
  • JavaScript-document.write和innerHTML的区别
  • Unity(2022.3.41LTS) - UI详细介绍-Scroll View(滚动视图)
  • Flink 1.14.* Flink窗口创建和窗口计算源码
  • 报告 | 以消费者为中心,消费品零售行业数字化建设持续深化
  • 详解React setState调用原理和批量更新的过程
  • Python基础笔记