gpt4 book ai didi

javascript - D3 力导向图添加新节点导致 x & y 为 NaN

转载 作者:行者123 更新时间:2023-11-30 14:23:59 25 4
gpt4 key购买 nike

当我点击一个节点时,我想要一个新的节点被添加到它上面。它们都应该有标签(我正在尝试构建同义词库可视化)。

我是 D3 的新手,所以如果您需要更详细地解释一些事情,我深表歉意。

到目前为止,这是我的代码:

var width = 960;
var height = 500;

var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height])
.nodes([{ "name": "One", "group": 1 }])
.start();

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var nodes = force.nodes();
var links = force.links();

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

var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(force.drag);

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

node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name);

force.on("tick", function() {
link.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node.attr("transform", d => {
// d.x & d.y are NaN for new nodes
return "translate(" + d.x + "," + d.y + ")";
});
});

restart();

function restart() {
node = node.data(nodes);

node.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 10)
.on("mousedown", onClick);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name);
node.exit().remove();

link = link.data(links);
}

function onClick(clicked_node) {
console.log("click!");
console.log(clicked_node);
var new_node = { name: "Test", group: 2 };
nodes.push(new_node);
// Has x & y set to NaN after adding
links.push({ source: clicked_node, target: new_node });
restart();
}

我一点击第一个节点,就导致该节点

{ name: "Test", group: 2 };

待补充,D3内部抛出错误

node.attr("transform", d => {
// d.x & d.y are NaN for new nodes
return "translate(" + d.x + "," + d.y + ")";
});

因为这个新节点的d.xd.yNaN

我尝试明确设置它们:

{ name: "Test", group: 2, x: clicked_node.y, y: clicked_node.y };

但是我得到了同样的错误。在检查器中,当此节点添加到屏幕时,xy 值变为 pxpy 取而代之的是值(value)观!

我不明白为什么会这样。

最佳答案

您缺少一行代码。添加新节点后,您需要重新启动 d3 的模拟以计算其位置:

function restart() {
node = node.data(nodes);

node.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 10)
.on("mousedown", onClick);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name);
node.exit().remove();

link = link.data(links);

force.start(); //<-- start simulation
}

运行代码:

<!DOCTYPE html>
<html>

<head>
<script data-require="d3@3.5.17" data-semver="3.5.17" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
</head>

<body>
<script>
var width = 960;
var height = 500;

var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height])
.nodes([{ "name": "One", "group": 1 }])
.start();

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var nodes = force.nodes();
var links = force.links();

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

var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(force.drag);

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

node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name);

force.on("tick", function() {
link.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node.attr("transform", d => {
// d.x & d.y are NaN for new nodes
return "translate(" + d.x + "," + d.y + ")";
});
});

restart();

function restart() {
node = node.data(nodes);

node.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 10)
.on("mousedown", onClick);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name);
node.exit().remove();

link = link.data(links);

force.start();
}

function onClick(clicked_node) {
console.log("click!");
console.log(clicked_node);
var new_node = { name: "Test", group: 2 };
nodes.push(new_node);
// Has x & y set to NaN after adding
links.push({ source: clicked_node, target: new_node });
restart();
}
</script>
</body>

</html>

关于javascript - D3 力导向图添加新节点导致 x & y 为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52235418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com