简体   繁体   English

D3.js转换后树中的直链接

[英]D3.js Straight links in tree after transformation

I am trying to make my tree have a straight links between the parent node and the children nodes. 我试图使我的树在父节点和子节点之间具有直接链接。 I have straight links now but the links are not connecting to the right places. 我现在有直接链接,但链接未连接到正确的位置。 I think this may be because there is a transformation of rotation and translate to the nodes and the x and y didn't change somehow. 我认为这可能是因为旋转发生了转换并转换为节点,而x和y并没有发生任何变化。

I have tried following the answer in this question but result is the same. 我试过按照这个问题的答案,但结果是相同的。 D3: Substituting d3.svg.diagonal() with d3.svg.line() D3:用d3.svg.line()替换d3.svg.diagonal()

 var lines = svg.selectAll('line')
      .data(links)
      .enter()
      .append('line')
      .attr('stroke','#000')

lines.attr('x1',function(d){return d.source.x })
    .attr('y1',function(d){return d.source.x})
    .attr('x2',function(d){return d.target.x })
    .attr('y2',function(d){return d.target.y })

Here is the full code: 这是完整的代码:

    var diameter = 1000;

var tree = d3.layout.tree()
    .size([360, diameter / 2 - 100])
    .separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });

 // var diagonal = d3.svg.diagonal.radial()
 // .projection(function(d) { 

 //  return [d.y, d.x ]; })

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter )
  .append("g")
    .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");


d3.json("flare.json", function(error, root) {

  var nodes = tree.nodes(root),
      links = tree.links(nodes);

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


   var lines = svg.selectAll('line')
          .data(links)
          .enter()
          .append('line')
          .attr('stroke','#000')

    lines.attr('x1',function(d){return d.source.x })
        .attr('y1',function(d){return d.source.x})
        .attr('x2',function(d){return d.target.x })
        .attr('y2',function(d){return d.target.y })


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

  node.append("circle")
      .attr("r", 10);


  node.append("text")
      .attr("dy", ".81em")
      .attr("text-anchor", function(d) {
       return d.x < 180 ? "start" : "end"; })
      .attr("transform", function(d) { return d.x < 180 ? "translate(20)" : "rotate(180)translate(-20)"; })
      .text(function(d) { return d.name; });

});

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

screenshots 屏幕截图 屏幕截图1

屏幕截图2

I finally got it to work.. The solution is quite bizarre. 我终于让它工作了。解决方案非常奇怪。 There is no projection method for line as there is for diagonal. 线没有投影方法,对角线没有投影方法。 So when resetting the positions of x1,x2,y1,y2 needs a little bit tuning just like the diagonal projection. 因此,重置x1,x2,y1,y2的位置时需要像对角线投影一样进行一些微调。

Also I have to apply the transformation like how the nodes are applied but without the translation. 另外,我还必须像应用节点一样应用转换,但是不进行转换。

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



var lines = svg.selectAll('line')
      .data(links)
      .enter()
      .append('line')
      .attr('stroke','#000')


lines.attr('x1',function(d){return d.source.y})
    .attr('y1',function(d){return d.source.x/180*Math.PI})
    .attr('x2',function(d){return d.target.y })
    .attr('y2',function(d){return d.target.x/180*Math.PI})

    // lines.attr("transform", function(d) {
    //  return "rotate(" + (d.source.x - 90 ) + ")translate(" + d.source.y + ")"; })

   lines.attr("transform", function(d) {      
     return "rotate(" + (d.target.x - 90 ) + ")"; })

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

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