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?
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
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