<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.5/d3.min.js"></script>
<style>
svg {
border: 1px black solid;
}
</style>
</head>
<body>
<div>
<div>
<button id='btnRenderExisting2Node'>Render 2 existing nodes</button>
<button id='btnRenderExisting3Node'>Render 3 existing nodes</button>
<button id='btnRenderNewNode'>Render 3 existing nodes and 1 brand new node</button>
</div>
<svg width="250" height="250"></svg>
</div>
<script>
var svg = d3.select("svg"),
width = 250,
height = 250,
color = d3.scaleOrdinal(d3.schemeCategory10);
var a = {
id: "a"
},
b = {
id: "b"
},
c = {
id: "c"
},
nodes = [a, b, c],
l_ab = {
source: a,
target: b
},
l_bc = {
source: b,
target: c
},
l_ca = {
source: c,
target: a
},
links = [l_ab, l_bc, l_ca];
var id = 0;
var simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody())
.force('link', d3.forceLink().links(links).strength(d => {
console.log(d);
return 1;
}))
.force('center', d3.forceCenter(width / 2, height / 2))
.alphaTarget(1)
.on("tick", ticked)
.stop();
var g = svg.append("g"),
link = g.append("g").attr("stroke", "#000").attr("stroke-width", 1.5).selectAll(".link"),
node = g.append("g").attr("stroke", "#fff").attr("stroke-width", 1.5).selectAll(".node");
function restart() {
node = node.data(nodes);
link = link.data(links);
node.exit().transition().attr("r", 0).remove();
link.exit().transition().attr("stroke-opacity", 0).remove();
node = node.enter().append("circle")
.attr("fill", d => color(d.id))
.call(node => node.transition().attr("r", 8))
.merge(node);
link = link.enter().append("line")
.call(link => link.transition().attr("stroke-opacity", 1))
.merge(link);
simulation.nodes(nodes);
simulation.force("link").links(links)
.strength(d => {
console.log(d);
return 1;
});
simulation.alpha(1).restart();
}
function ticked() {
node.attr("cx", d => d.x)
.attr("cy", d => d.y);
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);
}
restart();
document.getElementById('btnRenderExisting3Node').addEventListener('click', function() {
nodes = [a, b, c];
links = [l_ab, l_bc, l_ca];
restart();
});
document.getElementById('btnRenderExisting2Node').addEventListener('click', function() {
nodes = [a, b];
links = [l_ab];
restart();
});
document.getElementById('btnRenderNewNode').addEventListener('click', function() {
var d = {
id: id++,
x: width / 2,
y: height / 2
};
nodes = [a, b, c, d];
links = [l_ab, l_bc, l_ca, {
source: a,
target: d
}];
restart();
});
</script>
</body>
</html>
I need to refresh a d3 force graph with new data and also have a custom link strength, based on the properties of the edges.
This issue I run into happens when I use a custom link strength function and restart the simulation using the restart() function.
simulation.force("link").links(links)
.strength(d => {
console.log(d);
return 1;
});
The first time this runs, d will iterate over a different number of links and has unexpected behavior. Likewise, I find that if I try to redefine the strength function to return a different value, it keeps resorting to the original function.
Run 1
>>{source: {…}, target: {…}, index: 0}
>>{source: {…}, target: {…}, index: 1}
>>{source: {…}, target: {…}, index: 2}
Run 2
>>{source: {…}, target: {…}, index: 1}
>>{source: {…}, target: {…}, index: 2}
>>{source: {…}, target: {…}, index: 0}
>>{source: {…}, target: {…}, index: 1}
>>{source: {…}, target: {…}, index: 2}
>>{source: {…}, target: {…}, index: 3}
>>{source: {…}, target: {…}, index: 0}
>>{source: {…}, target: {…}, index: 1}
>>{source: {…}, target: {…}, index: 2}
>>{source: {…}, target: {…}, index: 3}
Run 3
>>{source: {…}, target: {…}, index: 0}
>>{source: {…}, target: {…}, index: 1}
>>{source: {…}, target: {…}, index: 2}
>>{source: {…}, target: {…}, index: 3}
>>{source: {…}, target: {…}, index: 0}
>>{source: {…}, target: {…}, index: 0}
It is seemingly unpredictable what it will output and sometimes uses data that isn’t from the current dataset. Why is this happening and is there anything I can do to mitigate the problem?
Full HTML Code:
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
svg {
border: 1px black solid;
}
</style>
</head>
<body>
<div>
<div>
<button id='btnRenderExisting2Node'>Render 2 existing nodes</button>
<button id='btnRenderExisting3Node'>Render 3 existing nodes</button>
<button id='btnRenderNewNode'>Render 3 existing nodes and 1 brand new node</button>
</div>
<svg width="250" height="250"></svg>
</div>
<script>
var test = true;
var svg = d3.select("svg"),
width = 250,
height = 250,
color = d3.scaleOrdinal(d3.schemeCategory10);
var a = {
id: "a"
},
b = {
id: "b"
},
c = {
id: "c"
},
nodes = [a, b, c],
l_ab = {
source: a,
target: b
},
l_bc = {
source: b,
target: c
},
l_ca = {
source: c,
target: a
}
links = [l_ab, l_bc, l_ca];
var id = 0;
var simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody())
.force('link', d3.forceLink())
.force('center', d3.forceCenter(width / 2, height / 2))
.alphaTarget(1)
.on("tick", ticked)
.stop();
var g = svg.append("g"),
link = g.append("g").attr("stroke", "#000").attr("stroke-width", 1.5).selectAll(".link"),
node = g.append("g").attr("stroke", "#fff").attr("stroke-width", 1.5).selectAll(".node");
function restart() {
// Updates the node & link data after nodes & links update
node = node.data(nodes);
link = link.data(links);
console.log(node);
console.log(link);
// Select all nodes & links that need removal
// after data update mismatch with DOM
node.exit().transition()
.attr("r", 0)
.remove();
link.exit().transition()
.attr("stroke-opacity", 0)
.attrTween("x1", d => {
return function () {
return d.source.x;
};
})
.attrTween("x2", d => {
return function () {
return d.target.x;
};
})
.attrTween("y1", d => {
return function () {
return d.source.y;
};
})
.attrTween("y2", d => {
return function () {
return d.target.y;
};
})
.remove();
node = node.enter().append("circle")
.attr("fill", d => color(d.id))
.call(node => node.transition().attr("r", 8))
.merge(node);
link = link.enter().append("line")
.call(link => link.transition().attr("stroke-opacity", 1))
.merge(link);
// Update and restart the simulation.
simulation.nodes(nodes);
simulation.force("link").links(links)
.strength(d => {
console.log(d);
return 1;
});
simulation.alpha(1).restart();
}
function ticked() {
node.attr("cx", d => d.x)
.attr("cy", d => d.y);
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);
}
restart();
document.getElementById('btnRenderExisting3Node').addEventListener('click', function () {
nodes = [a, b, c];
links = [l_ab, l_bc, l_ca];
restart();
});
document.getElementById('btnRenderExisting2Node').addEventListener('click', function () {
nodes = [a, b];
links = [l_ab];
restart();
});
document.getElementById('btnRenderNewNode').addEventListener('click', function () {
var d = {
id: id++,
x: width / 2,
y: height / 2
};
nodes = [a, b, c, d];
links = [l_ab, l_bc, l_ca, {
source: a,
target: d
}];
restart();
});
</script>
</body>
</html>