更改d3强制布局链接样式以匹配d3树外观

时间:2019-03-30 13:16:51

标签: d3.js svg force-layout

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

enter image description here

但是具有类似于d3 trees中常见的链接样式,即具有水平(或垂直)端的贝塞尔曲线:

enter image description here

是否可以在不深入研究d3力代码的情况下相应地更改链接?

1 个答案:

答案 0 :(得分:2)

如果您只是想匹配链接的样式,则无需深入研究d3力代码,它仅计算位置,而与样式无关。

每个链接的源和目标都有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-手动指定路径

我们可以用用于连接节点和路径的线代替,如果路径的基准包含目标和源的x,y,我们可以手动提供路径的d属性。也许像这样:

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")
 //.attr("stroke","black")
 //.attr("stroke-width", 2);


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]]);
      })
        //.attr("x1", function(d) { return d.source.x; })
        //.attr("y1", function(d) { return d.source.y; })
        //.attr("x2", function(d) { return d.target.x; })
        //.attr("y2", function(d) { return 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">

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