To draw a line between the circles, you do not need anything special - just the line element.
var line = svg.append("line") .style("stroke", "black") .attr("x1", 150) .attr("y1", 100) .attr("x2", 250) .attr("y2", 300);
Updating a dynamic position is a bit more complicated. At the moment, you have no way to distinguish which of the circles is dragging. One way to do this is to add a difference class to the g elements.
var g1 = svg.append("g") .attr("transform", "translate(" + 150 + "," + 100 + ")") .attr("class", "first") ...
and similarly for another. Now you can include the class in your dragmove function and update the start or end coordinates of the line.
if(d3.select(this).attr("class") == "first") { line.attr("x1", x); line.attr("y1", y); } else { line.attr("x2", x); line.attr("y2", y); }
Fill in the example here . There are other, more elegant ways to achieve this. In a real application, you will have data attached to the elements, and they can use them to distinguish between different circles.
source share