Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js scale returning wrong values from dataset

I'm having a problem with scales in d3.js. As I type the min and max into the Firefox console, I get the max value as the min and the second-max value as the max. What's the problem here?

Here is my dataset in form of csv file:

word,occur
obama,11
theguardian,9
world, 8
state, 8
care, 7
pakistan,7
block, 6
blog, 6
healthcare, 5

here is what I type into the console and the values I get in return:

    d3.min(dataset, function(d) { return d.occur}); => "11"
    d3.max(dataset, function(d) { return d.occur}); = "9"

the d3.max should return me 11 and d3.min should return me 5

like image 776
Taimoor Alam Avatar asked Dec 15 '13 08:12

Taimoor Alam


1 Answers

You are probably not converting your data from a string to a number. The following should give you the correct answer:

d3.min(dataset, function(d) { return +d.occur}); 
d3.max(dataset, function(d) { return +d.occur});

However, you probably want to convert the occur column of your data to numeric right after reading it:

d3.csv('myfile', function (err, dataset) {     
    dataset.forEach(function (d) { d.occur = +d.occur; });
    // ...
});
like image 134
musically_ut Avatar answered Sep 19 '22 20:09

musically_ut