Skip to content

An option to include empty bins #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ To control how the quantitative dimensions *x* and *y* are divided into bins, th
* **thresholds** - the threshold values; see below
* **domain** - values outside the domain will be omitted
* **cumulative** - if positive, each bin will contain all lesser bins
* **empty** - whether to include empty bins; false by default

If the **domain** option is not specified, it defaults to the minimum and maximum of the corresponding dimension (*x* or *y*), possibly niced to match the threshold interval to ensure that the first and last bin have the same width as other bins. If **cumulative** is negative (-1 by convention), each bin will contain all *greater* bins rather than all *lesser* bins, representing the [complementary cumulative distribution](https://en.wikipedia.org/wiki/Cumulative_distribution_function#Complementary_cumulative_distribution_function_.28tail_distribution.29).

Expand Down
13 changes: 8 additions & 5 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ function binn(
for (const [k, g] of maybeGroup(I, K)) {
for (const [x1, x2, fx] of BX) {
const bb = fx(g);
if (bb.length === 0) continue;
if (bx && bb.length === 0 && !bx.empty) continue;
for (const [y1, y2, fy] of BY) {
const b = fy(bb);
if (b.length === 0) continue;
if (by && b.length === 0 && !by.empty) continue;
groupFacet.push(i++);
groupData.push(reduceData.reduce(b, data));
if (K) GK.push(k);
Expand All @@ -124,12 +124,13 @@ function binn(
};
}

function maybeBinValue(value, {cumulative, domain, thresholds} = {}, defaultValue) {
function maybeBinValue(value, {cumulative, domain, thresholds, empty} = {}, defaultValue) {
value = {...maybeValue(value)};
if (value.domain === undefined) value.domain = domain;
if (value.cumulative === undefined) value.cumulative = cumulative;
if (value.thresholds === undefined) value.thresholds = thresholds;
if (value.value === undefined) value.value = defaultValue;
value.empty = !!empty; // Note: cannot be set per-dimension
value.thresholds = maybeThresholds(value.thresholds);
return value;
}
Expand All @@ -144,7 +145,7 @@ function maybeBinValueTuple(options = {}) {

function maybeBin(options) {
if (options == null) return;
const {value, cumulative, domain = extent, thresholds} = options;
const {value, cumulative, domain = extent, thresholds, empty} = options;
const bin = data => {
const V = valueof(data, value);
const bin = binner().value(i => V[i]);
Expand All @@ -165,8 +166,10 @@ function maybeBin(options) {
}
let bins = bin(range(data)).map(binset);
if (cumulative) bins = (cumulative < 0 ? bins.reverse() : bins).map(bincumset);
return bins.filter(nonempty2).map(binfilter);
if (!empty) bins = bins.filter(nonempty2);
return bins.map(binfilter);
};
bin.empty = empty;
bin.label = labelof(value);
return bin;
}
Expand Down
Loading