Skip to main content

Function: squarebin()

squarebin<T>(points, o): BinOf<T>[]

Defined in: packages/sdvplot/src/bins/squarebin.ts:69

Bin points into axis-aligned squares of side side, in the points' OWN units. As in d3-hexbin's lattice, a cell is centred on the origin, so centres are (i·side, j·side). Cells are half-open, [c − side/2, c + side/2) on each axis: a point exactly on an edge goes to the higher cell, because the cell index is Math.round(x / side), the rounding d3-hexbin uses (hexbin.js:33-34). Bins come out in first-seen order; points with a NaN coordinate are skipped; centres of -0 fold to 0. Throws InputError unless side is a finite number greater than 0.

Type Parameters​

T​

T

Parameters​

points​

readonly T[]

o​

side​

number

x​

(p) => number

y​

(p) => number

Returns​

BinOf<T>[]

Example​

import { squarebin } from "@sportsdataverse/sdvplot/bins";

// three Brooklyn shots of 2025-26 (sportsdataverse-data nba_stats_shots); x = 5 sits on a cell edge and goes to
// the higher cell: [[0, 0, 1], [0, 10, 1], [10, 90, 1]]
const shots = [
{ x_legacy: 0, y_legacy: 0 },
{ x_legacy: -1, y_legacy: 7 },
{ x_legacy: 5, y_legacy: 91 },
];
squarebin(shots, { side: 10, x: (s) => s.x_legacy, y: (s) => s.y_legacy }).map((b) => [b.x, b.y, b.length]);