【发布时间】:2020-10-23 00:39:24
【问题描述】:
我在 D3.js 中有一个力导向图,其中节点半径与该数据的属性(例如网页浏览量)成正比,链接宽度与链接数据的属性(例如点击次数)成正比。我想给链接曲线一个方向的指标。问题是链接会到达数据节点的center,所以如果我使用marker-end,我会得到:
(数据节点通常用链接到另一个数据类别的颜色填充...)
我使用以下方法创建我的 ~~arcs~~ 曲线:
positionLink = (d) => {
const offset = 100;
const midpoint_x = (d.source.x + d.target.x) / 2;
const midpoint_y = (d.source.y + d.target.y) / 2;
const dx = d.source.x - d.target.x;
const dy = d.source.y - d.target.y;
// Perpendicular vector
const nx = -dy;
const ny = dx;
const norm_length = Math.sqrt((nx*nx)+(ny*ny));
const normx = nx / norm_length;
const normy = ny / norm_length;
const offset_x = parseFloat(midpoint_x + offset * normx.toFixed(2));
const offset_y = parseFloat(midpoint_y + offset * normy.toFixed(2));
const arc = `M ${d.source.x.toFixed(2)} ${d.source.y.toFixed(2)} S ${offset_x} ${offset_y} ${d.target.x.toFixed(2)} ${d.target.y.toFixed(2)}`;
return arc;
};
我的代码调用 arc 是一个 SVG“S”路径,它是一个“平滑曲线”,但我并没有特别关注它:我只需要将弧彼此分开,这样我可以显示一个方向和另一个方向的数据之间的差异。
如何定位贝塞尔曲线与圆的交点?
(由于曲线的目标是圆心,我想这可以改写为“距离终点r 处的贝塞尔曲线的值”)
如果我有这一点,我可以让它成为一个箭头的顶点。
(如果我在那个点有贝塞尔曲线的斜率会更好,这样我就可以真正对齐它,但我想我可以将它与中点和锚点之间的线对齐......)
【问题讨论】:
标签: javascript d3.js geometry observablehq cubic-bezier