简体   繁体   English

selectAll()之后的D3 filter()

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

I'm trying to understand the beatiful library D3. 我试图了解美丽的库D3。 Really – I don't understand why the following lines do not work. 真的–我不明白以下几行为什么不起作用。 The code should fill only a few of the cirlces in a special color. 该代码应仅以特殊颜色填充少数几个圆。

<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>

The code looks like (circle = ° ) 代码看起来像(圆=°)

° ° ° ° ° ° ° ° ° ° ° (°) <-special color °°°°°°°°°°°°(°)<特殊颜色

° ° ° ° ° ° ° ° ° ° ° (°) <-special color °°°°°°°°°°°°(°)<特殊颜色

° ° ° ° ° ° ° ° ° ° ° (°) <-special color °°°°°°°°°°°°(°)<特殊颜色

But it does not work with the function filter (after all without it does^^). 但是它不能与功能过滤器一起使用(毕竟没有它会^^)。 It would be great, if someone could help me! 如果有人可以帮助我,那就太好了! Regards 问候

Each 'd' object you are passing to your filter holds all the properties of that circle. 您传递给过滤器的每个“ d”对象都拥有该圆的所有属性。 If you want to filter on a specific index, you can pass two arguments to filter: 如果要对特定索引进行过滤,则可以传递两个参数以进行过滤:

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

if you want to filter on a property of d, you need to access it with dot notation or bracket notation. 如果要过滤d的属性,则需要使用点表示法或方括号表示法对其进行访问。

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

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