17
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

D3.js を使ってcsvからテーブルを出力する

Posted at
var table = d3.select('body').append('table'); //tableタグ追加
var thead = table.append('thead'); //theadタグ追加
var tbody = table.append('tbody'); //tbodyタグ追加

//csv読み込み
d3.csv('hoge.csv', function(csv){
    var headerKyes = d3.map(csv[0]).keys(); //ヘッダー用にkeyを取得
    
    thead.append('tr')    //trタグ追加
        .selectAll('th') 
        .data(headerKyes) 
        .enter()
        .append('th')    //thタグ追加
        .text(function(key){return key});
    
    tbody.selectAll('tr')
        .data(csv)
        .enter()
        .append('tr')    //trタグ追加
            .selectAll('td')
            .data(function (row) { 
                      return d3.entries(row); //rowオブジェクトを配列へ変換
            }) 
            .enter()
            .append('td')    //tdタグ追加   
            .text(function(d){ return d.value })
        
})

example

17
16
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?