Function: hexbin()
hexbin<
T>(points,o):BinOf<T>[]
Defined in: packages/sdvplot/src/bins/hexbin.ts:37
Bin points into pointy-top hexagons of circumradius radius, in the points' OWN units (data space, not
pixels). Points with a NaN coordinate are skipped. Centres of -0 are folded to 0 (blazing-the-nets
main lib/data/aggregate.ts:101), so equal hexes share one key. Throws InputError unless radius is a finite
number greater than 0.
Each point goes to the centre upstream d3-hexbin picks, which is close to but not exactly the nearest one: it
compares two candidates' squared distances in lattice units (x over √3·radius, y over 1.5·radius; :43), so
about 1-2% of points sit just outside the hexagon drawn for their bin (on the real BKN fixture, 22 of 2000 shots
at radius 10 and 37 of 2000 at radius 15). That is faithful to d3-hexbin and to blazing-the-nets, which draw the
same; do not "fix" it. Squares (squarebin) contain every point they bin.
Type Parameters
T
T
Parameters
points
readonly T[]
o
radius
number
x
(p) => number
y
(p) => number
Returns
BinOf<T>[]
Example
import { hexbin } from "@sportsdataverse/sdvplot/bins";
// three Brooklyn shots of 2025-26 in tenths of a foot from the hoop (sportsdataverse-data nba_stats_shots): the
// two at the rim share a 1 ft hexagon, so this is 2
const shots = [
{ x_legacy: 0, y_legacy: 0 },
{ x_legacy: -1, y_legacy: 7 },
{ x_legacy: -44, y_legacy: 252 },
];
hexbin(shots, { radius: 10, x: (s) => s.x_legacy, y: (s) => s.y_legacy }).length;