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,Andy@foo.org
Miles,Wally,719-415-7177,Wally@foo.org
McLaren,Brenda,719-817-1096,Brenda@foo.org

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)

Andy
  • 69
  • 1
  • 5
  • 1
    possible duplicate of [Creating a table linked to a csv file](http://stackoverflow.com/questions/9268645/creating-a-table-linked-to-a-csv-file) – Lekensteyn May 19 '14 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. – Lekensteyn May 19 '14 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. – Andy May 20 '14 at 14:19

1 Answers1

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!

Community
  • 1
  • 1
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • 1
    @Andy Your welcome. If your question is answered, please mark this answer as accepted by clicking the tick on the left of this post. – Lekensteyn May 24 '14 at 13:15