Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.max didn't get the correct value [duplicate]

I am using d3.js to render some data and the data set is a csv file called bar-data.csv like this:

date,value,duration
www.sina.com,53,100
www.baidu.com,165,2000
www.qq.com,269,690
www.youku.com,421,224
www.facebook.com,405,345
www.apple.com,376,777
www.cnn.com,359,298
www.cctv.com,433,333
www.google.com,455,500
www.boston.com,670,274

However when I using following codes to get the maximum of duration, the return value is 777 not 2000 (which should be the maximum among the data set).

d3.csv("bar-data.csv", function(error, data) {

data.forEach(function(d) {
    d.date = d.date;
    d.value = d.value;
});

var maximum = d3.min(data, function(d) { return d.duration; });
alert ("max is " + maximum);

And if you change the 2000 to 999, you will get the 999 as the maximum value. It seems that it only give out the answer with the same amount of digits.

What's the issue here?

like image 662
chrisTina Avatar asked Jun 16 '15 17:06

chrisTina


1 Answers

Changing the code to parse the duration values to Int fixes the problem together with processing them with d3.forEach function. See below:

d3.csv("test.csv", function(error, data) {
        console.log(data);
        data.forEach(function(d) {
            d.date = d.date;
            d.value = d.value;
            d.duration = parseInt(d.duration);
        });

        var maximum = d3.max(data, function(d) { return d.duration; });
        console.log("max is " + maximum);
    });

Thanks for the question I bet plenty of people have had this before!

Bye, Alex

like image 149
Alex_B Avatar answered Oct 07 '22 03:10

Alex_B