vue.js中使用d3.js画家谱关系图

2023-10-22 14:51
文章标签 使用 关系 vue js 画家 d3

本文主要是介绍vue.js中使用d3.js画家谱关系图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

为什么80%的码农都做不了架构师?>>>   hot3.png

 项目中需要做个家谱图,网上查了好多资料没找到合适的,就自己写个简单的,方便以后查看,附上效果图

首先展示父亲、配偶、子女,三代人信息,然后选择其他人可以展开他的三代关系。如下图

下面是代码,这个关系图还只是个初稿,里有些逻辑不全,其中母亲这个通过父亲展开就不合适。以后有机会再完善吧。

<template lang='html'><div class='demo'><div class='left'><div class='left-top'><el-tableheight='250'highlight-current-row@current-change="handleCurrentChange":data='nodes'><el-table-columnprop='id'label='id'width='50'></el-table-column><el-table-columnprop='name'label='姓名'></el-table-column></el-table></div><div class='left-bottom'><el-tableheight='250':data='links'><el-table-columnprop='srcId'label='源id'width='50'></el-table-column><el-table-columnprop='toId'label='目标id'></el-table-column><el-table-columnlabel='关系'><template slot-scope='scope'>{{ scope.row.type | toCn }} </template></el-table-column></el-table></div></div><div class='testd3' ref="testd3"></div></div>
</template><script>
import * as d3 from 'd3';
let width_ = 60;
export default {data () {return {srcNode: null,svg: null,nodes: [{id: '0', name: '张三'},{id: '1', name: '张父'},{id: '2', name: '张母'},{id: '3', name: '张三妻'},{id: '4', name: '张大'},{id: '5', name: '张小'},{id: '6', name: '张小妻'},{id: '7', name: '张小小'},{id: '8', name: '张三妻父'},{id: '9', name: '张三妻母'},{id: '10', name: '张三妻弟'}],links: [{srcId: '0', toId: '1', type: 0}, // 0 父子// {srcId: '0', toId: '2', type: 1}, // 1 母子{srcId: '1', toId: '2', type: 2}, // 2 配偶{srcId: '0', toId: '3', type: 2}, // 2 配偶{srcId: '0', toId: '4', type: 3}, // 子女{srcId: '0', toId: '5', type: 3}, //  子女{srcId: '5', toId: '6', type: 2}, // 配偶{srcId: '5', toId: '7', type: 3}, //  子女{srcId: '3', toId: '8', type: 0}, //  父{srcId: '8', toId: '9', type: 2}, //  配偶{srcId: '8', toId: '10', type: 3} //  子女],drag: false};},filters: {toCn (src) {let res = ['父子', '母子', '配偶', '子女'];return res[src];}},methods: {handleCurrentChange (row) {this.srcNode = row;// 查找中心点连线关系 srcId = 0let srcId = row.id;let srcNode = this.nodes.filter(n => n.id === srcId)[0];// 获取与此节点关系点let links = this.links.filter(l => l.srcId === srcId);let otherlinks = this.links.filter(l => l.toId === srcId).map(l => {let link = {};if (l.type === 3) { // 子女 -> 父子link.type = 0;} else if (l.type === 0) { // 父女 -> 子女link.type = 3;} else {link.type = l.type;}link.srcId = l.toId;link.toId = l.srcId;return link;});links.push(...otherlinks);// 计算节点坐标let nodes_ = this.convert(srcNode, links);let that = this;// 设置画布let width = 1000;let height = 720;// console.log(d3.select('.testd3')[0].innerHTML);d3.select('.testd3').selectAll('*').remove();// 画中心点let svg = d3.select('.testd3').append('svg').attr('width', width).attr('height', height).append('g').attr('transform', 'translate(40,0)');var drag = d3.behavior.drag().on('drag', function(d) {d3.select(this).attr('transform', 'translate(' + (d3.event.x - 30) + ',' + (d3.event.y - 30) + ')').append('rect').attr('x', d.x = d3.event.x - 30).attr('y', d.y = d3.event.y - 30);// 线条svg.selectAll(`.link-${d.id}`).attr('d', function(dd) {return that.getPath(d, dd);});});that.drag = drag;// 画线this.drawLinks(svg, links);// 画点console.log('nodes_', nodes_);this.drawNodes(svg, nodes_);},// 转换convert (srcNode, links) {let nodes = [];let map = new Map();// 子女个数let childsize = links.filter(l => l.type === 3).length;// 子女开始位置let start = -(childsize - 1) * width_;if (srcNode.x === undefined) { // 默认坐标srcNode.x = 150;srcNode.y = 150;}map.set(srcNode.id, srcNode);let {x, y} = srcNode;links.forEach(l => {if (l.type === 0) { // 父子map.set(l.toId, {x: x, y: y - width_ * 2, type: l.type});}if (l.type === 1) { // 母子map.set(l.toId, {x: x + width_ * 2, y: y - width_ * 2, type: l.type});}if (l.type === 2) { // 配偶map.set(l.toId, {x: x + width_ * 2, y: y, type: l.type});}if (l.type === 3) { // 子女map.set(l.toId, {x: x + start, y: y + width_ * 2, type: l.type});start = start + width_ * 2;}});this.nodes.forEach(n => {let m = map.get(n.id);if (m) {n['x'] = n.x || m.x;n['y'] = n.y || m.y;n['type'] = m.type;nodes.push(n);}});return nodes;},getPath (move, stas) {// 获取对方坐标let srcId = stas.srcId;let flag = false;if (move.id === stas.srcId) {srcId = stas.toId;flag = true;}let path;// 源let {x, y} = this.nodes.filter(n => n.id === srcId)[0];if (stas.type === 0) { // 父子if (flag) {path = `M${x + width_ / 2} ${y}L${x + width_ / 2} ${y + width_ * 1.5}L${move.x + width_ / 2} ${move.y - width_ / 2}L${move.x + width_ / 2} ${move.y}`;} else {path = `M${x + width_ / 2} ${y}L${x + width_ / 2} ${y - width_ / 2}L${move.x + width_ / 2} ${move.y + width_ * 1.5}L${move.x + width_ / 2} ${move.y}`;}}if (stas.type === 2 || stas.type === 1) { // 配偶let w_ = move.x > x ? width_ : -width_;let padding = move.x > x ? -width_ / 2 : width_ * 1.5;if (flag) {path = `M${x + width_ / 2 + w_ / 2} ${y + width_ / 2}L${move.x + padding} ${y + width_ / 2}L${move.x + width_ / 2 - w_ / 2} ${move.y + width_ / 2}`;} else {path = `M${x + width_ / 2 + w_ / 2} ${y + width_ / 2}L${move.x + padding} ${y + width_ / 2}L${move.x + width_ / 2} ${move.y + width_ / 2}`;}}if (stas.type === 3) { // 子女if (flag) {path = `M${x + width_ / 2} ${y + width_}L${x + width_ / 2} ${y - width_ / 2}L${move.x + width_ / 2} ${move.y + width_ * 1.5}L${move.x + width_ / 2} ${move.y + width_}`;} else {path = `M${x + width_ / 2} ${y + width_}L${x + width_ / 2} ${y + width_ * 1.5}L${move.x + width_ / 2} ${move.y - width_ / 2}L${move.x + width_ / 2} ${move.y + width_}`;}}return path;},drawNode (svg, node) {let node_ = svg.selectAll(`.node-${node.id}`).data([node]).enter().append('g').attr('class', `node-${node.id}`).attr('transform', function(d) {return 'translate(' + (d.x) + ',' + (d.y) + ')';}).on('dblclick', (d) => {let links = this.links.filter(l => l.srcId === d.id);if (links.length === 0) {this.$message('没有关联数据');return;}let nodes2 = this.convert(d, links);this.drawLinks(svg, links);this.drawNodes(svg, nodes2);}).on('mouseover', function(d) {d3.select(this).select('rect').attr('stroke', '#FFCC33').attr('stroke-width', 3); // 设置边框}).on('mouseout', function(d) {d3.select(this).select('rect').attr('stroke-width', 0); // 取消边框}).call(this.drag);node_.append('rect').attr('width', 60).attr('height', 60).attr('x', 0).attr('y', 0).attr('style', (d) => {return (d.type === 1 || d.type === 2) ? 'fill:#FFAD5B;' : 'fill:#35AD5B;';});node_.append('text').attr('dx', function(d) {return 30;}).attr('dy', 30).style('text-anchor', function(d) {return 'middle';}).style('fill', '#fff').text(function(d) {return d.name;});},drawNodes (svg, nodes) {nodes.forEach(n => {this.drawNode(svg, n);});},// 添加链接drawLinks (svg, linksData) {let that = this;linksData.forEach(l => {let classFlag = l.srcId > l.toId ? `${l.srcId}-${l.toId}` : `${l.toId}-${l.srcId}`;svg.selectAll(`.link-${classFlag}`).data([l]).enter().append('path').attr('class', d => {return `link-${d.srcId} link-${d.toId} link-${classFlag}`;}).attr('d', function(d) {let node = that.nodes.filter(n => n.id === d.toId)[0];return that.getPath(node, d);}).attr('style', function() {return 'stroke:#F7881F';});});}},mounted() {}
};
</script><style lang='css'>
[class^=link] {fill: none;stroke: #ccc;stroke-width: 1.5px;
}
.demo {width: 100%;height: 100%;
}
.left {float: left;width: 30%;
}
.testd3 {float: left;width: 70%;
}
</style>

 

转载于:https://my.oschina.net/Lady/blog/1649994

这篇关于vue.js中使用d3.js画家谱关系图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/262150

相关文章

Java中Runnable和Callable的区别和联系及使用场景

《Java中Runnable和Callable的区别和联系及使用场景》Java多线程有两个重要的接口,Runnable和Callable,分别提供一个run方法和call方法,二者是有较大差异的,本文... 目录一、Runnable使用场景二、Callable的使用场景三、关于Future和FutureTa

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

使用国内镜像源优化pip install下载的方法步骤

《使用国内镜像源优化pipinstall下载的方法步骤》在Python开发中,pip是一个不可或缺的工具,用于安装和管理Python包,然而,由于默认的PyPI服务器位于国外,国内用户在安装依赖时可... 目录引言1. 为什么需要国内镜像源?2. 常用的国内镜像源3. 临时使用国内镜像源4. 永久配置国内镜

Go语言中最便捷的http请求包resty的使用详解

《Go语言中最便捷的http请求包resty的使用详解》go语言虽然自身就有net/http包,但是说实话用起来没那么好用,resty包是go语言中一个非常受欢迎的http请求处理包,下面我们一起来学... 目录安装一、一个简单的get二、带查询参数三、设置请求头、body四、设置表单数据五、处理响应六、超

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

详解如何使用Python提取视频文件中的音频

《详解如何使用Python提取视频文件中的音频》在多媒体处理中,有时我们需要从视频文件中提取音频,本文为大家整理了几种使用Python编程语言提取视频文件中的音频的方法,大家可以根据需要进行选择... 目录引言代码部分方法扩展引言在多媒体处理中,有时我们需要从视频文件中提取音频,以便进一步处理或分析。本文

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖