Git Product home page Git Product logo

Comments (6)

mbostock avatar mbostock commented on May 3, 2024 4

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.

curran avatar curran commented on May 3, 2024

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.

mbostock avatar mbostock commented on May 3, 2024

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.

curran avatar curran commented on May 3, 2024

That would be cool!

Could even simplify to:

d3.extent(arrays, d3.min, d3.max)

from d3-array.

mbostock avatar mbostock commented on May 3, 2024

@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

if ((value = valueof(values[i], i, values)) != null && value >= value) {

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.

curran avatar curran commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.