我正在尝试从中了解代码
http://bl.ocks.org/d3noob/e34791a32a54e015f57d
我没有得到尝试缩放以下数据的部分:
// 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); })]);
Run Code Online (Sandbox Code Playgroud)
为什么我们必须在返回语句中使用d3.max并还需要Math.max?
不应该d3.max(data, function(d) { return d.close ;} )
是足够的,以获得最大的数据?为什么我们又需要另一个Math.max?
小智 5
以下是d3.max和Math.max之间的区别-
与d3.max()中的Math.max()不同,使用自然顺序而不是数字顺序来比较元素,即[[120],“ 3”]的最大值为“ 3”,而[120,3]的最大值为120。
d3.max()会忽略在数组中传递的未定义值(这在仅考虑数据的定义区域时,对于计算比例域非常有用)。
如果需要最大值的数组为空,则d3.max()返回undefined,其中Math.max()返回-Infinity。
希望这些要点有助于您理解d3.max()和Math.max()之间的区别。