liveBook · Manning

Zoom

As explained in D3.js in Action, Third Edition

Zooming is an interaction technique that changes the magnification or visible extent of a visualization or displayed content.

Zooming changes a map’s scale and visible position so users can focus on a region and inspect more detail. Panning moves the view across the map. section 13.3

A D3 zoom implementation proceeds through a few practical steps:

  • Create a zoom behavior with d3.zoom() and listen for the zoom event. section 13.3
  • Attach the behavior to the map container with selection.call(). section 13.3
  • Apply the event’s transform to the SVG; k represents scale, while x and y represent translation. section 13.3
  • Hide overflow and constrain scale and translation so the map remains inside its container. section 13.3

Interactive world map

A world map responds to mouse-wheel or trackpad gestures, applies the zoom transform to its SVG, limits scale from 1 to 5, and can expose a reset control after the view changes. section 13.3

Listing 13.12 Implementing zooming and panning (map-world.js)
...
import { zoom } from "d3-zoom";

export const drawWorldMap = (laureates, world) => {

  ...

  const zoomHandler = zoom()   #1
    .on("zoom", (e) => {                    #2
      console.log(e);                      
    });                                    

  select(".map-container")   #3
    .call(zoomHandler);     

};

Inspect the minimal D3 pattern for creating a zoom handler, logging its event, and attaching it to the map container. section 13.3

How D3.js in Action, Third Edition covers it

How deep it goes
Explains event handling, SVG transforms, overflow, scale limits, translation limits, and reset behavior.
Examples
Uses an interactive world map with mouse-wheel or trackpad gestures, bounded transforms, clipped overflow, and a reset button.
What it assumes
Assumes a displayed SVG map and basic D3 selection usage while introducing zoom behavior through working code.
What you can do after
Build it from scratch with d3-zoom, attach gestures, transform an SVG map, constrain movement, and provide reset behavior.
How it connects
Connects zooming with panning, maps, SVG, GeoJSON, TopoJSON, projections, and brushing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.