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

admin2025-04-26  1

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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745605106a309538.html

最新回复(0)