D3 JS数据过滤

Che*_*yne 14 d3.js

我试图过滤数据集只显示某些选择元素的标签.这里显示的过滤器似乎工作,除了它创建了数以千计的空白元素,我显然想避免.这是因为过滤器位于追加之后,但是如果我将过滤器移到append语句之上,它就会中断.

我在这做错了什么

  var labels = svg.selectAll("text.label")
    .data(partition.nodes(bp.data.preparedData))
    .enter()
    .append("text")
    .filter(function(d){return d.ci_type === 'type'})
      .attr("class", "label")
      .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
      .text(function(d, i) { return d.name } );
Run Code Online (Sandbox Code Playgroud)

Lar*_*off 25

听起来你想在将数据传递给D3之前对其进行过滤.也就是说,你的代码就是

var labels = svg.selectAll("text.label")
   .data(partition.nodes(bp.data.preparedData).filter(
            function(d){return d.ci_type === 'type'}))
   .enter()
   .append("text")
   .attr("class", "label")
   .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
   .text(function(d, i) { return d.name } );
Run Code Online (Sandbox Code Playgroud)