Your rounded squares are problematic. You have two options: treat them like circles and do as I did in this matter . Or consider them as rectangles (squares) and find the intersection point with the square. Since I already gave the previous answer for circles, let me talk about the square (rectangle) / intersection of the line.
Now I won’t go into the details of the math behind the rectangle / line intersection (there are many google search resources for this), so let's start with the function from this great answer (it deserves more attention) and apply it to your question.
Firstly, I change the link code to work with svg path instead of line . Just cleaner and simpler in my opinion. Using the function linked above, it becomes as simple as:
function ticked(e) { link.attr("d", function(d) { var inter = pointOnRect(d.source.x, d.source.y, d.target.x - 20, d.target.y - 20, d.target.x + 20, d.target.y + 20); return "M" + d.source.x + "," + d.source.y + "L" + inter.x + "," + inter.y; }); ....
Here is the full code:
<!DOCTYPE html> <html> <head> <script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script> <style> .node { stroke: #fff; stroke-width: 1.5px; } .link { stroke: #000; stroke-opacity: .6; } </style> </head> <body> <div id="mainScreen" style="height:100%;width:100%;position:absolute;"> <svg id="diagramLayout" style="height:100%;width:100%;position:absolute;"></svg> </div> <script> var width = 500; var height = 500; var nodeWidth = 40; var nodeHeight = 40; var circleRadius = 5; var diagramLayout; var graphData = { "nodes": [{ "uid": "Term20", "name": "Term20", "image": "images/Term.png" }, { "uid": "glossforArrow", "name": "glossforArrow", "image": "images/Glossary.png" }, { "uid": "Term43", "name": "Term43", "image": "images/Term.png" }, { "uid": "Term1", "name": "Term43", "image": "images/Term.png" }, { "uid": "Term2", "name": "Term43", "image": "images/Term.png" }], "links": [{ "source": "glossforArrow", "target": "Term20", "direction": "output", "label": "Owned Terms" }, { "source": "glossforArrow", "target": "Term43", "direction": "output", "label": "Owned Terms" }, { "source": "glossforArrow", "target": "Term1", "direction": "output", "label": "Owned Terms" }, { "source": "glossforArrow", "target": "Term2", "direction": "output", "label": "Owned Terms" }] }; forceInitialize(graphData) function forceInitialize(graphData) { diagramLayout = d3.select("#diagramLayout") .attr("id", "diagramLayout") </script> </body> </html>
Mark Jan 6 '17 at 18:24 2017-01-06 18:24
source share