{
const d3SvgNode = d3.select(svg`<svg width=${width} height=400></svg>`);
const targetData = {
x: width / 2 + 100,
y: 50
};
const sourceData = {
x: width / 2 - 100,
y: 200
};
const offsetPointData = {
x: targetData.x,
y: d3.mean([targetData.y, sourceData.y]),
offsetMean: 0
};
const midData = {
x: width / 2 + 200,
y: 300
};
const drag = d3.drag().on("drag", dragged);
const link = d3SvgNode
.append("path")
.attr("d", diagonal(sourceData, targetData, midData, offsetPointData))
.attr("fill", "none")
.attr("stroke", "steelblue");
const source = d3SvgNode
.append("circle")
.datum(sourceData)
.attr("r", 10)
.attr("cx", sourceData.x)
.attr("cy", sourceData.y)
.attr("fill", "steelblue")
.attr("cursor", "move")
.call(drag);
const target = d3SvgNode
.append("circle")
.datum(targetData)
.attr("r", 10)
.attr("cx", targetData.x)
.attr("cy", targetData.y)
.attr("fill", "steelblue")
.attr("cursor", "move")
.call(drag);
const mid = d3SvgNode
.append("circle")
.datum(midData)
.attr("r", 10)
.attr("cx", midData.x)
.attr("cy", midData.y)
.attr("fill", "steelblue")
.attr("cursor", "move")
.call(drag);
const offset = d3SvgNode
.append("circle")
.datum(offsetPointData)
.attr("r", 5)
.attr("cx", targetData.x)
.attr(
"cy",
d3.mean([targetData.y, sourceData.y]) + offsetPointData.offsetMean
)
.attr("fill", "steelblue")
.attr("cursor", "move")
.call(drag);
function dragged(d) {
d.x = d3.event.x;
d.y = d3.event.y;
update();
}
function update() {
source.attr("cx", sourceData.x).attr("cy", sourceData.y);
target.attr("cx", targetData.x).attr("cy", targetData.y);
mid.attr("cx", midData.x).attr("cy", midData.y);
offsetPointData.offsetMean =
d3.mean([targetData.y, sourceData.y]) - offsetPointData.y;
console.log(offsetPointData.offsetMean);
offset
.attr("cx", targetData.x)
.attr(
"cy",
d3.mean([targetData.y, sourceData.y]) - offsetPointData.offsetMean
);
link.attr(
"d",
diagonal(sourceData, targetData, midData, {
offsetMean: offsetPointData.offsetMean
})
);
}
yield d3SvgNode.node();
}