What is the different between d3.max() vs. math.max() in d3.js?

家住魔仙堡 提交于 2020-01-13 17:05:53

问题


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 -

  1. 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.

  2. 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).

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!