Comments (6)
Maybe I’m missing something, but why not:
d3.extent(d3.merge(arrays))
Or, in modern browsers:
d3.extent(data.flat())
It works for Curran’s example, too:
d3.extent(d3.merge(d3.merge(stacked)))
Or better:
d3.extent(stacked.flat(2))
Especially now that d3-array 2.0 takes iterables (so you could write your own flattener if desired), I don’t feel there’s a need to exposed additional auto-flattening variants of d3-array methods.
from d3-array.
Here's a snippet I wrote to compute the extent of extents for one specific case:
https://bl.ocks.org/curran/f5282f177a5d339bc9d3e59745879fd7
yScale
.domain([
d3.min(stacked, function (series) {
return d3.min(series, function (d) { return d[0]; });
}),
d3.max(stacked, function (series) {
return d3.max(series, function (d) { return d[1]; });
})
])
from d3-array.
The expectation is that you call d3.min and d3.max separately if you have nested arrays, such as:
[
d3.min(arrays, array => d3.min(array)),
d3.max(arrays, array => d3.max(array))
]
We could make this a little more concise if d3.extent optionally allowed two accessors instead of just one. Then you could say:
d3.extent(arrays, array => d3.min(array), array => d3.max(array))
from d3-array.
That would be cool!
Could even simplify to:
d3.extent(arrays, d3.min, d3.max)
from d3-array.
@curran Unfortunately, no, because d3.extent passes the accessor additional arguments (the index and array), and d3.min and d3.max interpret the additional arguments as an accessor. It’s equivalent to:
d3.extent(arrays, (array, i) => d3.min(array, i), (array, i) => d3.max(array, i))
Which results in
TypeError: valueof is not a function
Line 22 in 544033c
We could change all the functions with optional accessors to ignore accessors that aren’t functions, but that’s always felt dirty to me; I prefer to limit our overloading to checking whether an argument is null.
We could support currying instead of optional arguments. So instead of:
d3.min(array, d => d.value)
You’d by the value accessor to d3.min using d3.min.by:
d3.min.by(d => d.value)(array)
If you just had an array of values, it’d be the same as before:
d3.min(values)
Now if you want to compose with d3.extent to compute the extent of an array of arrays of values, you can say:
d3.extent.by(d3.min, d3.max)(arrays)
But this change wouldn’t be backwards-compatible.
from d3-array.
Aha! An interesting gotcha.
Now it's interesting to compare these two alternatives:
d3.extent(arrays, array => d3.min(array), array => d3.max(array))
d3.extent.by(d3.min, d3.max)(arrays)
The latter is more concise but feels more heavy in terms of API surface area.
from d3-array.
Related Issues (20)
- BUG: d3-array/dist/d3-array.js: Unexpected token (139:15) HOT 4
- fix(babel): cumsum HOT 1
- binary ticks increments on linear scale HOT 2
- D3-array produces ERR_REQUIRE_ESM with node >= 15 HOT 3
- bisectCenter naming HOT 1
- quantile returns undefined on an empty array, differs from extent HOT 1
- Docs: define the bin thresholds with array HOT 2
- First and last thresholds are set to data extent (not explicitly stated limits) HOT 2
- bisector no longer supports two-argument (object, value) comparator HOT 12
- Testing a lib using `d3-array` HOT 1
- d3.blur HOT 1
- Incorrect results for binary search on large arrays due to miscomputation of midpoint HOT 11
- d3.bin can mutate the user-specified thresholds
- About the sorting problem of d3.rank HOT 2
- Insecure Randomness for the useof Math.random() in shuffle API (security vulnerability) HOT 1
- d3.thresholdScott returns NaN for single-element arrays
- Feature request: `find` / `findValue` methods
- groupSort should use ascendingDefined instead of ascending
- medianIndex/quantileIndex doesn’t handle missing data HOT 3
- can d3-array also support BigInt numbers? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-array.