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 1 commit
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
* **skip** - skip empty bins (default: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the name empty, where true means to include empty bins, defaulting to false?


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
12 changes: 7 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 && bx.skip && bb.length === 0) continue;
for (const [y1, y2, fy] of BY) {
const b = fy(bb);
if (b.length === 0) continue;
if (by && by.skip && b.length === 0) 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, skip = true} = {}, 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.skip = skip;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value.skip = skip;
value.skip = !!skip;

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, skip} = options;
const bin = data => {
const V = valueof(data, value);
const bin = binner().value(i => V[i]);
Expand All @@ -165,7 +166,8 @@ 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 (skip) bins = bins.filter(nonempty2);
return bins.map(binfilter);
};
bin.label = labelof(value);
return bin;
Expand Down