Home > Net >  Why does d3.max() output second largest number?
Why does d3.max() output second largest number?

Time:07-08

I am creating a bar chart with d3.js. The datasets I used output wrong max value so I tested with the following sets again.

name,value
us,1000
china,800
uk,850
spain,700
italy,400
france,400
belgium,300

But when I run my script below, the output is 850, not 1000. What's happening?

csv(filepath).then(data => {
    let top = max(data, d => d.value);
    console.log(top)
    render(data)                                        // refer to formerly created function render()
});

CodePudding user response:

you have to be sure that d.value is a number and not a string, if not it will give you the alphabetic max between string '850' and '1000'

one way can be to parseInt your data to compared it as number value and get the correct max

csv(filepath).then(data => {
    let top = max(data, d => parseInt(d.value, 10));
    console.log(top)
    render(data)                                        // refer to formerly created function render()
});
  • Related