D3集群布局中的链接和textPath

时间:2022-09-28 13:27:57

My question is very similar to JS D3 textPath isn't displayed

我的问题与JS D3非常相似,没有显示textPath

The one difference is I want to attach the json that comes back to the links not just the nodes(well, for every node that has a link going to it's parent, I want that link to have the same json)...and ideally, I actually want to add a line from the root node out to the left as well going nowhere.

一个区别是我想要附加回到链接的json而不仅仅是节点(好吧,对于每个节点都有链接转到它的父节点,我希望该链接具有相同的json)...并且理想情况下,我实际上想要从根节点向左添加一条线路,也无处可去。

Then, I basically want to have text in the middle of my link and following the link line would be nice but not absolutely necessary.

然后,我基本上希望在我的链接中间有文本,并在链接线后面会很好但不是绝对必要的。

My current non-working code is(and I am not sure, but do I need to add an id element to each link? but for to do so I would need the json!!!! plus I need the json to retrieve the text for each link as well) and then I think I use the id in the textPath to refer back to the path if I am not mistaken, right?

我当前的非工作代码是(我不确定,但是我需要为每个链接添加一个id元素吗?但为了这样做,我需要json !!!!加上我需要json来检索文本对于每个链接)然后我想我使用textPath中的id来引用回路径,如果我没有记错,对吧?

var width = $("#graph").width(),
    height = 200;

var cluster = d3.layout.cluster()
    .size([height, width - 160]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });
var myDiv = document.getElementById('graph');

var svg = d3.select(myDiv).append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(40,0)");

d3.json("/datastreams/json/${_encoded}", function(error, root) {
  var nodes = cluster.nodes(root),
      links = cluster.links(nodes);

  var link = svg.selectAll(".link")
      .data(links)
    .enter().append("path")
      .attr("class", "link")
      .attr("d", diagonal);

  var node = svg.selectAll(".node")
      .data(nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

  link.append("text")
     .style("font-size", function(d) { return d.selected ? "16px" : "10px"; })
     .style("font-weight", function(d) { return d.selected ? "bold" : ""; })
     .text(function(d) { return "SSSS="+d.name; });

  node.append("circle")
      .attr("r", function(d) { return d.selected ? 10 : 4.5; });

  var refs = node.append("a")
    .attr("xlink:href", function(d) { return d.root ? "start" : "end"; });

  var text = refs.append("text")
  .attr("dx", function(d) { return d.root ? 8 : -8; })
  .attr("dy", -4)
  .style("text-anchor", function(d) { return d.root ? "start" : "end"; });

  text.append("tspan")
     .style("font-size", function(d) { return d.selected ? "16px" : "10px"; })
     .style("font-weight", function(d) { return d.selected ? "bold" : ""; })
     .style("fill", function(d) { return "blue"; })
     .text(function(d) { return d.name; });

});

d3.select(self.frameElement).style("height", height + "px");

</script>

1 个解决方案

#1


1  

ah, there we go as this seems to be working(well, I am not fully done but have it very close to what I want).

啊,我们去,因为这似乎工作(好吧,我没有完全完成,但它非常接近我想要的)。

  var textPath = path.each(function(l){
    var aref = theG.append("a")
        .attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
    var text = aref.append("text")
          .style("fill", function(d) { return "blue"; });
    var textPath = text.append("textPath")
            .attr("startOffset", "50%")
            .attr("xlink:href", "#path"+l.target.name)
            .text("insert");

    var aref2 = theG.append("a")
    .attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
  var text2 = aref2.append("text")
    .style("fill", function(d) { return "blue"; });
  var textPath2 = text2.append("textPath")
    .attr("startOffset", "60%")
    .attr("xlink:href", "#path"+l.target.name)
    .text("delete");
  });

#1


1  

ah, there we go as this seems to be working(well, I am not fully done but have it very close to what I want).

啊,我们去,因为这似乎工作(好吧,我没有完全完成,但它非常接近我想要的)。

  var textPath = path.each(function(l){
    var aref = theG.append("a")
        .attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
    var text = aref.append("text")
          .style("fill", function(d) { return "blue"; });
    var textPath = text.append("textPath")
            .attr("startOffset", "50%")
            .attr("xlink:href", "#path"+l.target.name)
            .text("insert");

    var aref2 = theG.append("a")
    .attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
  var text2 = aref2.append("text")
    .style("fill", function(d) { return "blue"; });
  var textPath2 = text2.append("textPath")
    .attr("startOffset", "60%")
    .attr("xlink:href", "#path"+l.target.name)
    .text("delete");
  });