Skip to content

Commit

Permalink
fix bound for nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitlouis committed Sep 18, 2024
1 parent 01374f4 commit 4309b3b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ function createGraph(data) {
const simulation = d3.forceSimulation(data)
.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collision", d3.forceCollide().radius(d => calculateNodeSize(d) + 2));
.force("collision", d3.forceCollide().radius(d => calculateNodeSize(d) + 2))
.force("x", d3.forceX(width / 2).strength(0.1))
.force("y", d3.forceY(height / 2).strength(0.1));

const nodes = g.selectAll("circle")
.data(data)
Expand Down Expand Up @@ -447,8 +449,8 @@ function createGraph(data) {

simulation.on("tick", () => {
nodes
.attr("cx", d => d.x)
.attr("cy", d => d.y);
.attr("cx", d => boundNode(d.x, 0, width))
.attr("cy", d => boundNode(d.y, 0, height));
});

function dragstarted(event, d) {
Expand All @@ -458,15 +460,19 @@ function createGraph(data) {
}

function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
d.fx = boundNode(event.x, 0, width);
d.fy = boundNode(event.y, 0, height);
}

function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}

function boundNode(val, min, max) {
return Math.max(min, Math.min(max, val));
}
}

function calculateNodeScore(place) {
Expand Down

0 comments on commit 4309b3b

Please sign in to comment.