Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add header row to d3.csv generated table

Tags:

d3.js

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)

like image 933
Andy Avatar asked Jan 10 '23 13:01

Andy


1 Answers

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!

like image 196
Lekensteyn Avatar answered Jan 13 '23 02:01

Lekensteyn