简体   繁体   English

使用D3.JS时无法加载TSV

[英]Can't load TSV when using D3.JS

I need to visualise a TSV file with countries, dates and percentages in a slopegraph. 我需要使用斜率图中的国家,日期和百分比来可视化TSV文件。 Next to that I need to use a CSV file for another assignment, which frankly loads without any issues but I've been stick on this one for more then 1.5 day and I just can't figure it out. 紧接着,我需要使用CSV文件进行另一项任务,坦率地说,加载不会有任何问题,但是我坚持使用了1.5天以上,但我仍然无法解决。

I've made a basic code just to test whether I can display some of the text out of the TSV file. 我已经编写了一个基本代码,只是为了测试是否可以显示TSV文件中的某些文本。 Also when inspecting the elements in the chrome browser I don't see the SVG container. 另外,在Chrome浏览器中检查元素时,我也看不到SVG容器。 I really would appreciate some help on this subject!! 我真的很感谢在这个问题上的一些帮助!

    var container = d3.select("body").append("svg")
                .attr("width", 500)
                .attr("height", 500);

d3.tsv("18-year-olds-in-education.tsv", function(error, data) {
    data.forEach(function(d) {
        d.indic_ed,geo\time = d.indic_ed,geo\time;
        d.2001 = +d.2001
        d.2012 = +d.2012;
});


var text = container.selectAll("text")
                .data(data)
                .enter()
                .append("text");

var textLabels = text
     .attr("x", 200)
             .attr("y", 200)
             .text(function(d) { return d.2012)
             .attr("font-family", "sans-serif")
             .attr("font-size", "20px")
             .attr("fill", "red");

});

The problem are the field names you're using in the TSV. 问题是您在TSV中使用的字段名称。 In particular, the following code isn't valid Javascript: 特别是,以下代码不是有效的Javascript:

d.indic_ed,geo\time = d.indic_ed,geo\time;
d.2001 = +d.2001;
d.2012 = +d.2012;

You can use the alternative syntax for accessing fields though: 不过,您可以使用替代语法来访问字段:

d['2001'] = +d['2001'];
d['2012'] = +d['2012'];

I'm omitting the first statement here because it has no effect. 我在这里省略了第一条语句,因为它没有效果。

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

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