繁体   English   中英

selectAll()之后的D3 filter()

[英]D3 filter() after a selectAll()

我试图了解美丽的库D3。 真的–我不明白以下几行为什么不起作用。 该代码应仅以特殊颜色填充少数几个圆。

<svg>
<g id="row_1"> ... </g>
<g id="row_1"> ... </g>
<g id="row_3"> ... </g>
    <circle cx="16.887" cy="333.923" r="6.268"/>
    <circle cx="33.574" cy="333.923" r="6.268"/>
    <circle cx="50.262" cy="333.923" r="6.268"/>
    <circle cx="66.949" cy="333.923" r="6.268"/>
    <circle cx="167.074" cy="333.923" r="6.268"/>
    <circle cx="183.762" cy="333.923" r="6.268"/>
    <circle cx="333.387" cy="333.923" r="6.268"/>
    <circle cx="316.699" cy="333.923" r="6.268"/>
    <circle cx="300.199" cy="334.101" r="6.268"/>
    <circle cx="266.637" cy="333.923" r="6.268"/>
    <circle cx="250.137" cy="333.923" r="6.268"/>
    <circle cx="216.762" cy="333.923" r="6.268"/>
</g>
</svg>


<script>
var i;
var identifierID;
var svg = d3.select("svg");  

for (i=0; i<=20; i++){
   identifierID = "#row_"+i;
   svg.select(identifierID).selectAll("circle")
   .filter(function(d){ return d == 12; }).style("fill","blue") *//the value 12 is a example*
}
</script>

代码看起来像(圆=°)

°°°°°°°°°°°°(°)<特殊颜色

°°°°°°°°°°°°(°)<特殊颜色

°°°°°°°°°°°°(°)<特殊颜色

但是它不能与功能过滤器一起使用(毕竟没有它会^^)。 如果有人可以帮助我,那就太好了! 问候

您传递给过滤器的每个“ d”对象都拥有该圆的所有属性。 如果要对特定索引进行过滤,则可以传递两个参数以进行过滤:

.filter(function(d, i) // i is the index
    return i === 12;
});

如果要过滤d的属性,则需要使用点表示法或方括号表示法对其进行访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM