简体   繁体   English

添加文本标签以强制d3.js中的有向图链接

[英]Adding text labels to force directed graph links in d3.js

I am having trouble adding a text to the links that connect the nodes in the following D3JS connected node graph: http://jsfiddle.net/rqa0nvv2/1/ 我在向以下D3JS连接的节点图中的连接节点的链接添加文本时遇到麻烦: http : //jsfiddle.net/rqa0nvv2/1/

Could anyone please explain to me what is the process to add them? 谁能向我解释添加它们的过程是什么?

  var link = svg.selectAll(".link")
      .data(links)
    .enter().append("line")
      .attr("class", function(d) { if(d.value == "visible") {return "link";} else return "" })
      .style("stroke-width", function(d) { return Math.sqrt(d.stroke); });

   link.append("svg:title").text(function(d) { return "Unit: " +  ", Formula: ";});

Thanks! 谢谢!

Just a couple changes are needed to get the text attached to the edges. 仅需进行几次更改即可将文本附加到边缘。

First, the problem with your current method is that you are shoving the <text> inside the <line> . 首先,当前方法的问题是您将<text>推到<line> This just can't be done in SVG, so you need to create a <g> (group) to contain both the line and the text: 这只是在SVG中无法完成,因此您需要创建一个<g> (组)以包含行和文本:

var link = svg.selectAll(".link")
  .data(links)
  .enter()
  .append("g")
  .attr("class", "link-group")
  .append("line")
  .attr("class", function(d) { return d.value == "visible" ? "link" : ""; })
  .style("stroke-width", function(d) { return Math.sqrt(d.stroke); });

Ok, just adding this won't change anything visually, but it does create the groups that we need. 好的,仅添加此内容不会在视觉上进行任何更改,但确实会创建我们需要的组。 So we need to add the text with something like: 因此,我们需要添加类似以下内容的文本:

var linkText = svg.selectAll(".link-group")
  .append("text")
  .data(force.links())
  .text(function(d) { return d.value == "visible" ? "edge" : ""; })
  .attr("x", function(d) { return (d.source.x + (d.target.x - d.source.x) * 0.5); })
  .attr("y", function(d) { return (d.source.y + (d.target.y - d.source.y) * 0.5); })
  .attr("dy", ".25em")
  .attr("text-anchor", "middle");

This adds the text and initially positions it. 这将添加文本并进行初始定位。 Feel free (please!) style the text and change the content according to how you want it to look. 随意(请!)设置文本样式并根据您的外观更改内容。 Finally, we need to make it move with the force-directed graph: 最后,我们需要使其与力导向图一起移动:

force.on("tick", function() {
  link
    .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; });

  linkText
    .attr("x", function(d) { return (d.source.x + (d.target.x - d.source.x) * 0.5); })
    .attr("y", function(d) { return (d.source.y + (d.target.y - d.source.y) * 0.5); });
});

Which is simple of course since it's the same thing that we already did! 当然这很简单,因为这与我们已经做的一样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM