javascript - 努力在 D3 中使用贝塞尔曲线实现连接器路径
问题描述
我正在尝试使用 D3 创建一个粗略的数据库图表生成器,但我不知道如何在字段之间获取连接器。我可以从两点得到直线,但我希望它是圆形的,就像我猜的一条路径。
我试图把这个特定问题的一个例子放在一起,链接两个文本字段:
https://codesandbox.io/s/gifted-bardeen-5hbw2?fontsize=14&hidenavigation=1&theme=dark
这是我所指的 dbdiagram.io 的一个示例:
我一直在阅读 d 属性和各种命令,但似乎没有什么更接近。我怀疑该forceSimulation
方法,尤其是forceCenter
当我使用小写命令时,该函数可能会弄乱相对定位。但不是100%。
解决方案
您可以通过connectorPath例程计算 2 点之间的连接器路径:
const source = {x: 200, y: 120};
const target = {x: 50, y: 20};
const MAX_RADIUS = 15;
const connectorPath = (from, to) => {
if (from.y === to.y || from.x === to.x)
return `M ${from.x},${from.y} L ${to.x},${to.y}`;
const middle = (from.x + to.x) / 2;
const xFlag = from.x < to.x ? 1 : -1;
const yFlag = from.y < to.y ? 1 : -1;
const dX = Math.abs(from.x - to.x);
const dY = Math.abs(from.y - to.y);
const radius = Math.min(dX / 2, dY / 2, MAX_RADIUS);
return `M ${from.x},${from.y} H ${middle - radius * xFlag} Q ${middle},${from.y} ${middle},${from.y + radius * yFlag} V ${to.y - radius * yFlag} Q ${middle},${to.y} ${middle + radius * xFlag},${to.y} H ${to.x}`;
};
d3.select('#source').attr('cx', source.x).attr('cy', source.y);
d3.select('#target').attr('cx', target.x).attr('cy', target.y);
d3.select('#connector').attr('d', connectorPath(source, target));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="300" height="200">
<path id="connector" stroke="blue" fill="none" />
<circle id="source" fill="red" r="5"/>
<circle id="target" fill="green" r="5"/>
</svg>
推荐阅读
- java - 如何使用 appium 水平滑动
- jquery - 拆之前先看看有没有关系
- ruby-on-rails - 循环浏览 Google Drive 中的 PDF,显示在 Rails 网站上
- redis - Redis 连接问题
- erlang - 类型 ip_address() 未定义 类型 ip_port() 未定义
- node.js - 需要使用 smtp 的电子邮件通知服务,使用 Nodemailer
- java - 如何在java平台的ajax请求中使用已在简单HTTP请求中通过GET/POST设置的会话对象
- angular - 单击按钮打开 url 但来自 ts 函数
- python-3.x - 无法下载 spacy 模型
- ios - 更新应用程序后的 errSecItemNotFound