使用d3.js显示表中关联数组的数据

时间:2013-01-13 21:26:17

标签: javascript arrays d3.js html-table associative-array

我试图用d3.js显示表中关联数组的数据 但我无法在表格中得到个别价值。

这是我的阵列的一部分:

    var twitterDays = 
  [ 
    { 
      0: 
      {
        0: 0,
        1: 0,
        2: 0,
        3: 0,
        4: 0,
        5: 0,
        6: 0,
        7: 0,
        8: 0,
        9: 0,
        10: 0,
        11: 0,
        12: 0,
        13: 0,
        14: 0,
        15: 0,
        16: 0,
        17: 0,
        18: 0,
        19: 0,
        20: 0,
        21: 0,
        22: 0,
        23: 0,
        "total": 0
      },
      "day": "monday"
    },
    { 
      1: 
      {
        0: 0,
        1: 0,
        2: 0,
        3: 0,
        4: 0,
        5: 0,
        6: 0,
        7: 0,
        8: 0,
        9: 0,
        10: 0,
        11: 0,
        12: 0,
        13: 0,
        14: 0,
        15: 0,
        16: 0,
        17: 0,
        18: 0,
        19: 0,
        20: 0,
        21: 0,
        22: 0,
        23: 0,
        "total": 0
      },
      "day": "tueday"
    }, etc etc

使用d3.js我正在为不同的键添加值。

这就是我制作表格的方式:

var table = d3.select("#datavis").append("table").attr("class", "twitterTable");
var tbody = table.append("tbody");


var rows = tbody.selectAll("tr")
      .data(twitterDays)
        .enter()
        .append("tr").attr("class", function(d) { return d["day"] });

var monday = tbody.select(".monday")
        .data(d3.values(twitterDays[0][0]))
          .enter()
          .append("td")
          .text(d3.values(twitterDays[0][0]));

这是我认为需要改变的部分:.text(d3.values(twitterDays[0][0]));

问题在于我想要在“td”中创建单独的值(每个1个),而是获得每个“td”中所有值的所有值。

这就是我得到的:

<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td>
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td> 
<td>39,25,20,11,9,8,2,3,9,36,36,48,57,58,51,50,75,63,67,58,62,74,67,64,992</td>
etc etc

我想这样:

<td>39</td>
<td>25</td>
<td>20</td>
<td>11</td>
<td>9</td>
<td>8</td>
<td>2</td>
etc etc  

2 个答案:

答案 0 :(得分:2)

问题确实存在于.text(d3.values(twitterDays[0][0]));。它应该只是.text(String);,如下所示:http://jsfiddle.net/rKa2E/

注意:我在此处修正了一个小错误:var monday = tbody.select(".monday"),您可能需要var monday = rows.select(".monday")

答案 1 :(得分:1)

我认为行.text(d3.values(twitterDays[0][0]));应为:

.text(function(d){return d});

因为您已经拥有数据,所以应该包含数据,而不是整个矢量。