Using Pym.js for responsive d3
This example demonstrates how Pym.js enables responsive SVG graphics by redrawing d3 content based on iframe width. The embedded page uses a `pym.Child` with a `renderCallback` to trigger `createChart`, which swaps between a landscape scene with trees and a portrait figure at a 600-pixel breakpoint. The code applies d3.v3 selections, `append`, `attr`, and `use` elements with `xlink:href` references to symbol definitions. It also uses Pym.js’s parent–child messaging to update the graphic on window resize, as shown in the parent `index.html` and child `svgimage.html` files.
AI-generated descriptionPym.js
Pym.js by NPR is a really useful tool for deploying responsive SVG graphics.
Pym.js works by creating dynamic iframes on a (parent) page which can transmit window-size changes to an embedded (child) page. The child can also send changes back to the parent if interaction causes the embedded item to change in height. Crucially, the child page can be set to redraw its content when a window is resized, allowing different content to be generated depending on the size of the iframe.
This playful (and artistically inept) example illustrates the concept by using d3 to draw different images (a 'landscape' landscape and a 'portrait' portrait) depending on the window width. To see it properly using bl.ocks.org, you'll need to launch into the new window view - then just resize the window by dragging the window in and out (the switch happens at 600px).
You can read more about using pym.js on the NPR Blog
You can see data visualisation examples using this approach on Visual.ONS