Since we load data dynamically and do not always know what the min and max values of the data are, there are functions for this in d3: If we have an array, we can simply call the max-function like this:
let data = [1, 2, 3, 4, 5];
let min = d3.min(data); // output: 1
If your data consists of more complex objects, you can pass any of these methods an anonymous accessor function as second parameter, like we do it here with the weight of the fruits:
let maxWeight = d3.max(data, function(d){
return d.weight;
});
If you look at the code, it works exactly as we aspected:
You can also use the extent()-function in the same way as min/max, the return value will be an array instead: [min, max].
And there are a lot more statistical functions in d3, like mean, median, quantile, sum and more!