当前位置:首页 > VUE

vue实现树形关系图

2026-02-25 03:56:58VUE

vue实现树形关系图的方法

使用第三方库d3.js

安装d3.js库:npm install d3

在Vue组件中引入d3:

import * as d3 from 'd3'

创建树形布局:

const treeLayout = d3.tree().size([width, height])
const root = d3.hierarchy(data)
const treeData = treeLayout(root)

绘制节点和连线:

// 绘制连线
svg.selectAll('.link')
   .data(treeData.links())
   .enter()
   .append('path')
   .attr('class', 'link')
   .attr('d', d3.linkVertical())

// 绘制节点
svg.selectAll('.node')
   .data(treeData.descendants())
   .enter()
   .append('circle')
   .attr('class', 'node')
   .attr('cx', d => d.x)
   .attr('cy', d => d.y)

使用vue-orgchart组件

安装vue-orgchart:npm install vue-orgchart

基本用法:

<template>
  <orgchart :datasource="treeData"></orgchart>
</template>

<script>
import OrgChart from 'vue-orgchart'
export default {
  components: { OrgChart },
  data() {
    return {
      treeData: {
        name: 'CEO',
        children: [
          { name: 'CTO' },
          { name: 'CFO' }
        ]
      }
    }
  }
}
</script>

自定义节点样式:

vue实现树形关系图

<orgchart :datasource="treeData" :pan="true" :zoom="true">
  <template slot-scope="{nodeData}">
    <div class="custom-node">
      {{nodeData.name}}
    </div>
  </template>
</orgchart>

使用vue-tree-chart组件

安装vue-tree-chart:npm install vue-tree-chart

基本实现:

<template>
  <tree-chart :json="treeData" />
</template>

<script>
import TreeChart from 'vue-tree-chart'
export default {
  components: { TreeChart },
  data() {
    return {
      treeData: {
        name: 'Root',
        children: [
          { name: 'Child1' },
          { name: 'Child2' }
        ]
      }
    }
  }
}
</script>

自定义配置选项:

<tree-chart 
  :json="treeData"
  :custom-options="{
    nodeWidth: 150,
    nodeHeight: 80,
    direction: 'vertical'
  }"
/>

使用ECharts实现

安装ECharts:npm install echarts

vue实现树形关系图

基本树图实现:

<template>
  <div ref="treeChart" style="width: 600px; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  mounted() {
    const chart = echarts.init(this.$refs.treeChart)
    const option = {
      series: [{
        type: 'tree',
        data: [{
          name: 'Root',
          children: [
            { name: 'Child1' },
            { name: 'Child2' }
          ]
        }]
      }]
    }
    chart.setOption(option)
  }
}
</script>

自定义树图样式:

const option = {
  series: [{
    type: 'tree',
    orient: 'vertical',
    symbol: 'roundRect',
    symbolSize: [60, 30],
    label: {
      position: 'inside',
      formatter: '{b}'
    },
    leaves: {
      label: {
        position: 'right'
      }
    },
    expandAndCollapse: true,
    animationDuration: 550,
    data: treeData
  }]
}

纯CSS实现简单树形图

使用CSS伪元素和flex布局:

<template>
  <div class="tree">
    <ul>
      <li>
        <span>Root</span>
        <ul>
          <li><span>Child1</span></li>
          <li><span>Child2</span></li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<style>
.tree ul {
  padding-left: 20px;
  position: relative;
}
.tree li {
  list-style-type: none;
  position: relative;
  padding-left: 20px;
}
.tree li::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 15px;
  height: 100%;
  border-left: 1px solid #ccc;
}
.tree li::after {
  content: "";
  position: absolute;
  top: 15px;
  left: 0;
  width: 15px;
  border-top: 1px solid #ccc;
}
</style>

使用Vue递归组件实现

创建递归组件:

<template>
  <div class="node">
    {{ node.name }}
    <div v-if="node.children" class="children">
      <tree-node 
        v-for="child in node.children" 
        :key="child.id" 
        :node="child"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'TreeNode',
  props: {
    node: Object
  }
}
</script>

主组件中使用:

<template>
  <div class="tree-container">
    <tree-node :node="treeData" />
  </div>
</template>

<script>
import TreeNode from './TreeNode.vue'
export default {
  components: { TreeNode },
  data() {
    return {
      treeData: {
        name: 'Root',
        children: [
          { name: 'Child1' },
          { name: 'Child2' }
        ]
      }
    }
  }
}
</script>

这些方法提供了从简单到复杂的不同实现方案,可以根据项目需求选择合适的方案。第三方库通常提供更丰富的功能和更好的交互体验,而自定义实现则更加灵活可控。

标签: 关系vue
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现: &l…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…