Skip to content

cap the default number of bins at 200 #421

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

Merged
merged 6 commits into from
Aug 2, 2021
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ Plot.binX({y: "count"}, {x: {thresholds: 20, value: "culmen_length_mm"}})

The **thresholds** option may be specified as a named method or a variety of other ways:

* *auto* (default) - Scott’s rule, capped at 200
* *freedman-diaconis* - the [Freedman–Diaconis rule](https://en.wikipedia.org/wiki/Freedman–Diaconis_rule)
* *scott* - [Scott’s normal reference rule](https://en.wikipedia.org/wiki/Histogram#Scott.27s_normal_reference_rule)
* *sturges* - [Sturges’ formula](https://en.wikipedia.org/wiki/Histogram#Sturges.27_formula)
Expand All @@ -1047,7 +1048,7 @@ The **thresholds** option may be specified as a named method or a variety of oth
* a time interval (for temporal binning)
* a function that returns an array, count, or time interval

If the **thresholds** option is not specified, it defaults to *scott*. If a function, it is passed three arguments: the array of input values, the domain minimum, and the domain maximum. If a number, [d3.ticks](https://github.com/d3/d3-array/blob/master/README.md#ticks) or [d3.utcTicks](https://github.com/d3/d3-time/blob/master/README.md#ticks) is used to choose suitable nice thresholds.
If the **thresholds** option is specified as a function, it is passed three arguments: the array of input values, the domain minimum, and the domain maximum. If a number, [d3.ticks](https://github.com/d3/d3-array/blob/master/README.md#ticks) or [d3.utcTicks](https://github.com/d3/d3-time/blob/master/README.md#ticks) is used to choose suitable nice thresholds.

The bin transform supports grouping in addition to binning: you can subdivide bins by up to two additional ordinal or categorical dimensions (not including faceting). If any of **z**, **fill**, or **stroke** is a channel, the first of these channels will be used to subdivide bins. Similarly, Plot.binX will group on **y** if **y** is not an output channel, and Plot.binY will group on **x** if **x** is not an output channel. For example, for a stacked histogram:

Expand Down
7 changes: 6 additions & 1 deletion src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,23 @@ function maybeBin(options) {
return bin;
}

function maybeThresholds(thresholds = thresholdScott) {
function maybeThresholds(thresholds = thresholdAuto) {
if (typeof thresholds === "string") {
switch (thresholds.toLowerCase()) {
case "freedman-diaconis": return thresholdFreedmanDiaconis;
case "scott": return thresholdScott;
case "sturges": return thresholdSturges;
case "auto": return thresholdAuto;
}
throw new Error("invalid thresholds");
}
return thresholds; // pass array, count, or function to bin.thresholds
}

function thresholdAuto(values, min, max) {
return Math.min(200, thresholdScott(values, min, max));
}

function isTimeInterval(t) {
return t ? typeof t.range === "function" : false;
}
Expand Down