5

How do I place the first row of entries in a csv file into a table heading using d3.csv?

Here's my csv data file:

Surname,Name,Phone,eMail
Stupich,Andrew,719-817-2323,[email protected]
Miles,Wally,719-415-7177,[email protected]
McLaren,Brenda,719-817-1096,[email protected]

Here's my html file:

<!DOCTYPE html>
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>D3.csv Demo</title>
    <style type="text/css"> 
        body {
            margin-left: 4%;
            margin-right: 4%;
        }
        table {
            border-collapse: collapse;
            border: 2px #000 solid;
        }
        th {
            border: 1px #F00 solid;
            text-align: center;
            font-weight: bold;
        }
        td {
            border: 1px #00F solid;
            text-align: center;
            padding: 5px;
        }
    </style>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
    <script type="text/javascript" charset="utf-8"> 
        d3.text("DemoData.csv", function(datasetText) {
        var parsedCSV = d3.csv.parseRows(datasetText);
        var sampleHTML = d3.select("#viz6")
            .append("table")

            .selectAll("tr")
            .data(parsedCSV)
            .enter().append("tr")

            .selectAll("td")       
            .data(function(d){return d;})       
            .enter().append("td")
            .text(function(d){return d;})
        });
    </script>   
</head>
<body>
    <h1>D3.csv Demo</h1>
  <div id="viz6"></div>
</body> 
</html>

How and where do I place th row? Does it have something to do with d3.text vs d3.csv?

Thanks for your help; please be patient, I'm completely new to d3.csv with js - not quite certain how any of it works - what documentation I can find is completely incomprehensible to a beginner. (I'm comfortable with HTML/CSS/PHP)

3
  • 1
    possible duplicate of Creating a table linked to a csv file Commented May 19, 2014 at 22:31
  • Side note: your meta charset tag is missing a quote ("). If you look in your Developer tools / Console (Ctrl + Shift + K in Firefox, F12 in Chromium) you should have caught that. Commented May 19, 2014 at 22:32
  • Hmmm. The suggested resource "Creating a table linked to a CSV file" is no help. It requires you know the content of the first row of the .csv file before you read the file! I want to read in the first row from the .csv file and use the entries in that row as the heading for the rest of the file contents. Commented May 20, 2014 at 14:19

1 Answer 1

10

The parseRows function takes a string and returns a array of arrays. You must split this array yourself into the header fields (rows[0]) and body elements (elements starting at index 1, rows.slice(1)):

d3.text("DemoData.csv", function (datasetText) {
    var rows = d3.csv.parseRows(datasetText);
    var tbl = d3.select("#viz6")
        .append("table");

    // headers
    tbl.append("thead").append("tr")
        .selectAll("th")
        .data(rows[0])
        .enter().append("th")
        .text(function(d) {
            return d;
        });

    // data
    tbl.append("tbody")
        .selectAll("tr").data(rows.slice(1))
        .enter().append("tr")

        .selectAll("td")
        .data(function(d){return d;})
        .enter().append("td")
        .text(function(d){return d;})
});

If you are going to use the d3.csv() function, have a look at https://stackoverflow.com/a/9507713/427545. When the keys are not known beforehand, then you can use the Object.keys() function on the data element to retrieve an array of keys. Note: the order of those keys are not guaranteed!

Sign up to request clarification or add additional context in comments.

1 Comment

@Andy Your welcome. If your question is answered, please mark this answer as accepted by clicking the tick on the left of this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.