d3.js力导向图force实现拖拽时fx和fy使用

最近在看到d3.js实现力导向图中节点的拖拽功能时,用到了下面的代码:

    function dragStarted(d) {
        d.fx = d.x; // <-E
        d.fy = d.y;
    }

    function dragged(d) {
        d.fx = d3.event.x; // <-F
        d.fy = d3.event.y;
    }

    function dragEnded(d) {
        d.fx = null; // <-G
        d.fy = null;
    }

这几个函数使用时非常简单,如下:

        newNodes.forEach(function (node) {
            svg.append("circle")
                    .data([node])
                .attr("class", "node")
                .attr("cx", function (d) {return d.x;})
                .attr("cy", function (d) {return d.y;})
                .attr("r", 1e-6)
                    .call(d3.drag() // <-D
                            .on("start", dragStarted)
                            .on("drag", dragged)
                            .on("end", dragEnded))
                    .transition()
                .attr("r", 7)
                    .transition()
                    .delay(duration)
                .attr("r", 1e-6)
                .on("end", function () {nodes.shift();})
                .remove();
        });

主要疑惑在于,上面自定义的几个函数中,d.fx, d.fy的意义是什么?在拖拽过程中是怎么起作用的呢?

原来,力模拟时,每一个节点是一个object,有这样几个属性(原文如下:https://github.com/d3/d3-force#simulation_nodes

Each node must be an object. The following properties are assigned by the simulation:

  • index - the node’s zero-based index into nodes
  • x - the node’s current x-position
  • y - the node’s current y-position
  • vx - the node’s current x-velocity
  • vy - the node’s current y-velocity

To fix a node in a given position, you may specify two additional properties:

  • fx - the node’s fixed x-position
  • fy - the node’s fixed y-position

At the end of each tick, after the application of any forces, a node with a defined node.fx has node.x reset to this value and node.vx set to zero; likewise, a node with a defined node.fy has node.y reset to this value and node.vy set to zero. To unfix a node that was previously fixed, set node.fx and node.fy to null, or delete these properties.

简单地说就是,如果node的fx,fy值如果设置了,那么x,y就会设为fx,fy,而vx,vy相应设为0,表示节点静止。而如果再次将fx,fy设置为null,或删除这一属性,那么x,x, vx,vy又会再一次受力的作用确定其位置了。

以下是一个简单的D3.js力导向图示例,演示如何添加新节点关系连线: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>D3.js力导向图 - 新增节点关系连线示例</title> <script src="https://d3js.org/d3.v6.min.js"></script> </head> <body> <svg width="800" height="600"></svg> <script> // 定义节点关系数据 var nodes = [ { id: "node1", name: "节点1" }, { id: "node2", name: "节点2" }, { id: "node3", name: "节点3" }, { id: "node4", name: "节点4" } ]; var links = [ { source: "node1", target: "node2" }, { source: "node1", target: "node3" }, { source: "node2", target: "node4" } ]; // 创建力导向图对象 var simulation = d3.forceSimulation(nodes) .force("link", d3.forceLink(links).id(d => d.id)) .force("charge", d3.forceManyBody()) .force("center", d3.forceCenter(400, 300)); // 创建节点关系连线 var svg = d3.select("svg"); var link = svg.selectAll("line") .data(links) .enter().append("line") .attr("stroke", "#999") .attr("stroke-opacity", 0.6) .attr("stroke-width", d => Math.sqrt(d.value)); var node = svg.selectAll("circle") .data(nodes) .enter().append("circle") .attr("r", 10) .attr("fill", "#ccc") .call(drag(simulation)); var label = svg.selectAll("text") .data(nodes) .enter().append("text") .text(d => d.name) .attr("font-size", "12px") .attr("dx", 15) .attr("dy", 4); // 定义拖拽行为 function drag(simulation) { function dragstarted(event, d) { if (!event.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; } function dragged(event, d) { d.fx = event.x; d.fy = event.y; } function dragended(event, d) { if (!event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; } return d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended); } // 新增节点关系连线 var newNode = { id: "node5", name: "节点5" }; var newLink = { source: "node4", target: "node5" }; nodes.push(newNode); links.push(newLink); node = node.data(nodes, d => d.id); node.exit().remove(); node = node.enter().append("circle") .attr("r", 10) .attr("fill", "#ccc") .call(drag(simulation)) .merge(node); label = label.data(nodes, d => d.id); label.exit().remove(); label = label.enter().append("text") .text(d => d.name) .attr("font-size", "12px") .attr("dx", 15) .attr("dy", 4) .merge(label); link = link.data(links); link.exit().remove(); link = link.enter().append("line") .attr("stroke", "#999") .attr("stroke-opacity", 0.6) .attr("stroke-width", d => Math.sqrt(d.value)) .merge(link); // 更新力导向图 simulation.nodes(nodes); simulation.force("link").links(links); simulation.alpha(1).restart(); </script> </body> </html> ``` 在上面的示例中,我们首先定义了一个简单的节点关系数据,并创建了一个力导向图对象。然后,我们使用D3.js创建了节点关系连线的SVG元素,并绑定了数据。 接下来,我们定义了一个拖拽行为,以便用户可以拖动节点。然后,我们添加了一个新节点一个新的关系连线,并使用D3.js更新了节点关系连线的SVG元素。 最后,我们更新了力导向图对象,并重新启动了力导向图的模拟,以确保新节点关系连线被正确地添加到力导向图中。 请注意,这只是一个简单的示例,实际应用中需要根据具体需求进行更复杂的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值