Skip to content

Instantly share code, notes, and snippets.

@danharr
Created March 29, 2014 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharr/9856775 to your computer and use it in GitHub Desktop.
Save danharr/9856775 to your computer and use it in GitHub Desktop.
show data from a CSV

how to read data from a CSV and show it in the page.

name age
alan 10
bob 20
colin 30
dave 40
edward 50
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>show data from CSV</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<script type="text/javascript">
var dataset; //Declare global var
var svg = d3.select("body")
.append("svg")
.attr("width", 200)
.attr("height", 500);
d3.csv("data.csv", function(data) {
//Hand CSV data off to global variable so it's accessible later.
dataset = data;
//Call some other functions that
//generate your visualization, e.g.:
showdata();
});
function showdata() {
d3.select("body").selectAll("p")
.data(dataset)
.enter()
.append("p")
.text(function(d) {return d.name +", "+d.age;});
};
</script>
<body>
</body>
</html>
Copy link

ghost commented Mar 17, 2022

When I copy the code to my local and run the index.html. It displays a blank page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment