score:1

Accepted answer

Another approach might employ some more of D3's functionality:

function getExtent(list) {
    return d3.extent(                          // 4. Get the extent from that array
        d3.merge(                              // 3. Merge all arrays into a single one
            data.map(function(d) {             // 2. Map each object's values to an array
                return list.map(function(y) {  // 1. Get the array of values to map
                    return d[y]; 
                });
            })
        )
    );
}

console.log(getExtent(["Branch1"]));            // [1200, 1588]
console.log(getExtent(["Branch2"]));            // [2000, 3495]
console.log(getExtent(["Branch1", "Branch2"])); // [1200, 3495]

y.domain(getExtent(yAxisList));                 // This extent can be used directly

I am not making any claims on performance, but at least to the eye this seems to be more pleasing and elegant.


Related Query