D3.js 为什么我的更新函数要创建一个全新的力定向图?
我正在查看并提供该问题,我无法完全让自己的更新功能正常工作:D3.js 为什么我的更新函数要创建一个全新的力定向图?,d3.js,force-layout,directed-graph,D3.js,Force Layout,Directed Graph,我正在查看并提供该问题,我无法完全让自己的更新功能正常工作: update = function() { path = svg.append("svg:g").selectAll("path") .data(force.links()); path.enter().append("svg:path") .attr("class", "link") .attr("fill", "none") .attr("stroke
update = function() {
path = svg.append("svg:g").selectAll("path")
.data(force.links());
path.enter().append("svg:path")
.attr("class", "link")
.attr("fill", "none")
.attr("stroke", "#666")
.attr("stroke-width",
function(d) {
return (Math.floor(Math.log(d.value - min_val) / Math.log(2)) + 1) + "px"
})
.attr("marker-end", "url(#generic_arrow_marker)")
.on("mouseover",
function(d, i) {
if(d3.select(this).attr("stroke") != "#666") {
mousedOut = false;
}
})
.on("mouseout",
function(d, i) {
if(d3.select(this).attr("stroke") != "#666") {
mousedOut = true;
restoreGraph();
}
});
path.exit().remove();
circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes());
circle.enter().append("svg:circle")
.attr("r",
function(d) {
return (20 + Math.floor(Math.log(d.pagerank) / Math.log(2)) * 2) + "px"
})
.attr("fill", "#ccc")
.attr("stroke", "#333")
.attr("stroke-width", "1.5px")
.on("mouseover", // select relevant data nodes on click
function(d, i) {
mousedOut = false;
d3.select(this).attr("class", "selected");
transitions(d);
$("span#user").text(d.name)
$("span#pagerank").text(d.pagerank)
})
.on("click",
function(d, i) {
incoming = !incoming;
transitions(d);
})
.on("mouseout",
function(d, i) {
mousedOut = true;
d3.select(this).attr("class", "");
restoreGraph();
})
.call(force.drag);
circle.exit().remove();
text = svg.append("svg:g").selectAll("g")
.data(force.nodes());
textEnter = text.enter().append("svg:g");
textEnter.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.text(function(d) { return d.name; })
textEnter.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; })
text.exit().remove();
force.start();
}
无论何时调用update()
,它都会创建现有D3图形的全新副本,即使我没有更改任何内容
关于我可能做错了什么的想法?我一发布问题就发现了 这是因为我有
svg.append(“svg:g”)
用于路径
,圆圈
,以及文本
我想我会把这个问题留给别人,以防它对其他人有帮助