0

我正在尝试使用 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  
4

2 回答 2

2

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

注意:我在这里修正了一个小错字:var monday = tbody.select(".monday"),你可能是说var monday = rows.select(".monday").

于 2013-01-13T21:47:08.277 回答
1

我认为这条线.text(d3.values(twitterDays[0][0]));应该是:

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

因为你已经有了数据,所以你应该包括的数据,而不是整个向量。

于 2013-01-13T21:39:32.173 回答