D3.js从数据集中返回错误的值 [英] D3.js scale returning wrong values from dataset

查看:77
本文介绍了D3.js从数据集中返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在d3.js中遇到 scales 的问题。当我在Firefox控制台中键入min和max时,我得到的最大值为min,第二个最大值为max。这里有什么问题?



这是我的csv文件形式的数据集:

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



是我在控制台中输入的值,并返回值:

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

d3.max应该返回我11和d3.min应该返回我5

解决方案

您可能没有将您的数据从字符串 code> number 。以下应该给你正确的答案:

  d3.min(dataset,function(d){return + d.occur} ); 
d3.max(dataset,function(d){return + d.occur});但是,你可能想要转换出现 列的数据,直到读取它:

  d3.csv('myfile',function(err,dataset) {
dataset.forEach(function(d){d.occur = + d.occur;});
// ...
});


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; });
    // ...
});

这篇关于D3.js从数据集中返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆