【问题标题】:D3 Table Example with Array of Arrays Yields Empty TD Tags带有数组数组的 D3 表示例产生空 TD 标签
【发布时间】: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 datavar columns
  • 特别是,看起来您正在传递分层 JSON,但该示例使用平面 CSV。
  • 正如我在帖子中所说,数据是数组数组[ [ '2013-10', 18000, 43, ], [ '2013-10', 224500, 22, ], }
  • 你为什么认为我在传递 json?

标签: javascript d3.js


【解决方案1】:

要解决columns的值问题,你得改下面这行代码

return {column: column, value: row[column]};

这个

return {column: column, value: row[columns.indexOf(column)]};

享受吧!

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多