问题
I'm trying to understand the code from
http://bl.ocks.org/d3noob/e34791a32a54e015f57d
I don't get the part where the code trying to scale the data below :
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y0.domain([0, d3.max(data, function(d) {
return Math.max(d.close); })]);
y1.domain([0, d3.max(data, function(d) {
return Math.max(d.open); })]);
Why would we have to use d3.max and also need Math.max within the return statement ?
shouldn't d3.max(data, function(d) { return d.close ;} )
be enough to get maximum data ? why would we need another Math.max again ?
回答1:
Following are the differences between d3.max and Math.max -
Unlike Math.max() in d3.max() elements are compared using natural order rather than numeric order i.e. the maximum of ["120", "3"] is "3", while the maximum of [120, 3] is 120.
d3.max() ignores the undefined values passed in the array (which is useful for computing the domain of a scale while only considering the defined region of the data).
If the array from which the Max value is required is empty, d3.max() returns undefined where as Math.max() returns -Infinity.
Hope these points help you in understanding the difference between d3.max() and Math.max()..
来源:https://stackoverflow.com/questions/28592557/what-is-the-different-between-d3-max-vs-math-max-in-d3-js