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

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

问题描述

我在 d3.js 中遇到了 scales 问题.当我在 Firefox 控制台中输入最小值和最大值时,我将最大值作为最小值,将第二个最大值作为最大值.这里有什么问题?

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?

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

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"

d3.max 应该返回我 11 并且 d3.min 应该返回我 5

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

推荐答案

您可能没有将数据从 string 转换为 number.以下应该给你正确的答案:

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

但是,您可能希望在读取数据后立即将数据的 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天全站免登陆