you need to load the file through an asynchronous request with d3.csv. example code:

d3.csv("datafile.csv", function(error, data) {
  // do something exciting with data


your drawing code needs to stay inside csv callback:

d3.csv("testdata.csv", function(data){
    // this converts data to number
    data.foreach(function(d) {
        d.x = +d.x;
        d.y = +d.y;

    // rest of drawing code

see another example here:

it's easier to debug if you can post link to working code.

Related Query