d3.js:正确链接节点的路径

时间:2015-12-23 14:01:04

标签: javascript d3.js svg

我正在使用d3.js库绘制一些节点并链接它们,但它们没有正确链接它们。我尝试了太多东西,但没有一个给我一个解决方案。

enter image description here

就像您在图像中看到的那样,路径仅在svg:rect的输入位置链接。

 nodeEnter.append("svg:rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 1)
    .attr("height", 1)
    .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });



  nodeEnter.append("svg:text")
      .attr("x", 30)
      .attr("dy", ".35em")
      .attr("text-anchor", "")
      .text(function(d) { return d.name + " " + (d.occurences || ""); })
      .style("fill-opacity", 1e-6);



 nodeEnter.append("svg:image")

   .attr('width', 0)
   .attr('height', 0)
     .style("fill-opacity", 1e-6)
   .attr("xlink:href", function(d) {return d.icon; });


  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });



  nodeUpdate.select("rect")
    .attr("rx", 6)
    .attr("ry", 6)
    .attr("x", 0)
    .attr("y", -12.5)
    .attr("width", function(d){ return  (getWidthOfText(d.name, 'Helvetica Neue', 14)+getWidthOfText(d.ocurrences, 'Helvetica Neue', 14))+30;})
    .attr("height", 25)
    //.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
     .style("filter", "url(#drop-shadow)");


  nodeUpdate.select("text")
  .style("fill-opacity", 1);

  nodeUpdate.select("image")
     .attr("x", 6)
   .attr("y", -7)
  .attr('width', 20)
  .attr('height', 15)
  .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 1)

    .attr("height", 1)
        .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });


  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  vis.selectAll("image")
   .append("svg:title")
   .text(function(d) { return "Descripción: "+d.description+"\n\nComentarios: "+d.comment});

// Update the links…
var link = vis.selectAll("path.link")
  .data(tree.links(nodes), function(d) { return d.target.id; });

// Enter any new links at the parent's previous position.
link.enter().insert("svg:path", "g")
  .attr("class", "link")
  .attr("d", function(d) {
    var o = {x: source.x0, y: source.y0};
    return diagonal({source: o, target: o});
  })
.transition()

  .duration(duration)
  .attr("d", diagonal);


 // Transition links to their new position.
 link.transition()
  .duration(duration)
  .attr("d", diagonal);

// Transition exiting nodes to the parent's new position.
link.exit().transition()

  .duration(duration)
  .attr("d", function(d) {

    var o = {x: source.x, y: source.y};
    return diagonal({source: o, target: o});
  })
  .remove();

// Stash the old positions for transition.
nodes.forEach(function(d) {
  d.x0 = d.x;
  d.y0 = d.y;

});

有没有办法移动路径以正确链接节点?

0 个答案:

没有答案