如何从一个txt文件得到d3.js与TSV或CSV到阵列中的数据? [英] how to get data with tsv or csv to array in d3.js from a txt file?

查看:773
本文介绍了如何从一个txt文件得到d3.js与TSV或CSV到阵列中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这来解析csv文件,并创建一个数组数据作为D3文档指定:

I am using this to parse a csv file and create an array data as specified in d3 docs:

d3.tsv("classes_h.txt", function(data) { 
    data.forEach(function(d) { 
        console.log(data[0]);
        console.log("lol");
    });
    console.log(data[0]);
});

d3.tsv("classes_h.txt", function(data) { 
    console.log(data[0]);
});

不过,我在控制台中未定义当我打电话数据[0]。我也试着写出数据,而不是数据[0],这导致我​​得到一个空数组 - > []:

However I get undefined in the console when I call the data[0]. I've also tried writing out "data instead of "data[0]" which result in me getting an empty array -> []:

我的tx​​t文件看起来是这样的:
http://puu.sh/7LrRm.png

My txt file looks like this: http://puu.sh/7LrRm.png

一切都分开了标签,所以TSV是我使用的是什么,如果我理解正确的话,是否使用TSV或CSV取决于数据格式为?

Everything is seperated with a tab, so tsv is what I'm using and if I have understood it correctly, whether tsv or csv is used depends on the format the data is in?

先谢谢了。

推荐答案

解决

SOLVED

TSV不只是把标签分开元素把他们到一个数组。您的格式如下:

TSV doesn't just take tab seperated elements a throw them into an array. You the format to look like this:

Group1 Group2 Group3
data1  data1  data1
data2  data2  data2
data3  data3  data3

然后就可以打印出来,并验证数据加载:

Then you can print out and verify that the data is loaded:

data.forEach(function(d) { 
    console.log(d);
});

当打印出整个数据D3将创建一个对象,每个有一个组与所有它的数据的约束。下面是我的例子的输出:

When printing out the entire data D3 will create an objects that each have a Group bound with all of it's data. Here's the output of my example:

Object {Group1: "data1", Group2: "data1", Group3: "data1"}
Object {Group1: "data2", Group2: "data2", Group3: "data2"}
Object {Group1: "data3", Group2: "data3", Group3: "data3"}

这些对象是相当于数据[0],数据[1]及数据[2],这使组1,组2和组3

Those the objects are equivalent to data[0], data[1] and data[2], which leaves group1, group2 and group3.

这篇关于如何从一个txt文件得到d3.js与TSV或CSV到阵列中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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