Thursday, October 16, 2025
HomeLanguagesJavascriptD3.js csv() Function

D3.js csv() Function

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=
        "https://d3js.org/d3.v4.min.js">
    </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=
        "https://d3js.org/d3.v4.min.js">
    </script>
  
    <script>
  
        // Fake json request
        d3.csv(
        (d) => {
            console.log(d)
        })
    </script>
</body>
  
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS