Uncaught TypeError: .filter is not a function

Uncaught TypeError: .filter is not a function

我已阅读https://github.com/d3/d3-selection/blob/master/README.md#selection_filter。但是我不知道为什么功能不起作用

var bubble = d3.pack()
    .size([diameter, diameter])
    .padding(2);

var root = d3.hierarchy({"children": data.values});

var node = svg.selectAll(".node")
    .data(bubble(root)
        .filter(function(d) {return !d.children;}))
    .enter().append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
        return "translate(" + d.x + "," + d.y + ")";
    });

在将数据传递给 pack() 函数之前,您必须将其传递给 d3.hierarchy(),即:

Constructs a root node from the specified hierarchical data.

因此,给定您的数据对象:

var data = {
    //Hierarchical data here
};

你必须把它传递给d3.hierarchy():

var root = d3.hierarchy(data);

然后,您可以使用 bubble:

.data(bubble(root)) 

编辑:由于您更改了问题的 title,问题现在很清楚了:bubble(root) returns 一个 object,并且不能在对象中使用 filter。这是一个 array 方法。