简体   繁体   中英

D3.csv not loading the csv file

For some reason, I am not able to load any .csv file with the d3.csv(...) function. What I do is that I load a csv file using the function above and print out the data directly to the console.

This is what what I do:

  1. I create a file called irisData.csv using notepad. It includes some data from the iris dataset from https://archive.ics.uci.edu/ml/datasets/Iris

    sepal_length,sepal_width,petal_length,petal_width,species 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1.5,0.2,Iris-setosa 5,3.6,1.4,0.2,Iris-setosa 5.4,3.9,1.7,0.4,Iris-setosa
  2. I write this code in my irisTest.html , to print out the data into the console, checking if it works correctly.

     ... d3.csv("irisData.csv", type, function(error, data){ console.log(data); }); ...
  3. Not sure if this is relevant, but I will put it up anyway: In order to run my html, I use Node.js to run a server. This is the code for the server, server.html :

     var http = require('http'); fs = require('fs'); http.createServer(function(req, res){ fs.readFile('./irisTest.html', function(err, data){ if(err){ res.writeHead(500, {'Content-type': 'text/plain'}); res.end('500 - Internal Error'); }else{ res.writeHead(200, {'Content-type': 'text/html'}); res.end(data); } }); }).listen(3000);

So what I would expect is that the console prints out an object consisting of the data from the csv file. Something like this:

[
  {data...}
  {data...}
  {data...}
  ...
]

However what I get is the code of my irisTest.html (which is the html code itself) wrapped into objects. I realize that it doesn't matter what I put instead of "irisData.cvs" as the path in d3.csv("irisData.csv", ...) , it always outputs my own code such as below. So I thought it might be a problem with the path to the csv file, but there shouldn't be. All files are in the same folder.

 [    
    ...
    {<!DOCTYPE html>: "d3.csv("irisData.csv", type, function(error, data){"}
    {<!DOCTYPE html>: "console.log(data);"}
    {<!DOCTYPE html>: "});}"}
    ...
 ]

Does anyone know what is going on?

As specified in the documentation here , the anonymous function is expected instead of type . I quote the example from the doc:

d3.csv("example.csv", function(d) {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
}, function(error, rows) {
  console.log(rows);
});

So, in your case, reading the csv file should be done this way:

d3.csv("irisData.csv",function(data){
    console.log(data);
},function(error, rows){
   console.log(rows); 
});

Here is a working example in gist , check the console to see the data object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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