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
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; });
// ...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With