最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Selecting a subset of circles using D3 - Stack Overflow

matteradmin6PV0评论

When creating circles using D3, is it possible to create a group such that they can be selected at a later stage? For example, if circles are created using the following approach:

var dataset = [   [ 30, 50, 20],
                  [ 100, 50, 20],
                  [ 150, 50, 30]];


//Create SVG element
var svg = d3.select("#chart")
            .append("svg")
            .attr("width",  200)
            .attr("height", 200);

// generate circles 
svg.selectAll("circle")
   .data(dataset)
   .enter()
   .append("circle")
   .attr("cx", function(d){
                return d[0];
                })
   .attr("cy", function(d){
                return d[1];
                })
   .attr("r",  function(d){
                return d[2];
                });

Can I tag the circle created from the first array element with circle1 and the second two circles as circle2?

When creating circles using D3, is it possible to create a group such that they can be selected at a later stage? For example, if circles are created using the following approach:

var dataset = [   [ 30, 50, 20],
                  [ 100, 50, 20],
                  [ 150, 50, 30]];


//Create SVG element
var svg = d3.select("#chart")
            .append("svg")
            .attr("width",  200)
            .attr("height", 200);

// generate circles 
svg.selectAll("circle")
   .data(dataset)
   .enter()
   .append("circle")
   .attr("cx", function(d){
                return d[0];
                })
   .attr("cy", function(d){
                return d[1];
                })
   .attr("r",  function(d){
                return d[2];
                });

Can I tag the circle created from the first array element with circle1 and the second two circles as circle2?

Share Improve this question asked Aug 12, 2012 at 22:09 djqdjq 15.3k46 gold badges126 silver badges158 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

absolutely - update the class attribute dynamically based on the data index:

.attr("class", function(d,i) {return i == 0 ? "circle1" : "circle2";});

then use the assigned classes for selecting elements:

d3.select(".circle1"); //first circle
d3.selectAll(".circle2"); //second and third circles
Post a comment

comment list (0)

  1. No comments so far