【发布时间】:2014-06-17 10:41:04
【问题描述】:
我正在尝试在this example 之后使用 d3.js 创建一个表。即这是我正在使用的代码
var table = d3.select("#myTableDiv").append("table")
.attr("style", "margin-left: 0px"),
thead = table.append("thead"),
tbody = table.append("tbody");
// append the header row
thead.append("tr")
.selectAll("th")
.data(columns)
.enter()
.append("th")
.text(function(column) { return column; });
// create a row for each object in the data
var rows = tbody.selectAll("tr")
.data(data)
.enter()
.append("tr");
// create a cell in each row for each column
var cells = rows.selectAll("td")
.data(function(row) {
return columns.map(function(column) {
return {column: column, value: row[column]};
});
})
.enter()
.append("td")
.attr("style", "font-family: Courier")
.html(function(d) { return d.value; });
我正在传递看起来像标准 AoA 的数据,如下所示:
[
[
'2013-10',
18000,
43,
],
[
'2013-10',
224500,
22,
],
}
但是当上面的javascript执行时,表格包含正确的行数和列数,但是数据本身是空的
<tbody>
<tr>
<td style="font-family: Courier"></td>
<td style="font-family: Courier"></td>
<td style="font-family: Courier"></td>
</tr>
<tr>
<td style="font-family: Courier"></td>
<td style="font-family: Courier"></td>
<td style="font-family: Courier"></td>
</tr>
</tbody>
我在这里做错了什么?
实际上当我提醒d变量的内容时,我看到了这个
{
'column' => 'month',
'value' => [undefined]
}
显然这里有问题:
return {column: column, value: row[column]};
【问题讨论】:
-
您能给我们更清楚地了解您传入的数据吗?
var data和var columns -
特别是,看起来您正在传递分层 JSON,但该示例使用平面 CSV。
-
正如我在帖子中所说,数据是数组数组
[ [ '2013-10', 18000, 43, ], [ '2013-10', 224500, 22, ], } -
你为什么认为我在传递 json?
标签: javascript d3.js