How to access the DOM element that correlates to a D3 SVG object?

前端 未结 3 770

I\'m trying to learn D3 by experimenting with one of their basic bubblecharts. First task: figure out how to drag an bubble and have it become the topmost object while it\'s

3条回答
  •  遥遥无期
    2020-12-13 17:33

    You can assign IDs and classes to the individual elements if you want when you append:

    node.append("circle.bubble")
    .attr("r", function(d) { return d.r; })
    .style("fill", function(d) { return fill(d.packageName); })
    .attr("id", function(d, i) { return ("idlabel_" + i)})
    .attr("class", "bubble")
    ;
    

    And then you can select by class with selectAll("circle.bubble") or select by id and modify attributes like so:

    var testCircle = svg.select("#idlabel_3")
    .style("stroke-width", 8);
    

提交回复
热议问题