admin管理员组文章数量:1356709
I'm trying to understand the beatiful library 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
I'm trying to understand the beatiful library 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
Share Improve this question edited Oct 11, 2015 at 10:18 LolaRuns asked Oct 10, 2015 at 23:16 LolaRunsLolaRuns 1091 gold badge1 silver badge8 bronze badges 1-
d
would be the data bound to the circle, but you have not bound any data sod
will beundefined
so all elements will return falsey in the filter and you will get an empty selection. – Cool Blue Commented Oct 11, 2015 at 0:27
1 Answer
Reset to default 6Each 'd' object you are passing to your filter holds all the properties of that circle. 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.
本文标签: javascriptD3 filter() after a selectAll()Stack Overflow
版权声明:本文标题:javascript - D3 filter() after a selectAll() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743949410a2567008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论