Friday, 12 June 2015

Read CSV file from Local File system

 Read CSV file from Local File system

 
You need a relative path.  The easiest way is to have the .csv
file in the same directory/folder as the main file and refer to
it simply by name.  Or you could have it in a sub-directory
named “sample” and refer to it as “sample/test.csv”



<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<h1 id="output">
</h1>
<script>
/*
THIS FILE: “index.html”
CONTENTS OF file “test.csv” saved in the same directory as this:
Name,Surname,Age
Joe,Smith,42
Alice,Jones,29
*/
var rows, f = "test.csv"
var parts = d3.csv(f, function(d) {
   return {
     Name: d.Name, Surname: d.Surname, Age: d.Age,
   };
}, function(error, rows) {
   d3.select("#output")
     .text(
       rows[0].Name + " " +
       rows[0].Surname + " " +
       "is " + rows[0].Age + " years old")
});
</script>
</html>

No comments:

Post a Comment