Skip to content
mbostock edited this page Jul 1, 2011 · 17 revisions

API Reference

# d3.csv(url, function)

Issues an HTTP GET request for the comma-separated values (CSV) file at the specified url. The mime type of the request will be "text/csv". The request is processed asynchronously, such that this method returns immediately after opening the request. When the CSV data is available, the specified callback function will be invoked, being passed the parsed rows. If an error occurs, the callback function will instead of invoked with null.

# d3.csv.parse(string)

Parses the specified string, which is the contents of a CSV file, returning an array of objects representing the parsed rows. Unlike the parseRows method, this method requires that the first line of the CSV file contain a comma-separated list of column names; these column names become the attributes on the returned objects. For example, consider the following CSV file:

Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38

The resulting JavaScript array is:

[
  {"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
  {"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]

Note that the values themselves are always strings; they will not be automatically converted to numbers. JavaScript may coerce strings to numbers for your automatically (for example, using the + operator). Alternatively, you can convert the strings to numbers by iterating over the returned objects:

rows.forEach(function(o) {
  o.Year = parseInt(o.Year);
  o.Length = parseFloat(o.Length);
});

Using + rather than parseInt or parseFloat is typically faster, though more restrictive. For example, "30px" when coerced using + returns NaN, while parseInt and parseFloat return 30.

# d3.csv.parseRows(string[, accessor])

# d3.csv.format(rows)

Clone this wiki locally