.data() 中的 D3 新数据使 svg 重绘而不是更新节点位置
D3 new data at .data() makes svg to redraw instead of updating nodes position
考虑到此工作example,数据更新使 svg 更新而无需重新绘制。相关的原始代码部分使它的行为如所解释的那样:
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// DATA JOIN
node = node.data(graph.nodes);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
但是,如果节点和链接数据引用是新的,即使内容相同,每次更新都会重新绘制 svg。
相关更改的代码部分使它的行为如解释的那样:
var new_graph_http_data = fetchNewData(); // unreferenced new data copy from graph
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
Here,在函数 update() 中,调用 update_local() 将使图形按预期工作。调用 update_http() 将使图形重新绘制。
// Local working vs fetch (clone)
function update() {
/* Original working version */
// update_local();
/* Re-drawing version */
update_http();
};
update();
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
/* Style Definitions */
button {
position: absolute;
top: 1em;
left: 1em;
}
.node {
stroke: white;
stroke-width: 2px;
}
.link {
stroke: gray;
stroke-width: 2px;
}
</style>
<button type="button" id="switch-btn">Switch Links</button>
<svg width="500" height="300"></svg>
<!--<script src="https://d3js.org/d3.v4.min.js"></script>-->
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// graph data store
// Part of original blocks-data.json
var graph = {
"nodes": [
{ "id": "0", "group": "1" },
{ "id": "1", "group": "2" },
{ "id": "2", "group": "2" },
{ "id": "3", "group": "2" },
{ "id": "4", "group": "2" },
{ "id": "5", "group": "3" },
{ "id": "6", "group": "3" },
{ "id": "7", "group": "3" },
{ "id": "8", "group": "3" }
],
"links1": [
{ "source": "0", "target": "1"},
{ "source": "0", "target": "2"},
{ "source": "0", "target": "3"},
{ "source": "0", "target": "4"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"},
{ "source": "1", "target": "8"},
{ "source": "2", "target": "5"},
{ "source": "3", "target": "6"},
{ "source": "4", "target": "7"}
],
"links2": [
{ "source": "0", "target": "5"},
{ "source": "0", "target": "6"},
{ "source": "0", "target": "7"},
{ "source": "0", "target": "8"},
{ "source": "5", "target": "6"},
{ "source": "5", "target": "8"},
{ "source": "7", "target": "6"},
{ "source": "7", "target": "8"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"}
]
};
// state variable for current link set
var firstLinks = true;
// svg and sizing
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// d3 color scheme
var color = d3.scaleOrdinal(d3.schemeCategory10);
// elements for data join
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
// simulation initialization
var simulation = d3.forceSimulation()
.force("link", d3.forceLink()
.id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody()
.strength(function(d) { return -500;}))
.force("center", d3.forceCenter(width / 2, height / 2));
// button event handling
d3.select("#switch-btn").on("click", function() {
firstLinks = !firstLinks;
update();
});
// follow v4 general update pattern
function update_local() {
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(graph.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
simulation.alphaTarget(0.3).restart();
}
// follow v4 general update pattern
function update_http() {
var new_graph_http_data = fetchNewData();
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(new_graph_http_data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
simulation.alphaTarget(0.3).restart();
}
// drag event handlers
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// tick event handler (nodes bound to container)
function ticked() {
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Value copying function
function fetchNewData() {
var idx, tempStr;
var obj = {
"nodes": [],
"links1": [],
"links2": [],
};
for (idx in graph.nodes) {
tempStr = JSON.stringify(graph.nodes[idx]);
tempStr = JSON.parse(tempStr);
obj.nodes.push(tempStr);
}
for (idx in graph.links1) {
tempStr = JSON.stringify(graph.links1[idx]);
tempStr = JSON.parse(tempStr);
obj.links1.push(tempStr);
}
for (idx in graph.links2) {
tempStr = JSON.stringify(graph.links2[idx]);
tempStr = JSON.parse(tempStr);
obj.links2.push(tempStr);
}
return obj;
}
</script>
</html>
在 Firefox 78.6.0esr 和 Chrome 86.0.4240.111 中使用 D3 v4.13.0 和 D3 v6.3.1 进行了测试。开发工具上没有错误。
谢谢。
虽然您关注的是 enter/update/exit 循环,但问题在于 d3-force:代表节点的对象本身存储了它们自己的位置数据。
D3-force 修改每个节点的数据以包含位置属性。这些属性是 tick 函数用来放置节点的属性:
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
})
除了d.x
和d.y
,还有d.vx
和d.vy
属性。如果一个节点没有这些属性,力将创建并分配它们。如果您删除所有节点并用没有 x
,y
属性的新对象替换它们,力将创建和初始化这些属性而不考虑以前的节点(毕竟,它们不是不再模拟。在视觉上,这将使它看起来像所有旧的 nodes/links 已退出并进入新的)。
如果您通过(重新)加载节点用新对象替换对象,并且新对象旨在位于旧节点所在的位置,则可以转移节点的 x
、y
,dx
,dy
属性,如果你想从旧节点到新对象,例如:
var old = simulation.nodes()
newGraph.nodes.forEach(function(node,i) {
node.x = old[i].x;
node.y = old[i].y;
node.vx = old[i].vx;
node.vy = old[i].vy;
})
// Local working vs fetch (clone)
function update() {
/* Original working version */
// update_local();
/* Re-drawing version */
update_http();
};
update();
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
/* Style Definitions */
button {
position: absolute;
top: 1em;
left: 1em;
}
.node {
stroke: white;
stroke-width: 2px;
}
.link {
stroke: gray;
stroke-width: 2px;
}
</style>
<button type="button" id="switch-btn">Switch Links</button>
<svg width="500" height="300"></svg>
<!--<script src="https://d3js.org/d3.v4.min.js"></script>-->
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// graph data store
// Part of original blocks-data.json
var graph = {
"nodes": [
{ "id": "0", "group": "1" },
{ "id": "1", "group": "2" },
{ "id": "2", "group": "2" },
{ "id": "3", "group": "2" },
{ "id": "4", "group": "2" },
{ "id": "5", "group": "3" },
{ "id": "6", "group": "3" },
{ "id": "7", "group": "3" },
{ "id": "8", "group": "3" }
],
"links1": [
{ "source": "0", "target": "1"},
{ "source": "0", "target": "2"},
{ "source": "0", "target": "3"},
{ "source": "0", "target": "4"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"},
{ "source": "1", "target": "8"},
{ "source": "2", "target": "5"},
{ "source": "3", "target": "6"},
{ "source": "4", "target": "7"}
],
"links2": [
{ "source": "0", "target": "5"},
{ "source": "0", "target": "6"},
{ "source": "0", "target": "7"},
{ "source": "0", "target": "8"},
{ "source": "5", "target": "6"},
{ "source": "5", "target": "8"},
{ "source": "7", "target": "6"},
{ "source": "7", "target": "8"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"}
]
};
// state variable for current link set
var firstLinks = true;
// svg and sizing
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// d3 color scheme
var color = d3.scaleOrdinal(d3.schemeCategory10);
// elements for data join
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
// simulation initialization
var simulation = d3.forceSimulation()
.force("link", d3.forceLink()
.id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody()
.strength(function(d) { return -500;}))
.force("center", d3.forceCenter(width / 2, height / 2));
// button event handling
d3.select("#switch-btn").on("click", function() {
firstLinks = !firstLinks;
update();
});
// follow v4 general update pattern
function update_local() {
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(graph.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
simulation.alphaTarget(0.3).restart();
}
// follow v4 general update pattern
function update_http() {
var new_graph_http_data = fetchNewData();
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(new_graph_http_data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
simulation.alphaTarget(0.3).restart();
}
// drag event handlers
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// tick event handler (nodes bound to container)
function ticked() {
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Value copying function
function fetchNewData() {
var idx, tempStr;
var obj = {
"nodes": [],
"links1": [],
"links2": [],
};
for (idx in graph.nodes) {
tempStr = JSON.stringify(graph.nodes[idx]);
tempStr = JSON.parse(tempStr);
obj.nodes.push(tempStr);
}
for (idx in graph.links1) {
tempStr = JSON.stringify(graph.links1[idx]);
tempStr = JSON.parse(tempStr);
obj.links1.push(tempStr);
}
for (idx in graph.links2) {
tempStr = JSON.stringify(graph.links2[idx]);
tempStr = JSON.parse(tempStr);
obj.links2.push(tempStr);
}
if(simulation.nodes().length) {
var old = simulation.nodes()
obj.nodes.forEach(function(node,i) {
node.x = old[i].x;
node.y = old[i].y;
node.vx = old[i].vx;
node.vy = old[i].vy;
})
}
return obj;
}
</script>
</html>
考虑到此工作example,数据更新使 svg 更新而无需重新绘制。相关的原始代码部分使它的行为如所解释的那样:
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// DATA JOIN
node = node.data(graph.nodes);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
但是,如果节点和链接数据引用是新的,即使内容相同,每次更新都会重新绘制 svg。 相关更改的代码部分使它的行为如解释的那样:
var new_graph_http_data = fetchNewData(); // unreferenced new data copy from graph
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
Here,在函数 update() 中,调用 update_local() 将使图形按预期工作。调用 update_http() 将使图形重新绘制。
// Local working vs fetch (clone)
function update() {
/* Original working version */
// update_local();
/* Re-drawing version */
update_http();
};
update();
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
/* Style Definitions */
button {
position: absolute;
top: 1em;
left: 1em;
}
.node {
stroke: white;
stroke-width: 2px;
}
.link {
stroke: gray;
stroke-width: 2px;
}
</style>
<button type="button" id="switch-btn">Switch Links</button>
<svg width="500" height="300"></svg>
<!--<script src="https://d3js.org/d3.v4.min.js"></script>-->
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// graph data store
// Part of original blocks-data.json
var graph = {
"nodes": [
{ "id": "0", "group": "1" },
{ "id": "1", "group": "2" },
{ "id": "2", "group": "2" },
{ "id": "3", "group": "2" },
{ "id": "4", "group": "2" },
{ "id": "5", "group": "3" },
{ "id": "6", "group": "3" },
{ "id": "7", "group": "3" },
{ "id": "8", "group": "3" }
],
"links1": [
{ "source": "0", "target": "1"},
{ "source": "0", "target": "2"},
{ "source": "0", "target": "3"},
{ "source": "0", "target": "4"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"},
{ "source": "1", "target": "8"},
{ "source": "2", "target": "5"},
{ "source": "3", "target": "6"},
{ "source": "4", "target": "7"}
],
"links2": [
{ "source": "0", "target": "5"},
{ "source": "0", "target": "6"},
{ "source": "0", "target": "7"},
{ "source": "0", "target": "8"},
{ "source": "5", "target": "6"},
{ "source": "5", "target": "8"},
{ "source": "7", "target": "6"},
{ "source": "7", "target": "8"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"}
]
};
// state variable for current link set
var firstLinks = true;
// svg and sizing
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// d3 color scheme
var color = d3.scaleOrdinal(d3.schemeCategory10);
// elements for data join
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
// simulation initialization
var simulation = d3.forceSimulation()
.force("link", d3.forceLink()
.id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody()
.strength(function(d) { return -500;}))
.force("center", d3.forceCenter(width / 2, height / 2));
// button event handling
d3.select("#switch-btn").on("click", function() {
firstLinks = !firstLinks;
update();
});
// follow v4 general update pattern
function update_local() {
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(graph.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
simulation.alphaTarget(0.3).restart();
}
// follow v4 general update pattern
function update_http() {
var new_graph_http_data = fetchNewData();
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(new_graph_http_data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
simulation.alphaTarget(0.3).restart();
}
// drag event handlers
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// tick event handler (nodes bound to container)
function ticked() {
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Value copying function
function fetchNewData() {
var idx, tempStr;
var obj = {
"nodes": [],
"links1": [],
"links2": [],
};
for (idx in graph.nodes) {
tempStr = JSON.stringify(graph.nodes[idx]);
tempStr = JSON.parse(tempStr);
obj.nodes.push(tempStr);
}
for (idx in graph.links1) {
tempStr = JSON.stringify(graph.links1[idx]);
tempStr = JSON.parse(tempStr);
obj.links1.push(tempStr);
}
for (idx in graph.links2) {
tempStr = JSON.stringify(graph.links2[idx]);
tempStr = JSON.parse(tempStr);
obj.links2.push(tempStr);
}
return obj;
}
</script>
</html>
在 Firefox 78.6.0esr 和 Chrome 86.0.4240.111 中使用 D3 v4.13.0 和 D3 v6.3.1 进行了测试。开发工具上没有错误。
谢谢。
虽然您关注的是 enter/update/exit 循环,但问题在于 d3-force:代表节点的对象本身存储了它们自己的位置数据。
D3-force 修改每个节点的数据以包含位置属性。这些属性是 tick 函数用来放置节点的属性:
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
})
除了d.x
和d.y
,还有d.vx
和d.vy
属性。如果一个节点没有这些属性,力将创建并分配它们。如果您删除所有节点并用没有 x
,y
属性的新对象替换它们,力将创建和初始化这些属性而不考虑以前的节点(毕竟,它们不是不再模拟。在视觉上,这将使它看起来像所有旧的 nodes/links 已退出并进入新的)。
如果您通过(重新)加载节点用新对象替换对象,并且新对象旨在位于旧节点所在的位置,则可以转移节点的 x
、y
,dx
,dy
属性,如果你想从旧节点到新对象,例如:
var old = simulation.nodes()
newGraph.nodes.forEach(function(node,i) {
node.x = old[i].x;
node.y = old[i].y;
node.vx = old[i].vx;
node.vy = old[i].vy;
})
// Local working vs fetch (clone)
function update() {
/* Original working version */
// update_local();
/* Re-drawing version */
update_http();
};
update();
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
/* Style Definitions */
button {
position: absolute;
top: 1em;
left: 1em;
}
.node {
stroke: white;
stroke-width: 2px;
}
.link {
stroke: gray;
stroke-width: 2px;
}
</style>
<button type="button" id="switch-btn">Switch Links</button>
<svg width="500" height="300"></svg>
<!--<script src="https://d3js.org/d3.v4.min.js"></script>-->
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// graph data store
// Part of original blocks-data.json
var graph = {
"nodes": [
{ "id": "0", "group": "1" },
{ "id": "1", "group": "2" },
{ "id": "2", "group": "2" },
{ "id": "3", "group": "2" },
{ "id": "4", "group": "2" },
{ "id": "5", "group": "3" },
{ "id": "6", "group": "3" },
{ "id": "7", "group": "3" },
{ "id": "8", "group": "3" }
],
"links1": [
{ "source": "0", "target": "1"},
{ "source": "0", "target": "2"},
{ "source": "0", "target": "3"},
{ "source": "0", "target": "4"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"},
{ "source": "1", "target": "8"},
{ "source": "2", "target": "5"},
{ "source": "3", "target": "6"},
{ "source": "4", "target": "7"}
],
"links2": [
{ "source": "0", "target": "5"},
{ "source": "0", "target": "6"},
{ "source": "0", "target": "7"},
{ "source": "0", "target": "8"},
{ "source": "5", "target": "6"},
{ "source": "5", "target": "8"},
{ "source": "7", "target": "6"},
{ "source": "7", "target": "8"},
{ "source": "1", "target": "5"},
{ "source": "2", "target": "6"},
{ "source": "3", "target": "7"},
{ "source": "4", "target": "8"}
]
};
// state variable for current link set
var firstLinks = true;
// svg and sizing
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// d3 color scheme
var color = d3.scaleOrdinal(d3.schemeCategory10);
// elements for data join
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
// simulation initialization
var simulation = d3.forceSimulation()
.force("link", d3.forceLink()
.id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody()
.strength(function(d) { return -500;}))
.force("center", d3.forceCenter(width / 2, height / 2));
// button event handling
d3.select("#switch-btn").on("click", function() {
firstLinks = !firstLinks;
update();
});
// follow v4 general update pattern
function update_local() {
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? graph.links1 : graph.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(graph.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? graph.links1 : graph.links2);
simulation.alphaTarget(0.3).restart();
}
// follow v4 general update pattern
function update_http() {
var new_graph_http_data = fetchNewData();
// Update link set based on current state
// DATA JOIN
link = link.data(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
// EXIT
// Remove old links
link.exit().remove();
// ENTER
// Create new links as needed.
link = link.enter().append("line")
.attr("class", "link")
.merge(link);
// DATA JOIN
node = node.data(new_graph_http_data.nodes);
// EXIT
node.exit().remove();
// ENTER
node = node.enter().append("circle")
.attr("class", "node")
.attr("r", 10)
.attr("fill", function(d) {return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.merge(node);
// Set nodes, links, and alpha target for simulation
simulation
.nodes(new_graph_http_data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(firstLinks ? new_graph_http_data.links1 : new_graph_http_data.links2);
simulation.alphaTarget(0.3).restart();
}
// drag event handlers
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// tick event handler (nodes bound to container)
function ticked() {
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Value copying function
function fetchNewData() {
var idx, tempStr;
var obj = {
"nodes": [],
"links1": [],
"links2": [],
};
for (idx in graph.nodes) {
tempStr = JSON.stringify(graph.nodes[idx]);
tempStr = JSON.parse(tempStr);
obj.nodes.push(tempStr);
}
for (idx in graph.links1) {
tempStr = JSON.stringify(graph.links1[idx]);
tempStr = JSON.parse(tempStr);
obj.links1.push(tempStr);
}
for (idx in graph.links2) {
tempStr = JSON.stringify(graph.links2[idx]);
tempStr = JSON.parse(tempStr);
obj.links2.push(tempStr);
}
if(simulation.nodes().length) {
var old = simulation.nodes()
obj.nodes.forEach(function(node,i) {
node.x = old[i].x;
node.y = old[i].y;
node.vx = old[i].vx;
node.vy = old[i].vy;
})
}
return obj;
}
</script>
</html>