liveBook · Manning

Bucket

As explained in D3.js in Action, Third Edition

A bin is an interval or group in a distribution that contains data points with values within specified boundaries.

A bucket, also called a bin, groups data points whose values fall within a range. Buckets turn individual values into a distribution: you can see how many values occur in each part of an axis. They are generally equal in width, not necessarily equal in the number of points they contain. section 6.1 section 6.6

Salary brackets as buckets

A survey response of $20,000–$39,999 identifies a salary range, not an exact salary. That bracket is a bucket: it tells you where the value belongs without revealing the value itself. To practice drawing charts, the salary project generates a random value inside each reported bracket. Those generated values are illustrative, not observed salaries. section 6.1

In D3, `d3.bin()` prepares buckets for drawing. Point its value accessor at the field you want to group, then pass in the data: `const bins = d3.bin().value(d => d.salary)(data);`. The result is an array of bins. Each bin contains its data points; `x0` and `x1` give its lower and upper boundaries, and `length` gives its point count. section 6.1

Figure 6.3 In the multidimensional array returned by the bin generator, each bin is an array containing data points. In this figure, the data points, consisting of JavaScript objects, are represented by {...} for conciseness. A bin’s lower and upper limits are accessible under the keys x0 and x1.
figure

Inspect the nested array to see the difference between a bin’s contained data points and its `x0` and `x1` boundaries. section 6.1

Once you have bins, connect each count to a visible mark:

  • For a histogram, bind bins to SVG rectangles. Position each rectangle using `x0`, set its width from `x1 - x0` through the horizontal scale, and set its height from `length` through the vertical scale. Taller rectangles indicate more values in that range. section 6.2
  • For violin plots, bin salaries separately for each category. Use each bin’s count to determine the plot’s width at that salary range, then draw a curved area through those widths. section 6.5
  • For an interactive histogram, filter the data and run it through the bin generator again. Bind the updated bins to the rectangles so their heights reflect the selected group. section 7.2.3

How D3.js in Action, Third Edition covers it

How deep it goes
Works through D3 bin generation, returned boundaries and contents, rectangle encoding, and recalculation after filtering.
Examples
Uses survey salary brackets, a salary histogram, role-based violin plots, and a gender-filtered histogram.
What it assumes
Introduces bins in plain terms while using JavaScript objects, D3 methods, scales, and SVG chart elements in the implementation.
What you can do after
Apply it via D3 to generate bins, draw distribution charts, and recalculate counts when the displayed data changes.
How it connects
Connects bins to histograms, violin plots, SVG data binding, scales, and interactive filtering.

Also explained in

Compare how 9 books explain bucket →

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.