min, max, extent exhibit unexpected behavior for arrays of integers of differing numbers of digits
d-lawrence opened this issue · 1 comments
I am new to d3, so this may be user error, however I have tried to make a good faith effort to establish this a non-PEBKAC issue.
I have some non-sense data (examples-simple.tsv
):
x y
100 5
270 12
300 15
440 23
600 25
after loading the data to data
(console.log
verifies it all loaded correctly) when I call d3.extent(data, d => d['y'])
I get back [ "12", "5" ]
However, when I pad the 5 out to two digits, I get back the expected ["05", "25"]
same thing happens if you multiply the y-column by 10 and pad the 50 to three digits.
When I test d3.min
and d3.max
I get the same kinds of unexpected behavior.
I am using Firefox 88, MacOS 10.15.7 (in case this is specific to browser or operating system). I have tested this with both D3 6.7.0 and 7.0.0.
I would be grateful of either disconfirmation or replication.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minimal Working Example</title>
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<!--script src="https://cdn.jsdelivr.net/npm/d3@6.7.0"></script-->
<script>
function mwe() {
d3.tsv("examples-simple.tsv").then(
function (data) {
console.log(data);
console.log(d3.extent(data, d => d['y']));
console.log(d3.min(data, d => d['y']));
console.log(d3.max(data, d => d['y']));
}
);
}
</script>
</head>
<body onload="mwe()"></body>
</html>
From the README, emphasis added:
Unlike the built-in Math.min, this method ignores undefined, null and NaN values; this is useful for ignoring missing data. In addition, elements are compared using natural order rather than numeric order. For example, the minimum of the strings [“20”, “3”] is “20”, while the minimum of the numbers [20, 3] is 3.
By default d3.tsv will return strings for all fields which is probably not what you want. If your TSV file is appropriately formatted, you may be able to use d3.autoType to convert your strings to numbers. Otherwise you will need to write your own row conversion function to coerce the data to the desired type.