Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing D3 force layout

Is it possible to resize a force layout in d3.js? For example, I have a layout defined by

var force = d3.layout.force()
.nodes(documents.nodes)
.linkDistance(0)
.friction(.2)
.size([width, height]);

force.start();

and I wish to resize it later in the project. Is this possible?

like image 847
jcmitt Avatar asked Jan 23 '26 13:01

jcmitt


1 Answers

Shortly, on a resize event you should change the attributes of your svg object (it changes also the center of gravity) and resume force calculations:

window.onresize = function() {
  width = window.innerWidth, height = window.innerHeight;
  svg.attr('width', width).attr('height', height);
  force.size([width, height]).resume();
};

An example for the d3 force resize is provided here.

like image 144
Dmitry Avatar answered Jan 26 '26 02:01

Dmitry