首页 > 解决方案 > 在 a 内的两个元素之间放置一条线有缩放事件?

问题描述

我有这段代码,并且我有一个算法可以在两个nodes. 我希望这条线将 the#nodo4与 the连接起来#nodo6,并且每个rectanglesnodes与其同名id

代码有点长,但实现这一点的重要部分在这里:

setTimeout(() => {
    let source = d3.select("#node4");
    let target = d3.select("#node6");
    source.datum(source.node().getBoundingClientRect())
        .attr('nodeX', d => d.x + d.width / 2)
        .attr('nodeY', d => d.y + d.height / 2)

    target.datum(target.node().getBoundingClientRect())
        .attr('nodeX', d => d.x + d.width / 2)
        .attr('nodeY', d => d.y + d.height / 2)

    d3.select("#g_main").append("line")
        .style("stroke", "black") // colour the line
        .attr("x1", source.attr('nodeX')) // x position of the first end of the line
        .attr("y1", source.attr('nodeY')) // y position of the first end of the line
        .attr("x2", target.attr('nodeX')) // x position of the second end of the line
        .attr("y2", target.attr('nodeY')); // y position of the second end of the line

}, 5000)

但考虑到我可以不断缩放和平移,我无法让线条出现在应有的位置。我想要一个动态解决方案,因为将来我想放置一条连接到其他节点的线,并且我想做这个动态计算。我究竟做错了什么?

var width = 960,
    height = 800;
var i = 0,
    duration = 750,
    rectW = 100,
    rectH = 30;
var tree = d3.layout.tree().nodeSize([220, 40]);
var diagonal = d3.svg.diagonal()
    .projection(function(d) {
        return [d.x + rectW / 2, d.y + rectH / 2];
    });

var svg = d3.select("#body").append("svg").attr("width", 1000).attr("height", 1000).style("border", "1px solid red")
    .call(zm = d3.behavior.zoom().scaleExtent([0.3, 3]).on("zoom", redraw)).append("g").attr("id", "g_main")
    .attr("transform", "translate(" + 350 + "," + 20 + ")");

//necessary so that zoom knows where to zoom and unzoom from
zm.translate([350, 20]);


var root = {
    "name": "node6",
    "children": [{
            "name": "node5",
            "respuesta": "SI",
            "children": [{
                "name": "node4",
                "children": [{
                    "name": "node3"
                }]
            }]
        }, {
            "name": "node2",
            "respuesta": "NO"
        },

        {
            "name": "node1",
            "respuesta": "SI"
        }
    ]
}
root.x0 = 0;
root.y0 = height / 2;
root.children.forEach(collapse);
update(root);


root.x0 = 0;
root.y0 = height / 2;

setTimeout(() => {
    let source = d3.select("#node4");
    let target = d3.select("#node6");
    source.datum(source.node().getBoundingClientRect())
        .attr('nodeX', d => d.x + d.width / 2)
        .attr('nodeY', d => d.y + d.height / 2)

    target.datum(target.node().getBoundingClientRect())
        .attr('nodeX', d => d.x + d.width / 2)
        .attr('nodeY', d => d.y + d.height / 2)

    d3.select("#g_main").append("line")
        .style("stroke", "black") // colour the line
        .attr("x1", source.attr('nodeX')) // x position of the first end of the line
        .attr("y1", source.attr('nodeY')) // y position of the first end of the line
        .attr("x2", target.attr('nodeX')) // x position of the second end of the line
        .attr("y2", target.attr('nodeY')); // y position of the second end of the line

}, 5000)

function collapse(d) {
    if (d.children) {
        d._children = d.children;
        d._children.forEach(collapse);
        // d.children = null;
    }
}

root.children.forEach(collapse);
update(root);

d3.select("#body").style("height", "800px");

function update(source) {

    // Compute the new tree layout.
    var nodes = tree.nodes(root).reverse(),
        links = tree.links(nodes);

    // Normalize for fixed-depth.
    nodes.forEach(function(d) {
        d.y = d.depth * 180;
    });

    // Update the nodes…
    var node = svg.selectAll("g.node")
        .data(nodes, function(d) {
            return d.id || (d.id = ++i);
        });

    // Enter any new nodes at the parent's previous position.
    var nodeEnter = node.enter().append("g")
        .attr("class", "node")
        .attr("transform", function(d) {
            return "translate(" + source.x0 + "," + source.y0 + ")";
        })

    nodeEnter.append("rect")
        .attr("id", function(d) {
            return "node" + d.id
        })
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "white")
        .attr("stroke-width", 1)
        .style("fill", function(d) {
            return d._children ? "lightsteelblue" : "#fff";
        });

    nodeEnter.append("image").attr("href", "plus-flat.png").attr("width", (d) => {
        let length = 0;
        if (d.children) {
            length = d.children.length;
        } else {
            length = 0;
        }
        if (d.name == "INICIO" && length == 0) {
            return 0;
        } else if (d.name == "INICIO" && length != 0) {
            return 0;
        } else {
            return 25;
        }

    }).style("transform", "translate(65px, -10px)")
    nodeEnter.append("text")
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("dy", ".35em")
        .text(function(d) {
            return d.name;
        })



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

    nodeUpdate.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)
        .style("fill", function(d) {
            return d._children ? "lightsteelblue" : "#fff";
        });

    nodeUpdate.select("text")
        .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.x + "," + source.y + ")";
        })
        .remove();

    nodeExit.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        //.attr("width", bbox.getBBox().width)""
        //.attr("height", bbox.getBBox().height)
        .attr("stroke", "black")
        .attr("stroke-width", 1);

    nodeExit.select("text");

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

    // Enter any new links at the parent's previous position.
    link.enter().insert("path", "g")
        .attr("class", "link")
        .style("stroke", (d) => {
            let respuesta = d.target.respuesta;
            if (respuesta == "SI") {
                return "#2a8841";
            } else if (respuesta == "NO") {
                return "#d44646";

            } else {
                return "#b7b7b7";
            }
        })
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("d", function(d) {
            var o = {
                x: source.x0,
                y: source.y0
            };
            return diagonal({
                source: o,
                target: o
            });
        });


    // 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();
    link.append("text").text("otros")

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

// Toggle children on click.
function click(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
}

//Redraw for zoom
function redraw() {
    //console.log("here", d3.event.translate, d3.event.scale);
    svg.attr("transform",
        "translate(" + d3.event.translate + ")" +
        " scale(" + d3.event.scale + ")");
}
.node {
    cursor: pointer;
}
.node circle {
    fill: #fff;
    stroke: steelblue;
    stroke-width: 1.5px;
}
.node text {
    font: 10px sans-serif;
}
.link {
    fill: none;
    stroke: #ccc;
    stroke-width: 1.5px;
}

body {
    overflow: hidden;
}
<script src="https://d3js.org/d3.v3.js"></script>
<div id="body"></div>

预期的结果,是这样的:

在此处输入图像描述

标签: d3.js

解决方案


不要覆盖datum源和目标。这意味着您将无法重新绘制树,因为您覆盖了它所需的所有值。

另外,不要使用getBoundingClientRect(). 如果您缩放或平移怎么办?它会扭曲,并且线永远不会在正确的位置。

相反,依靠你已经给节点的数据,你已经拥有它,你可以通过.datum()不带参数的调用来访问它!然后,计算节点的位置就没有问题了,无论缩放还是滚动,在你想要的位置添加一条线也没有问题。

var width = 960,
  height = 800;
var i = 0,
  duration = 750,
  rectW = 100,
  rectH = 30;
var tree = d3.layout.tree().nodeSize([220, 40]);
var diagonal = d3.svg.diagonal()
  .projection(function(d) {
    return [d.x + rectW / 2, d.y + rectH / 2];
  });

var svg = d3.select("#body").append("svg").attr("width", 1000).attr("height", 1000).style("border", "1px solid red")
  .call(zm = d3.behavior.zoom().scaleExtent([0.3, 3]).on("zoom", redraw)).append("g").attr("id", "g_main")
  .attr("transform", "translate(" + 350 + "," + 20 + ")");

//necessary so that zoom knows where to zoom and unzoom from
zm.translate([350, 20]);


var root = {
  "name": "node6",
  "children": [{
      "name": "node5",
      "respuesta": "SI",
      "children": [{
        "name": "node4",
        "children": [{
          "name": "node3"
        }]
      }]
    }, {
      "name": "node2",
      "respuesta": "NO"
    },

    {
      "name": "node1",
      "respuesta": "SI"
    }
  ]
}
root.x0 = 0;
root.y0 = height / 2;
root.children.forEach(collapse);
update(root);


root.x0 = 0;
root.y0 = height / 2;

setTimeout(() => {
  let source = d3.select("#node4").datum();
  let target = d3.select("#node6").datum();

  d3.select("#g_main").append("line")
    .style("stroke", "black") // colour the line
    .attr("x1", source.x0 + rectW / 2) // x position of the first end of the line
    .attr("y1", source.y0) // y position of the first end of the line
    .attr("x2", target.x0 + rectW / 2) // x position of the second end of the line
    .attr("y2", target.y0 + rectH); // y position of the second end of the line

}, 1000)

function collapse(d) {
  if (d.children) {
    d._children = d.children;
    d._children.forEach(collapse);
    // d.children = null;
  }
}

root.children.forEach(collapse);
update(root);

d3.select("#body").style("height", "800px");

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
    links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) {
    d.y = d.depth * 180;
  });

  // Update the nodes…
  var node = svg.selectAll("g.node")
    .data(nodes, function(d) {
      return d.id || (d.id = ++i);
    });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
      return "translate(" + source.x0 + "," + source.y0 + ")";
    })

  nodeEnter.append("rect")
    .attr("id", function(d) {
      return "node" + d.id
    })
    .attr("width", rectW)
    .attr("height", rectH)
    .attr("stroke", "white")
    .attr("stroke-width", 1)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  nodeEnter.append("image").attr("href", "plus-flat.png").attr("width", (d) => {
    let length = 0;
    if (d.children) {
      length = d.children.length;
    } else {
      length = 0;
    }
    if (d.name == "INICIO" && length == 0) {
      return 0;
    } else if (d.name == "INICIO" && length != 0) {
      return 0;
    } else {
      return 25;
    }

  }).style("transform", "translate(65px, -10px)")
  nodeEnter.append("text")
    .attr("x", rectW / 2)
    .attr("y", rectH / 2)
    .attr("dy", ".35em")
    .text(function(d) {
      return d.name;
    })



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

  nodeUpdate.select("rect")
    .attr("width", rectW)
    .attr("height", rectH)
    .attr("stroke", "black")
    .attr("stroke-width", 1)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  nodeUpdate.select("text")
    .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.x + "," + source.y + ")";
    })
    .remove();

  nodeExit.select("rect")
    .attr("width", rectW)
    .attr("height", rectH)
    //.attr("width", bbox.getBBox().width)""
    //.attr("height", bbox.getBBox().height)
    .attr("stroke", "black")
    .attr("stroke-width", 1);

  nodeExit.select("text");

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

  // Enter any new links at the parent's previous position.
  link.enter().insert("path", "g")
    .attr("class", "link")
    .style("stroke", (d) => {
      let respuesta = d.target.respuesta;
      if (respuesta == "SI") {
        return "#2a8841";
      } else if (respuesta == "NO") {
        return "#d44646";

      } else {
        return "#b7b7b7";
      }
    })
    .attr("x", rectW / 2)
    .attr("y", rectH / 2)
    .attr("d", function(d) {
      var o = {
        x: source.x0,
        y: source.y0
      };
      return diagonal({
        source: o,
        target: o
      });
    });


  // 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();
  link.append("text").text("otros")

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

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

//Redraw for zoom
function redraw() {
  //console.log("here", d3.event.translate, d3.event.scale);
  svg.attr("transform",
    "translate(" + d3.event.translate + ")" +
    " scale(" + d3.event.scale + ")");
}
.node {
  cursor: pointer;
}

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node text {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

body {
  overflow: hidden;
}
<script src="https://d3js.org/d3.v3.js"></script>
<div id="body"></div>


推荐阅读