Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Last active February 16, 2019 08:39
Show Gist options
  • Save LironHazan/ad9de7f7eed1534ba3ed30a85e070a73 to your computer and use it in GitHub Desktop.
Save LironHazan/ad9de7f7eed1534ba3ed30a85e070a73 to your computer and use it in GitHub Desktop.
for d3-force medium post
export const ticked = (link, node, edgepaths, edgelabels) => {
link
.attr("x1", function (d) {return d.source.x;})
.attr("y1", function (d) {return d.source.y;})
.attr("x2", function (d) {return d.target.x;})
.attr("y2", function (d) {return d.target.y;});
node.attr("transform",
function (d) {return "translate(" + d.x + ", " + d.y + ")";});
edgepaths.attr('d', function (d) {
return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y;
});
edgelabels.attr('transform', function (d) {
if (d.target.x < d.source.x) {
let bbox = this.getBBox();
let rx = bbox.x + bbox.width / 2;
let ry = bbox.y + bbox.height / 2;
return 'rotate(180 ' + rx + ' ' + ry + ')';
}
else {
return 'rotate(0)';
}
});
}
// used as:
simulation
.nodes(nodes)
.on("tick", () => { ticked(link, node, edgepaths, edgelabels)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment