Skip to main content
100%

Polybrush Example

✓ Published0🌍 Public
GGerHobbelt
Last edited Sep 7, 2012
Created on Sep 16, 2012

This example presents a custom D3 plugin, `d3.svg.polybrush`, which enables interactive polygon selection on a scatterplot. The visualization uses a Voronoi tessellation of 200 random points, with each cell clipped to a circular region around its vertex. Users click to place anchor points and double-click to close the selection, then drag to reposition the polygon. The `brush` event handler uses the plugin’s `isWithinExtent` method to highlight both the selected points and their corresponding Voronoi cells in red. Built with d3.v2, the code demonstrates custom brush behavior beyond the standard `d3.svg.brush`.

AI-generated description

Here's a d3 plugin that allows you to create a polygon selection. You instantiate it just like d3.svg.brush.

var brush = d3.svg.polybrush();

It has an extra public method that 'brush' does not, and that's 'isWithinExtent(x, y)'. You can use this method to test if a given point falls within the drawn extent.

if (brush.isWithinExtent(x, y)) {
  console.log("I'm inside!");
}

Usage:<br/> Click to add an anchor point, double click to close the selection.<br/> Drag the selection to reposition it.<br/> Double click outside the selection to clear it.<br/>

Areas For Improvement:<br/>

  1. Add anchor handles to allow the repositioning of an anchor point.<br/>
  2. Better viewport edge detection. Right now it simply stops moving if you try to move the selection out of the viewport.

Similar vizzes