I want to attempt to convert this force from D3 v4 to v7, but not sure how to go about it.The code conversion what I have done is here. I try to do this< https://observablehq.com/@d3/d3v6-migration-guide>
D3 6.0 is out! It comes with exciting new features and improvements.
It also introduces a few important API changes. But, do not fear: almost all the methods are behaving exactly as they did with v5; just a handful of them are not backwards compatible.
Here's a comprehensive list of the changes you will need to watch for when you upgrade to D3 v6:
!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: sans-serif;
font-size: 10px;
}
</style>
<svg width="500" height="500"></svg>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width/3, height / 3));
d3.json("helpme.json", function(error, graph) {
if (error) throw error;
const link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d[2])*2; });
const node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter().append("g")
const circles = node.append("circle")
.attr("r", 6)
.attr("fill", "olive");
// Create a drag handler and append it to the node object instead
const drag_handler = d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
drag_handler(node);
const lables = node.append("text")
.text(function(d) {
return d.id;
})
.attr('x', 6)
.attr('y', 3);
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
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("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
}
});
function dragstarted(event,d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event,d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event,d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
helpme.json
{
"nodes": [
{"id": "A"},
{"id": "B"},
{"id": "C"},
{"id": "D"},
{"id": "E"},
{"id": "F"}
],
"links": [
{"source": "C", "target": "B", "value": 2},
{"source": "C", "target": "D", "value": 3},
{"source": "D", "target": "A", "value": 1},
{"source": "D", "target": "E", "value": 1},
{"source": "E", "target": "F", "value": 1},
{"source": "F", "target": "B", "value": 4}
]
}
Can someone please help me fix this?Some assistance would be amazing! Danke in Voraus
Have a look at their full migration guide: https://github.com/d3/d3/blob/main/CHANGES.md
I would recommend going major by major.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With