Skip to main content
100%

Dynamic Size Example

✓ Published0🌍 Public
CCurran Kelleher
Last edited Oct 12, 2017
Created on Aug 12, 2015

This example shows how a D3 v4 visualization can dynamically fill the browser window and automatically redraw when the viewport resizes. It uses a fixed-position CSS container to define the available space, then reads `clientWidth` and `clientHeight` to set the SVG dimensions on each resize. The code draws two diagonal lines forming an X, with stroke opacity and width, to visually confirm the resizing works. A `window.addEventListener("resize", redraw)` triggers the redraw, while the initial call to `redraw()` handles the first render. The example uses plain SVG with D3’s selection and merge methods to update line attributes.

AI-generated description

Check out my video course: D3.js in Motion. Learn D3 fast!

This example shows how to make a D3 visualization that fills up the size of the page at the time it is loaded AND whenever the page is resized by the user. This is necessary when, for example, you want to be able to load your visualization "full screen" and give users the ability to size the window however they want to. This example was created to address this question on the D3 Google Group: Any advice for setting width in iframes with D3?.

To experience the resize behavior, run this example full-screen and resize the browser.

<!-- Start of SimpleHitCounter Code --> <div align="center"><img src="http://simplehitcounter.com/hit.php?uid=2221531&f=16777215&b=0" border="0" height="18" width="83"></div> <!-- End of SimpleHitCounter Code -->
mit Licensed

Similar vizzes