简体   繁体   English

如何将 csv 文件加载到变量中以与 d3.js 一起使用

[英]How to load csv file into variable for usage with d3.js

I need to write all values from a specific column in a .csv file to an array.我需要将.csv文件中特定列的所有值写入数组。 Please execute the following working example and notice, that all the values from the column Address are successfully written into the array variable tmpArr请执行以下工作示例并注意,列Address中的所有值都已成功写入数组变量tmpArr

 var csvString= "Address,longitude,latitude,geometry\\n"+ "1,2,3,4\\n5,6,7,8\\n2,3,4,5\\n6,7,8,9"; var t = d3.csv.parse(csvString); var tmpArr = t.map(function(d){return d.Address;}); console.log(tmpArr);
 <script src="https://d3js.org/d3.v3.min.js"></script>

But how can I load a .csv file into the variable csvString instead of a hardcoded string?但是如何将.csv文件加载到变量csvString而不是硬编码字符串中?

I successfully load the .csv file via ajax, but I only get the output ,,,我通过ajax成功加载了.csv文件,但我只得到了输出,,,

csvToChart("daten/kundenimporte/", "test.csv");

function csvToChart(basepath, filename)
{
    $.ajax({
        url: basepath + filename,
        method: "GET",
        success: myCallback
    });
}

function myCallback(response)
{
    var t = d3.csv.parse(response);
    d3.select("#output").html(
        t.map(function(d){
            return d.Kundengruppe;
        })
    );
}

I get ReferenceError: d is not defined .我得到ReferenceError: d is not defined

How can I successfully load my .csv file?如何成功加载我的.csv文件?

There is no need to mix in jQuery as D3 provides methods for conveniently loading and parsing CSV files.无需混入 jQuery,因为 D3 提供了方便加载解析 CSV 文件的方法。

In the old versions 3 or 4 of D3 you could use d3.csv() which was part of the now deprecated d3-request module.在 D3 的旧版本 3 或 4 中,您可以使用d3.csv() ,它是现已弃用的d3-request模块的一部分。 This will employ an Ajax request for loading the file's content:这将使用 Ajax 请求来加载文件的内容:

d3.csv("daten/kundenimporte/test.csv", function(data) {
  d3.select("#output").html(
    data.map(function(d){
      return d.Address;
    })
  );
});

If you decide on upgrading to v5, however, things have slightly changed , since d3.csv() despite having the same name is now part of the d3-fetch module and will return a Promise instead of executing a callback.但是,如果您决定升级到 v5,事情会发生一些变化,因为尽管具有相同名称的d3.csv()现在是d3-fetch模块的一部分,并且将返回一个 Promise 而不是执行回调。 Your code then becomes:然后您的代码变为:

d3.csv("daten/kundenimporte/test.csv")
  .then(function(data) {   // Handle the resolved Promise
    d3.select("#output").html(
      data.map(function(d){
        return d.Address;
      })
    );
  });

If you dont mind using a jQuery library, here might be an answer for you problem: How to read data From *.CSV file using javascript?如果您不介意使用 jQuery 库,这里可能是您问题的答案: How to read data From *.CSV file using javascript?

Otherwise a person added his own "quick and dirty" function above ^that answer.否则,有人在 ^ 那个答案上方添加了他自己的“快速而肮脏”的功能。

Hope that helps.希望有帮助。

/Good luck and happy coding /祝你好运,编码愉快

You should now migrate to d3 V4/V5.您现在应该迁移到 d3 V4/V5。 In V4, it goes like below.在 V4 中,它如下所示。

d3.csv("your_path_to_csv file", function(data) {
//Your code goes here

//Now you can use 'data' variable as an array of objects
console.log
});

In V5, It goes like在 V5 中,它就像

d3.csv("your_path_to_csv file").then(function(data) {
//Your code
});

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

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