当前位置:首页 > 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>

自定义节点样式:

<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

基本树图实现:

<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实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

实现vue vm

实现vue vm

Vue VM 实现方法 Vue VM(ViewModel)是 Vue.js 的核心概念之一,负责连接视图(View)和数据模型(Model)。以下是实现 Vue VM 的几种方法: 使用 Vue…

vue实现投票

vue实现投票

Vue实现投票功能 数据准备 在Vue组件的data中定义投票相关数据,包括选项列表和当前选中状态: data() { return { options: [ { id: 1…

vue搜索功能实现

vue搜索功能实现

Vue搜索功能实现方法 在Vue中实现搜索功能可以通过多种方式完成,以下是几种常见的方法: 使用计算属性实现搜索 计算属性非常适合处理需要根据输入值动态过滤数据的情况。创建一个计算属性,根据搜索关键…