{
function dragged(d) {
d.x += d3.event.dx;
d.y += d3.event.dy;
update();
}
function update() {
svg.selectAll("rect")
.attr("x", d => d.x)
.attr("y", d => d.y);
svg.selectAll("path").attr("d", link);
}
const svg = d3.select(DOM.svg(width, height));
const data = [
{
x: width / 10,
y: 60,
height: 80,
width: 100
},
{
x: 3 * width / 4,
y: height / 2,
height: 140,
width: 100
}
];
const links = [
{
source: data[0],
target: data[1]
}
];
const link = d3.linkHorizontal()
.source(function (d) {
return [
d.source.x + d.source.width,
d.source.y + (d.source.height / 2)
]
})
.target(function (d) {
return [
d.target.x,
d.target.y + (d.target.height / 2)
];
});
const drag = d3.drag().on("drag", dragged);
svg.selectAll("rect").data(data)
.enter().append("rect")
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("width", d => d.width)
.attr("height", d => d.height)
.style("fill", "white")
.style("stroke", "#56445D")
.style("stroke-width", "2px")
.call(drag);
svg.selectAll("path").data(links)
.enter().append("path")
.attr("d", link)
.style("fill", "none")
.style("stroke", "#56445D")
.style("stroke-width", "4px");
return svg.node();
}