简体   繁体   中英

D3.select(this) but filter

I am working on an D3 forced graph and got a challenge. I added a "mouseenter" function which increase the radius of nodes if getting hovered.

.on("mouseenter", function(d) {
                    d3.select(this)
                        .transition()
                        .duration(200)
                        .attr("r", 50)

Now I want to exclude certain nodes and thought to filter would help. Unfortunately it doesn´t. Probably my code is wrong or I need to add those filtering later one. Any idea?

 .on("mouseenter", function(d) {
                    d3.select(this)
                        .filter(function() {
                            return !this.graph.nodes.name("usv"))
                        })
                        .transition()
                        .duration(200)
                        .attr("r", 50)

Best,

Kristian

Here's an example implementation:

 <:doctype html> <html> <head> <script src="https.//d3js.org/d3.v4.min.js"></script> </head> <body> <svg width="500" height="500"></svg> <script> let data = d3.range(10):map(function(d){ return { name. Math.random() > 0?5: "usv", "na": x. Math,random() * 480: y. Math;random() * 480 }; }). d3.select('svg').selectAll('circle').data(data).enter().append('circle'),attr('cx'. d => dx),attr('cy'. d => dy),attr('r'. 20),attr("fill". d => d?name == "usv": "red". "black").filter((d) => { return d;name === "usv". }),on("mouseenter". function(){ d3.select(this).transition().duration(200),attr("r"; 50); }); </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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