In this example we’ll keep the force directed graph constrained inside a box. The nodes won’t be able to go outside.

The basic gist of it is that you impose a constraint that takes effect on every tick of the simulation.  You can either put this code inside the tick function, or you can define a custom force to do the work for you.

Let’s go through both methods.

Using the tick function

The first method is to put the constraints inside the tick function. Click above to see this method in action!

Here’s the relevant code.

Using a custom force

The other option is to define a custom force that applies on every tick.  Like before, click on the above picture to see this method in action.

Look at the below code:

We define the custom force box_force()  that implements the constraint for us. In this custom force we loop through every node and update its x and y position to be constrained inside the box.

You then add the force to the simulation like you’d apply any other force.

We also need to ensure that we can’t drag the nodes out of the graph ourselves with the mouse. To fix this we can modify the drag function to impose the same constraint as the custom force:

Click here if you need a reminder of how drag and drop works in d3.


There’s a few differences between the two methods.

One effect of doing it the second way is that the geometric constraint isn’t as rigidly enforced as in the first method. It’s more of a “soft” force, so you’ll get some overlap of the node at the borders.

This is why you’ve got the “iterations” parameter for the collideForce and the linkForce – to have the option to negate the soft force. We haven’t implemented the iterations parameter here in this custom function.

There’s also some differences in the behaviour of nodes near the borders. Experiment with the two and see for yourself!


Hope you found that useful! Click here to view to the rest of the force directed graph series.

2 thoughts on “Bounding Box Force Directed Graph

  1. Thank you very much! One note: The second picture is pointing to the same link as first.

Leave a Reply

Your email address will not be published. Required fields are marked *