d3.js - 更改 d3 强制布局链接样式以匹配 d3-tree 外观

标签 d3.js svg force-layout

我正在尝试用 d3 绘制家谱。为此,我想使用通常的节点链接图(如 this one ):

enter image description here

但是使用通常在 d3 trees 中找到的链接样式,即具有水平(或垂直)端的贝塞尔曲线:

enter image description here

是否可以相应地更改链接,而无需深入研究 d3-force 代码?

最佳答案

如果您只是想匹配链接的样式,则无需深入研究 d3-force 代码,它只计算位置,与样式无关。
每个链接都具有源和目标的 x 和 y 值。如果您用路径替换在大多数强制布局示例中找到的链接源和目标的行,您可以使用这些 x 和 y 值来设置您想要的任何类型的链接。
我在下面使用 d3v4+ - 您的示例使用 d3v3。
选项 1 - 使用内置链接
在 d3v3 中,您将使用 d3.svg.diagonal ,但现在有 d3.linkVertical()d3.linkHorizontal()达到同样的目的。有了这个,我们可以使用:

d3.linkVertical()
      .x(function(d) { return d.x; })
      .y(function(d) { return d.y; }));
然后塑造表示链接的路径:
 link.attr("d",d3.linkVertical()
      .x(function(d) { return d.x; })
      .y(function(d) { return d.y; }));
我在下面只做了一个垂直样式 - 但您可以确定 x 坐标的差异是否大于 y 坐标,以确定是否应该应用水平样式或垂直样式。

var svg = d3.select("svg");
  
var nodes = "abcdefg".split("").map(function(d) {
  return {name:d};
})

var links = "bcdef".split("").map(function(d) {
  return {target:"a", source:d}
})
links.push({target:"d", source:"b"},{target:"d", source:"g"})
 
var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.name; }))
    .force("charge", d3.forceManyBody().strength(-1000))
    .force("center", d3.forceCenter(250,150));

var node = svg.append("g")
 .selectAll("circle")
 .data(nodes)
 .enter().append("circle")
 .attr("r", 5)

var link = svg.append("g")
 .selectAll("path")
 .data(links)
 .enter().append("path")


simulation
 .nodes(nodes)
 .on("tick", ticked)
 .force("link")
    .links(links);  
      
function ticked() {
    link.attr("d", d3.linkVertical()
          .x(function(d) { return d.x; })
          .y(function(d) { return d.y; }));
          
    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
}
path {
   stroke: black;
   stroke-width: 2px;
   fill:none;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="300">

选项 2 - 手动指定路径
我们可以用路径替换用于连接节点的线,我们可以提供 d手动给定路径数据的路径属性包含目标和源的 x,y。也许是这样的:
path.attr("d", function(d) {
  var x0 = d.source.x;
  var y0 = d.source.y;
  var x1 = d.target.x;
  var y1 = d.target.y;
  var xcontrol = x1 * 0.5 + x0 * 0.5;
  return ["M",x0,y0,"C",xcontrol,y0,xcontrol,y1,x1,y1].join(" ");
})
同样,我在这里只做了一种样式,这次是水平样式,但是添加检查以查看是否需要水平或垂直链接应该相当简单:
enter image description here

var svg = d3.select("svg");
  
var nodes = "abcdefg".split("").map(function(d) {
  return {name:d};
})

var links = "bcdef".split("").map(function(d) {
  return {target:"a", source:d}
})
links.push({target:"d", source:"b"},{target:"d", source:"g"})
 
var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.name; }))
    .force("charge", d3.forceManyBody().strength(-1000))
    .force("center", d3.forceCenter(250,150));

var node = svg.append("g")
 .selectAll("circle")
 .data(nodes)
 .enter().append("circle")
 .attr("r", 5)

var link = svg.append("g")
 .selectAll("path")
 .data(links)
 .enter().append("path")


simulation
 .nodes(nodes)
 .on("tick", ticked)
 .force("link")
    .links(links);
      
      
function ticked() {
    link.attr("d", function(d) {
      var x0 = d.source.x;
      var y0 = d.source.y;
      var x1 = d.target.x;
      var y1 = d.target.y;
      var xcontrol = x1 * 0.5 + x0 * 0.5;
      return ["M",x0,y0,"C",xcontrol,y0,xcontrol,y1,x1,y1].join(" ");
    })

    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
}
path {
   stroke: black;
   stroke-width: 2px;
   fill:none;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="300">

选项 3 - 使用自定义曲线生成器
我包括这个是因为我 just recently answered关于自定义曲线的问题,偶然使用相同的样式。这样我们就可以定义每个链接的路径:
var line = d3.line().curve(d3.someCurve))
link.attr("d", function(d) {
  return line([[d.source.x,d.source.y],[d.target.x,d.target.y]]);
})
我也添加了几条线来构建上面的示例,曲线可以是垂直的也可以是水平的:

var curve = function(context) {
  var custom = d3.curveLinear(context);
  custom._context = context;
  custom.point = function(x,y) {
    x = +x, y = +y;
    switch (this._point) {
      case 0: this._point = 1; 
        this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y);
        this.x0 = x; this.y0 = y;        
        break;
      case 1: this._point = 2;
      default: 
        if (Math.abs(this.x0 - x) > Math.abs(this.y0 - y)) {
           var x1 = this.x0 * 0.5 + x * 0.5;
           this._context.bezierCurveTo(x1,this.y0,x1,y,x,y);       
        }
        else {
           var y1 = this.y0 * 0.5 + y * 0.5;
           this._context.bezierCurveTo(this.x0,y1,x,y1,x,y);            
        }
        this.x0 = x; this.y0 = y; 
        break;
    }
  }
  return custom;
}

var svg = d3.select("svg");

var line = d3.line()
  .curve(curve);
  
var nodes = "abcdefg".split("").map(function(d) {
  return {name:d};
})

var links = "bcdef".split("").map(function(d) {
  return {target:"a", source:d}
})
links.push({target:"d", source:"b"},{target:"d", source:"g"})
 
var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.name; }))
    .force("charge", d3.forceManyBody().strength(-1000))
    .force("center", d3.forceCenter(250,150));

var node = svg.append("g")
 .selectAll("circle")
 .data(nodes)
 .enter().append("circle")
 .attr("r", 5)

var link = svg.append("g")
 .selectAll("path")
 .data(links)
 .enter().append("path")


simulation
 .nodes(nodes)
 .on("tick", ticked)
 .force("link")
    .links(links);
      
      
function ticked() {
    link.
      attr("d", function(d) {
        return line([[d.source.x,d.source.y],[d.target.x,d.target.y]]);
      })

    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
}
 path {
   stroke: black;
   stroke-width: 2px;
   fill:none;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="300">

此选项也适用于 Canvas (如果我没记错的话,选项 1 也适用)。

关于d3.js - 更改 d3 强制布局链接样式以匹配 d3-tree 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55431818/

相关文章:

javascript - 如何选择 d3 中具有特定属性值的元素

javascript - D3力有向图,根据给定的数据和值不同的形状?

javascript - 不渲染字体的不透明度

javascript - 奇怪的 SVG 悬停行为

css - 错误的关键帧动画

javascript - mpld3:如何使用插件更改工具栏的位置?

svg - FabricJS - 设置导入的 svg 的高度和宽度

javascript - 为 d3.js 强制布局实现通用更新模式

svg - 在缩放平移后检测 d3.js 强制布局中的可见节点

javascript - 无限缩放强制布局d3.js