Skip to main content
100%

Click-to-Recenter Brush

✓ Published0🌍 Public
Mmbostock
Last edited Aug 8, 2019
Created on Sep 9, 2013

A click-to-recenter brush demonstrates a modified brushing interaction for scatterplot data. By default, dragging outside an existing brush selection creates a new selection; here, it instead repositions the brush so it remains centered on the pointer. The chart plots 800 random points with `d3.randomUniform` and `d3.randomNormal`, rendered as SVG circles with a linear x-scale. The customization uses `d3.brushX`, replacing the overlay’s interaction type with “selection” and adding a `mousedown` handler that calls `brush.move` to recenter before brushing.

AI-generated description

By default, clicking and dragging outside the current brush selection drags a new selection. This brush has been modified such that this interaction instead recenters the brush.

This customization is implemented by changing the type of the brush’s overlay element from overlay to selection so that clicking and dragging on the overlay element behaves identically to clicking and dragging on the brush selection. (The overlay element is the invisible rect element that receives pointer events when clicking outside the brush selection.) When the overlay is clicked and before brushing starts, the brush is recentered around the pointer, such that the brush remains centered around the pointer during brushing.

This technique can be composed with snapping on brushend if desired.

gpl-3.0 Licensed

Similar vizzes