Javascript 具有数组数组的D3表示例生成空TD标记
我正在尝试创建一个包含以下d3.js的表。也就是说,这就是我正在使用的代码Javascript 具有数组数组的D3表示例生成空TD标记,javascript,d3.js,Javascript,D3.js,我正在尝试创建一个包含以下d3.js的表。也就是说,这就是我正在使用的代码 var table = d3.select("#myTableDiv").append("table") .attr("style", "margin-left: 0px"), thead = table.append("thead"), tbody = table.append("tbody"); // append th
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>
很明显,这里有问题:
return {column: column, value: row[column]};
要解决列的值问题,必须更改以下代码行
return {column: column, value: row[column]};
通过这个
return {column: column, value: row[columns.indexOf(column)]};
享受吧 你能更清楚地告诉我们你传递的数据吗?var数据和var列特别是,看起来您传递的是分层JSON,但示例使用的是平面CSV。正如我在文章中提到的,数据是一个数组[['2013-10',18000,43,],['2013-10',224500,22,],}为什么您认为我传递的是JSON?
return {column: column, value: row[columns.indexOf(column)]};