2016-06-28 11 views
1

こんにちは私はD3に新しいですD3.js

var data = [ 
 
    ["First Name", "Last Name", "Job Title", "Favorite Color", "Wars or Trek?", "Porn Name", "Date of Birth", "Dream Vacation City", "GPA", "Arbitrary Data"], 
 
    ["James,Matman", "Chief Sandwich Eater", "Lettuce Green", "Trek,Digby Green", "January 13, 1979", "Gotham City", "3.1", "RBX-12"], 
 
    ["The", "Tick", "Crimefighter Sorta", "Blue", "Wars", "John Smith", "July 19, 1968", "Athens", "N/A", "Edlund, Ben (July 1996)."] 
 
]; 
 

 
var sortAscending = true; 
 

 
var titles = data[0]; 
 

 
var table = d3.select('#page-wrap') 
 
    .append('table'); 
 

 
var headers = table.append('thead') 
 
    .append('tr') 
 
    .selectAll('th') 
 
    .data(titles) 
 
    .enter() 
 
    .append('th') 
 
    .text(function(d) { 
 
    return d; 
 
    }) 
 
    .on('click', function(d) { 
 
    headers.attr('class', 'header'); 
 

 
    if (sortAscending) { 
 
     rows.sort(function(a, b) { 
 
     return b[d] < a[d]; 
 
     }); 
 
     sortAscending = false; 
 
     this.className = 'aes'; 
 
    } else { 
 
     rows.sort(function(a, b) { 
 
     return b[d] > a[d]; 
 
     }); 
 
     sortAscending = true; 
 
     this.className = 'des'; 
 
    } 
 

 
    }); 
 

 
var rows = table.append('tbody') 
 
    .selectAll('tr') 
 
    .data(data) 
 
    .enter() 
 
    .append('tr'); 
 

 
rows.selectAll('td') 
 
    .data(function(d) { 
 
    return titles.map(function(k) { 
 
     return { 
 
     'value': d[k], 
 
     'name': k 
 
     }; 
 
    }); 
 
    }) 
 
    .enter() 
 
    .append('td') 
 
    .attr('data-th', function(d) { 
 
    return d.name; 
 
    }) 
 
    .text(function(d) { 
 
    return d.value; 
 
    });
\t * { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t } 
 
\t body { 
 
\t font: 14px/1.4 Georgia, Serif; 
 
\t } 
 
\t #page-wrap { 
 
\t margin: 50px; 
 
\t } 
 
\t p { 
 
\t margin: 20px 0; 
 
\t } 
 
\t /* 
 
\t Generic Styling, for Desktops/Laptops 
 
\t */ 
 
\t table { 
 
\t width: 100%; 
 
\t border-collapse: collapse; 
 
\t } 
 
\t /* Zebra striping */ 
 
\t tr:nth-of-type(odd) { 
 
\t background: #eee; 
 
\t } 
 
\t th { 
 
\t background: #333; 
 
\t color: white; 
 
\t font-weight: bold; 
 
\t cursor: s-resize; 
 
\t background-repeat: no-repeat; 
 
\t background-position: 3% center; 
 
\t } 
 
\t td, 
 
\t th { 
 
\t padding: 6px; 
 
\t border: 1px solid #ccc; 
 
\t text-align: left; 
 
\t } 
 
\t th.des:after { 
 
\t content: "\21E9"; 
 
\t } 
 
\t th.aes:after { 
 
\t content: "\21E7"; 
 
\t } 
 
\t /* 
 
\t Max width before this PARTICULAR table gets nasty 
 
\t This query will take effect for any screen smaller than 760px 
 
\t and also iPads specifically. 
 
\t */ 
 
\t @media only screen and (max-width: 760px), 
 
\t (min-device-width: 768px) and (max-device-width: 1024px) { 
 
\t /* Force table to not be like tables anymore */ 
 
\t table, 
 
\t thead, 
 
\t tbody, 
 
\t th, 
 
\t td, 
 
\t tr { 
 
\t  display: block; 
 
\t } 
 
\t /* Hide table headers (but not display: none;, for accessibility) */ 
 
\t thead tr { 
 
\t  position: absolute; 
 
\t  top: -9999px; 
 
\t  left: -9999px; 
 
\t } 
 
\t tr { 
 
\t  border: 1px solid #ccc; 
 
\t } 
 
\t td { 
 
\t  /* Behave like a "row" */ 
 
\t  border: none; 
 
\t  border-bottom: 1px solid #eee; 
 
\t  position: relative; 
 
\t  padding-left: 50%; 
 
\t } 
 
\t td:before { 
 
\t  /* Now like a table header */ 
 
\t  position: absolute; 
 
\t  /* Top/left values mimic padding */ 
 
\t  top: 6px; 
 
\t  left: 6px; 
 
\t  width: 45%; 
 
\t  padding-right: 10px; 
 
\t  white-space: nowrap; 
 
\t } 
 
\t /* 
 
\t \t Label the data 
 
\t \t */ 
 
\t td:before { 
 
\t  content: attr(data-th)": "; 
 
\t  font-weight: bold; 
 
\t  width: 6.5em; 
 
\t  display: inline-block; 
 
\t } 
 
\t } 
 
\t /* Smartphones (portrait and landscape) ----------- */ 
 
\t @media only screen and (min-device-width: 320px) and (max-device-width: 480px) { 
 
\t body { 
 
\t  padding: 0; 
 
\t  margin: 0; 
 
\t  width: 320px; 
 
\t } 
 
\t } 
 
\t /* iPads (portrait and landscape) ----------- */ 
 
\t @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { 
 
\t body { 
 
\t  width: 495px; 
 
\t } 
 
\t } 
 
\t
<div id="page-wrap"></div> 
 
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>

を使用して配列からテーブルデータを表示することができません。私は実際に私のデータセットを使用してd3テーブルを描画するポイントで立ち往生しています。私を助けてください。ヘッダーのみを取得できますが、テーブル行のヘッダーのデータは取得できません。私はこれで何をしているのか分かりません。私に何かを提案してください。前もって感謝します。

答えて

0

rows.selectAll('td') 
    .data(function(d) { 
    return titles.map(function(k) { 
     return { 
     'value': d[k], 
     'name': k 
     }; 
    }); 
    }) 
    .enter() 
    .append('td') 
    .attr('data-th', function(d) { 
    return d.name; 
    }) 
    .text(function(d) { 
    return d.value; 
    }); 

から最後のビットを変更してみてください -

rows.selectAll('td') 
    .data(function(d) { 
    return titles.map(function(k,i) { 
     return { 
     'value': d[i], 
     'name': k 
     }; 
    }); 
    }) 
    .enter() 
    .append('td') 
    .attr('data-th', function(d) { 
    return d.name; 
    }) 
    .text(function(d) { 
    return d.value; 
    }); 

のタイトルは「配列です。各データ入力にアクセスするためのインデックスが必要になります。

+0

あなたの答えのためにそんなにTanuありがとうございます。それは魅力のように、私の必要に応じて動作しますが、私は最初の行に私のヘッダー要素を再び表示する余分な行を取得しています。それを避ける方法。実際には、私はテーブルのデータに2行がありますが、まだ3行が最初の行に繰り返すヘッダー要素で作成されています..私はこれで間違って何ですか?教えてください。 :) – Rach

+0

これは、行に渡されたデータに3行すべてが含まれているためです。行オブジェクトに渡す前にデータ配列をスライスすることができます。 var data2 = data.slice(1、data.length); var行= table.append( 'tbody') .selectAll( 'tr') 。データ(data2) .enter() .append( 'tr'); – Tanu

+0

私はそれを得ました...私はこの分野には新しいので、.sliceメソッドについては不明です。データの2行目からデータの全長までの値をとり、別の変数として保存します。とった。本当にありがとうございました。 :)) – Rach

関連する問題