Use D3.min to find lowest value that is not 0

坚强是说给别人听的谎言 提交于 2020-01-03 14:21:48

问题


I'm trying to use D3 to find the lowest value in my dataset. However, I also have values that are 0, but I want D3 to find the lowest value that is not 0.

Currently I am using:

d3.min(data, function(d) {return d.houseValues; })

But obviously this returns 0 sometimes, when a 0 is found.

Is there a way to do this? Or is the only solution to build a normal for-loop with an if-statement to ignore the 0 values..?

Thanks!


回答1:


You can use the constant Infinity, since Math.min(Infinity, someNumber) always return someNumber (unless someNumber is also infinity). So it'll look like this:

smallest = d3.min(data, function(d) {return d.houseValues || Infinity; })

If needed, you can check smallest == Infinity, which would be true in the case that all house values were 0.




回答2:


Try filtering the data to remove zeroes first, e.g.

var noZeroes = data.filter(function(d) { return d.houseValues !== 0; });
d3.min(noZeroes, function(d) {return d.houseValues; })


来源:https://stackoverflow.com/questions/29261994/use-d3-min-to-find-lowest-value-that-is-not-0

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