简体   繁体   English

d3.csv不加载数字矩阵

[英]d3.csv doesnt load numeric matrix

I want to load an external .csv file using d3.csv. 我想使用d3.csv加载外部.csv文件。

This is data.csv (created in vim): 这是data.csv(在vim中创建):

x,y
2,2
99,18

This is the d3 code: 这是d3代码:

d3.csv("data.csv", function(dataset){
console.log(d3.max(dataset, function(d){return d["y"];})); 
});

Even though I expected '18' the output in the console I get is '2'. 即使我期望“ 18”,控制台中的输出还是“ 2”。 If I change the '18' to another number, eg '35' the output is correct again. 如果将“ 18”更改为另一个数字,例如“ 35”,则输出将再次正确。 What am I doing wrong? 我究竟做错了什么?

These numbers are getting loaded as Strings, and that's causing unexpected behavior of d3.max. 这些数字将作为字符串加载,这导致d3.max出现意外行为。 You need to cast the values into Numbers. 您需要将值转换为数字。

You can turn them into numbers via Number(dy) or ( parseInt , parseFloat ). 您可以通过Number(dy)或( parseIntparseFloat )将它们转换为数字。 In d3 you often see it shortened as y: +dy . 在d3中,您经常看到它缩短为y: +dy

With d3.csv you can specify a function where you can do this conversion as it parses. 使用d3.csv您可以指定一个函数,在解析时可以在其中进行此转换。 See documentation . 请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM