【发布时间】:2015-07-31 20:33:05
【问题描述】:
我有一个 d3 力图,每次删除节点时都会覆盖(覆盖?)文本。我认为解决这个问题的方法是删除我的 update() 函数中的文本标签。但是,如果屏幕上有很多节点,我不确定性能是否会很重。我也不确定为什么它首先会发生。
以下是我认为与该问题有关的代码片段:
function update() {
// refresh list of selected nodes
selectedNodes = nodes.filter(function(d) { return d.selected; });
// Update link data based on edges array.
link = link.data(edges);
// Create new links
link.enter().append("line")
.attr("class", "link")
.style("stroke-width", 1.5);
// Delete removed links
link.exit().remove();
// Update node data based on nodes array.
node = node.data(nodes);
// Create new nodes
node.enter().append("g")
.attr("class", "node")
.attr("id", function(d) { return d.data['id'] })
//.attr("fixed", function(d) { return d.fixed=true })
.call(force.drag)
.on('mouseover', connectedNodes)
.on('mouseleave', restore)
//.on('dblclick', highlight)
.on('dblclick', highlight);
// Delete removed nodes
node.exit().remove();
node.append("circle").attr("r", 11);
node.classed("selected", function(d) { return d === d.selected; })
// Node behavior for checking if selected otherwise colors nodes to color given from JSON.
node.style("fill", function(d) {
if (d.selected === false) {
console.log("Not Highlighting " + d.data['id'] + " selected is " + d.selected);
return d.data['color']
update();
}
else {
console.log("Highlighting " + d.data['id'] + " selected is " + d.selected);
return "yellow";
update();
}
}).select("circle").style("stroke", "black");
// Link color based on JSON data.
link.style("stroke", function(d) { return d.data['color'] });
// Adds text to nodes
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.style("fill", "black")
.text(function (d) { return d.data['label']; });
// Creates an index used to figure out neighbor nodes.
root.edges.forEach(function (d) {
linkedByIndex[d.data.source + "," + d.data.target] = 1;
});
// responsive behavior for graph based on window.
window.addEventListener('resize', resize);
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
force.start();
}
// Delete node with prompt
function deleteNode() {
console.log("Prompted to delete selected nodes.");
if (confirm("Deleting selected element(s) will remove them from the graph entirely.\nAre you sure? (This cannot be undone).")) {
if (selectedNodes.length > 0) {
for (var i = 0; i < selectedNodes.length; i++) {
selectedNodes[i].removed = true;
nodes.splice(nodes.indexOf(selectedNodes[i]), 1);
spliceLinksForNode(selectedNodes[i]);
}
}
else alert("No node(s) selected.");
update();
}
}
function spliceLinksForNode(node) {
toSplice = edges.filter(
function(e) {
return (e.source === node) || (e.target === node); });
toSplice.map(
function(e) {
edges.splice(edges.indexOf(e), 1); });
}
这是删除节点后的截图。
【问题讨论】:
-
看起来您的选择...输入...附加模式有问题。在这里阅读更多。 bl.ocks.org/mbostock/3808218 看起来您没有正确更新。需要小提琴或块来为您提供更多帮助。
-
bl.ocks.org/joeycf/f021e60bb38846dcfaf2 这是我大部分代码的 sn-p。要重新创建我的场景...双击一个节点(如诗人,或者您可以选择多个),然后单击删除按钮。
-
我一直在寻找更新节点文本的方法.. 仍然没有成功,你设法做到了吗@Joey?
-
@coiso 不,不幸的是,我一直无法弄清楚。如果您确实弄清楚了,请更新我或回答这个问题。
标签: javascript d3.js force-layout