Static and sticky force-directed layout in D3

This is and interesting read. Testing it out today. Will update this post as I proceed with it.

Katie Schick

I’ve been playing around with D3 lately, especially with force-directed graph layouts. For graphs with a lot of nodes, it can become a bit nauseating to have them constantly moving, so I wanted to combine this static force layout, while maintaining the draggability of nodes of this sticky force layout. Here’s what I came up with!

When you’re actually implementing this, you’ll have your own graph of nodes and links, but for this example, let’s use this quick way of generating nodes and links from the above-linked examples:

let n = 100,
    nodes = d3.range(n).map(function() { return {}; }),
    links = d3.range(n).map(d => {
        source: d, target: (d + 3) % n

Next, let’s set up our basic force-directed layout:

let width = 960,
    height = 500;

let force = d3.layout.force()
    .size([width, height]);

let svg ="body")
      .attr("width", width)
      .attr("height", height);

Now, we can define our drag behaviors…

View original post 285 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s