3

我有这个可重用的模式来创建一个表,灵感来自http://bl.ocks.org/3687826,我有两个问题。

这是功能:

d3.table = function(config) {
var columns = [];

var tbl = function(selection) {
if (columns.length == 0) columns = d3.keys(selection.data()[0][0]);
console.log(columns)    

// Creating the table
var table = selection.append("table");
var thead = table.append("thead");
var tbody = table.append("tbody");

// appending the header row
var th = thead.selectAll("th")
        .data(columns)

th.enter().append("th");
    th.text(function(d) { return d });
th.exit().remove()

// creating a row for each object in the data
var rows = tbody.selectAll('tr')
    .data(function(d) { return d; })

rows.enter().append("tr");
rows.attr('data-row',function(d,i){return i});
rows.exit().remove();   

// creating a cell for each column in the rows
var cells = rows.selectAll("td")
        .data(function(row) {
    return columns.map(function(key) {
                return {key:key, value:row[key]};
    });
        })

cells.enter().append("td");
cells.text(function(d) { return d.value; })
    .attr('data-col',function(d,i){return i})
    .attr('data-key',function(d,i){return d.key});
cells.exit().remove();

return tbl;
};

tbl.columns = function(_) {
if (!arguments.length) return columns;
columns = _;
return this;
};

return tbl;
};

这个表可以这样调用:

/// new table
var t = d3.table();

/// loading data
d3.csv('reusable.csv', function(error,data) {
    d3.select("body")
    .datum(data.filter(function(d){return d.price<850})) /// filter on lines
    .call(t)
});

reusable.csv 文件是这样的:

date,price
Jan 2000,1394.46
Feb 2000,1366.42
Mar 2000,1498.58
Apr 2000,1452.43
May 2000,1420.6
Jun 2000,1454.6
Jul 2000,1430.83
Aug 2000,1517.68
Sep 2000,1436.51

并且列数可以通过

t.columns(["price"]); 
d3.select("body").call(t);

问题是在更新时创建了另一个带有 thead 和 tbody 的表,因为该表的创建是在函数内部。

我怎么说“只创建一次表,然后更新”?

另一个问题是:如何使用函数内部的方法过滤行?

4

1 回答 1

4

问题在于这三行代码:

// Creating the table
var table = selection.append("table");
var thead = table.append("thead");
var tbody = table.append("tbody");

它总是将新的 table、thead 和 tbody 元素附加到您的文档中。只有当这些元素尚不存在时,您才能有条件地执行此操作(您引用的示例类似地创建其 div.header 元素):

selection.selectAll('table').data([0]).enter().append('table');
var table = selection.select('table');

table.selectAll('thead').data([0]).enter().append('thead');
var thead = table.select('thead');

table.selectAll('tbody').data([0]).enter().append('tbody');
var tbody = table.select('tbody');

如果未找到,则 selectAll().data([0]).enter().append() 模式有条件地创建单个元素。引用的示例使用 data([true]) 但任何具有单个元素的数组都可以。

要从函数中过滤嵌套数据,请将您的调用更改为 data() 并传递选择数据的过滤子集,如下所示:

var rows = tbody.selectAll('tr').data(tbody.data()[0].filter(function(d) { 
    return d.price > 1400; 
}));

祝你好运!

于 2013-01-26T09:24:13.813 回答