Skip to main content
Public

Share visualization

Loading preview…

Responding to Resize

vizhub.com

Shared links may look like this on X, LinkedIn, Slack, and Discord.

Before sharing, review the publishing checklist.

Choose how to deliver this visualization

LinkedIn caption

LinkedIn may not keep text supplied by the share sheet. Copy the caption and paste it into the post if needed. You can tag the VizHub company page when posting.

More options

Embed on a websiteCopy iframe embed code
Download & exportPNG, HTML, source ZIP

Responding to Resize

1
Ggonghewan
Last edited Jan 17, 2020
Created on Jan 17, 2020

This example demonstrates how a visualization can respond to browser window resizing by redrawing an SVG to match its container’s updated dimensions. It shows a green X spanning the full width and height of the viewport, with thick, semi-transparent strokes. The code uses `d3.select` to create an SVG element and a `redraw()` function that reads `clientWidth` and `clientHeight` from the chart’s `<div>`, then sets the SVG’s `width` and `height` attributes accordingly. A `window.addEventListener("resize", redraw)` triggers the redraw on every resize event, while `selection.enter().merge()` updates the two `<line>` elements’ coordinates.

AI-generated description

Comments

Sign in to join the discussion.

MIT Licensed