The d3.csv() function in D3.js is a part of the request API that returns a request for the file of type CSV at the specified URL. The mime type is text/CSV.
Syntax:
d3.csv(url[[, row], callback])
Parameters:
- url: It is the URL of the file that is to be fetched.
- callback: It is the function that is to be performed after the file is fetched.
Return Value: It returns a request for the file of type text/csv.CSV
Example 1: Fetching a file named sample.csv which is stored in the same location where the index.html is present. Please create the sample.csv file if not created already. Data of the CSV file is given as a comment in the code below.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" path1tent = "width=device-width,initial-scale=1.0" > </ head > < body > < script src = </ script > < script > // Sample.csv file data // should be like this // year, population // 2006, 40 // 2008, 45 // 2010, 48 // 2012, 51 // 2014, 53 // 2016, 57 // 2017, 62 // Fetch file d3.csv("sample.csv", (d) => { console.log(d) }) </ script > </ body > </ html > |
Output:
Example 2:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" path1tent = "width=device-width,initial-scale=1.0" > </ head > < body > < script src = </ script > < script > // Fake json request d3.csv( (d) => { console.log(d) }) </ script > </ body > </ html > |
Output: