当前位置:首页 > VUE

vue实现charts树形

2026-03-08 21:59:42VUE

Vue 实现树形图表

在 Vue 中实现树形图表可以使用第三方库如 echartsd3.js。以下是使用 echartsd3.js 的两种方法。

使用 ECharts 实现树形图

安装 ECharts 依赖:

npm install echarts --save

在 Vue 组件中引入并使用:

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

<script>
import * as echarts from 'echarts';

export default {
  mounted() {
    this.initTreeChart();
  },
  methods: {
    initTreeChart() {
      const chart = echarts.init(this.$refs.treeChart);
      const option = {
        series: [{
          type: 'tree',
          data: [{
            name: 'Root',
            children: [
              { name: 'Child 1' },
              { name: 'Child 2', children: [{ name: 'Grandchild' }] }
            ]
          }],
          orient: 'vertical',
          symbol: 'circle',
          symbolSize: 10,
          label: {
            position: 'top',
            rotate: 0,
            verticalAlign: 'middle',
            align: 'right'
          },
          leaves: {
            label: {
              position: 'right',
              verticalAlign: 'middle',
              align: 'left'
            }
          },
          expandAndCollapse: true,
          animationDuration: 550,
          animationDurationUpdate: 750
        }]
      };
      chart.setOption(option);
    }
  }
};
</script>

使用 D3.js 实现树形图

安装 D3.js 依赖:

npm install d3 --save

在 Vue 组件中引入并使用:

vue实现charts树形

<template>
  <svg ref="treeSvg" width="600" height="400"></svg>
</template>

<script>
import * as d3 from 'd3';

export default {
  mounted() {
    this.initD3Tree();
  },
  methods: {
    initD3Tree() {
      const data = {
        name: 'Root',
        children: [
          { name: 'Child 1' },
          { name: 'Child 2', children: [{ name: 'Grandchild' }] }
        ]
      };

      const svg = d3.select(this.$refs.treeSvg);
      const margin = { top: 20, right: 90, bottom: 30, left: 90 };
      const width = 600 - margin.left - margin.right;
      const height = 400 - margin.top - margin.bottom;

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

      const g = svg.append('g')
        .attr('transform', `translate(${margin.left},${margin.top})`);

      g.selectAll('.link')
        .data(root.links())
        .enter().append('path')
        .attr('class', 'link')
        .attr('d', d3.linkVertical()
          .x(d => d.x)
          .y(d => d.y));

      const node = g.selectAll('.node')
        .data(root.descendants())
        .enter().append('g')
        .attr('class', 'node')
        .attr('transform', d => `translate(${d.x},${d.y})`);

      node.append('circle')
        .attr('r', 5);

      node.append('text')
        .attr('dy', '.31em')
        .attr('x', d => d.children ? -13 : 13)
        .style('text-anchor', d => d.children ? 'end' : 'start')
        .text(d => d.data.name);
    }
  }
};
</script>

<style>
.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}
.node text {
  font: 12px sans-serif;
}
</style>

注意事项

  • ECharts 适合快速实现交互式图表,支持丰富的配置项和动画效果。
  • D3.js 更适合高度定制化的需求,但代码复杂度较高。
  • 两种方法均需在 Vue 生命周期钩子(如 mounted)中初始化图表,确保 DOM 已渲染完成。

标签: vuecharts
分享给朋友:

相关文章

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…