vue3.0 + D3.js 实现拓扑绘图

这篇具有很好参考价值的文章主要介绍了vue3.0 + D3.js 实现拓扑绘图。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

vue 拓扑图组件,javascript,vue.js,前端

 

1. 首先,在项目中安装 D3.js 和 Vue 3.0:

npm install d3@^7.0.0
npm install vue@^3.0.0

2. Vue组件中引入 D3.js

import * as d3 from 'd3';

3. 在 Vue 组件中定义一个 data 对象,用于存储拓扑结构的节点和边:

data() {
  return {
    nodes: [
      {id: 1, name: 'Node 1'},
      {id: 2, name: 'Node 2'},
      {id: 3, name: 'Node 3'}
    ],
    links: [
      {source: 1, target: 2},
      {source: 1, target: 3}
    ]
  }
}

4. 在 Vue 组件的 mounted 生命周期中使用 D3.js 绘制拓扑结构:

mounted() {
  const svg = d3.select('#topology');
  const width = svg.attr('width');
  const height = svg.attr('height');

  const simulation = d3.forceSimulation(this.nodes)
    .force('link', d3.forceLink(this.links).id(d => d.id))
    .force('charge', d3.forceManyBody())
    .force('center', d3.forceCenter(width / 2, height / 2));

  const link = svg.selectAll('line')
    .data(this.links)
    .enter()
    .append('line')
    .attr('stroke', '#ccc')
    .attr('stroke-width', 1);

  const node = svg.selectAll('circle')
    .data(this.nodes)
    .enter()
    .append('circle')
    .attr('r', 10)
    .attr('fill', 'blue')
    .call(d3.drag()
      .on('start', dragstarted)
      .on('drag', dragged)
      .on('end', dragended));

  node.append('title')
    .text(d => d.name);

  simulation.on('tick', () => {
    link
      .attr('x1', d => d.source.x)
      .attr('y1', d => d.source.y)
      .attr('x2', d => d.target.x)
      .attr('y2', d => d.target.y);

    node
      .attr('cx', d => d.x)
      .attr('cy', d => d.y);
  });

  function dragstarted(event, d) {
    if (!event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
  }

  function dragged(event, d) {
    d.fx = event.x;
    d.fy = event.y;
  }

  function dragended(event, d) {
    if (!event.active) simulation.alphaTarget(0);
    d.fx = null;
    d.fy = null;
  }
}

5. 在 Vue 组件的 template 中添加一个 SVG 元素用于显示拓扑结构:

<template>
  <div>
    <svg id="topology" width="600" height="400"></svg>
  </div>
</template>

 完整代码实现文章来源地址https://www.toymoban.com/news/detail-648072.html

<template>
  <div>
    <svg id="topology" width="600" height="400"></svg>
  </div>
</template>

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

export default {
  name: "topology",
  data() {
    return {
      nodes: [
        {id: 1, name: 'Node 1'},
        {id: 2, name: 'Node 2'},
        {id: 3, name: 'Node 3'},
        {id: 4, name: 'Node 4'}
      ],
      links: [
        {source: 1, target: 2},
        {source: 1, target: 3},
        {source: 1, target: 4}
      ]
    }
  },
  mounted() {
    const svg = d3.select('#topology');
    const width = svg.attr('width');
    const height = svg.attr('height');

    const simulation = d3.forceSimulation(this.nodes)
        .force('link', d3.forceLink(this.links).id(d => d.id).distance(150))
        .force('charge', d3.forceManyBody())
        .force('center', d3.forceCenter(width / 2, height / 2));

    const link = svg.selectAll('line')
        .data(this.links)
        .enter()
        .append('line')
        .attr('stroke', '#ccc')
        .attr('stroke-width', 1);

    const node = svg.selectAll('circle')
        .data(this.nodes)
        .enter()
        .append('circle')
        .attr('r', 10)
        .attr('fill', 'red')
        .call(d3.drag()
            .on('start', dragstarted)
            .on('drag', dragged)
            .on('end', dragended));

    const label = svg.selectAll('.label')
        .data(this.nodes)
        .enter()
        .append('text')
        .attr('class', "label")
        .text(function (d) {return d.name;})
        .attr("dx", 12)
        .attr("dy", ".35em");

    node.append('title')
        .text(d => d.name);

    simulation.on('tick', () => {
      link
          .attr('x1', d => d.source.x)
          .attr('y1', d => d.source.y)
          .attr('x2', d => d.target.x)
          .attr('y2', d => d.target.y);

      node
          .attr('cx', d => d.x)
          .attr('cy', d => d.y);
      label
          .attr('x', function (d) {return d.x;})
          .attr('y', function (d) {return d.y;});
    });

    function dragstarted(event, d) {
      if (!event.active) simulation.alphaTarget(0.1).restart();
      d.fx = d.x;
      d.fy = d.y;
    }

    function dragged(event, d) {
      d.fx = event.x;
      d.fy = event.y;
    }

    function dragended(event, d) {
      if (!event.active) simulation.alphaTarget(0);
      d.fx = null;
      d.fy = null;
    }
  }
}
</script>

到了这里,关于vue3.0 + D3.js 实现拓扑绘图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于SSL VPN技术的中小企业网络接入设计与实现(完整文档+思科拓扑图)

    大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。 作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量

    2024年02月04日
    浏览(21)
  • 基于静态NAT的中小企业服务器网络端口映射与实现(完整文档+思科拓扑图)

    大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。 作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量

    2024年02月04日
    浏览(27)
  • 最最简单的ensp拓扑图

    拓扑图如下 选择两个PC和一个路由器    PC1配置如下:   PC2配置如下: 配置完成后打开路由器对端口G0/0/0进行配置:     再对端口G0/0/1进行配置:   配置完成,打开PC1在命令行中pingPC2:   打开PC2在命令行中pingPC1:  

    2024年02月12日
    浏览(19)
  • 浅谈Flink架构及拓扑图

    声明 : 本文是博主阅读 云邪(Jark)博客 整理后的笔记,如有侵权,可联系博主删除。 本文参考文章如下: https://wuchong.me/blog/2016/05/03/flink-internals-overview/ https://wuchong.me/blog/2016/05/04/flink-internal-how-to-build-streamgraph/ https://wuchong.me/blog/2016/05/10/flink-internals-how-to-build-jobgraph/ http

    2024年01月25日
    浏览(22)
  • 快速读懂网络拓扑图

    简介 总线型拓扑是采用单根传输作为共用的传输介质,将网络中所有的计算机通过相应的硬件接口和电缆直接连接到这根共享的总线上。使用总线型拓扑结构需解决的是确保端用户使用媒体发送数据时不能出现冲突。 优点 (1)网络结构简单,易于网络扩展; (2)设备少、造

    2024年02月07日
    浏览(37)
  • 医院网络设计(完整文档+思科拓扑图)

    大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。 作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量

    2024年02月07日
    浏览(21)
  • 住宅小区的拓扑规划与网络设计(完整文档+ensp拓扑图)

    大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。 作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量

    2024年02月04日
    浏览(23)
  • ENSP安装以及简单配置一个拓扑图

    ENSP学习心得 eNSP软件安装    安装eNSP之前必须先安装以下三个插件: VirtualBox WinPcap Wireshack eNSP作为模拟器主体,需要对应版本的VirtualBox和WinPcap提供虚拟环境,Wireshack用于实验当中测试抓取数据包使用。 安装好这三个插件,只需要点下一步选择好对应的安装位置即可,然后

    2024年02月11日
    浏览(34)
  • threejs 3d网络设备拓扑图绘制示例

    技能点:threejs,Vue,canvas,几何数学。 展示网站:http://jstopo.top 模型上方图标贴图 canvas文字贴图

    2024年02月15日
    浏览(19)
  • 一个小时学会画网络拓扑图(附标准素材)

    一,常见的网络拓扑图图标 二,核心交换机,汇聚交换机,接入交换机的区别? 三,核心交换机,汇聚交换机,接入交换机如何使用? 一、核心层交换机和汇聚层交换机区别 1、功能区别 2、性能区别 核心层交换机是三层交换机,高速转发,有大容量接口带宽(比如万兆接口),较

    2023年04月21日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包