Skip to content

Commit 75f2ab2

Browse files
Filmbostock
andauthored
cap the default number of bins at 200 (#421)
* cap the default number of bins at 200 (see #417) * no closure; explicit args * default argument * explicit "auto" thresholds * rename to thresholdAuto * update README Co-authored-by: Mike Bostock <[email protected]>
1 parent 2f326d2 commit 75f2ab2

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ Plot.binX({y: "count"}, {x: {thresholds: 20, value: "culmen_length_mm"}})
10391039

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

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

1050-
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.
1051+
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.
10511052

10521053
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:
10531054

src/transforms/bin.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,23 @@ function maybeBin(options) {
171171
return bin;
172172
}
173173

174-
function maybeThresholds(thresholds = thresholdScott) {
174+
function maybeThresholds(thresholds = thresholdAuto) {
175175
if (typeof thresholds === "string") {
176176
switch (thresholds.toLowerCase()) {
177177
case "freedman-diaconis": return thresholdFreedmanDiaconis;
178178
case "scott": return thresholdScott;
179179
case "sturges": return thresholdSturges;
180+
case "auto": return thresholdAuto;
180181
}
181182
throw new Error("invalid thresholds");
182183
}
183184
return thresholds; // pass array, count, or function to bin.thresholds
184185
}
185186

187+
function thresholdAuto(values, min, max) {
188+
return Math.min(200, thresholdScott(values, min, max));
189+
}
190+
186191
function isTimeInterval(t) {
187192
return t ? typeof t.range === "function" : false;
188193
}

0 commit comments

Comments
 (0)