score:20
Accepted answer
The trick is to draw the self link as a path with an arc in it. It took me a bit of fiddling with the arc parameter syntax to get things working and the key seemed to be that the arc could not start and end at the same point. Here is the relevant code that draws the edges at each update.
function tick() {
link.attr("d", function(d) {
var x1 = d.source.x,
y1 = d.source.y,
x2 = d.target.x,
y2 = d.target.y,
dx = x2 - x1,
dy = y2 - y1,
dr = Math.sqrt(dx * dx + dy * dy),
// Defaults for normal edge.
drx = dr,
dry = dr,
xRotation = 0, // degrees
largeArc = 0, // 1 or 0
sweep = 1; // 1 or 0
// Self edge.
if ( x1 === x2 && y1 === y2 ) {
// Fiddle with this angle to get loop oriented.
xRotation = -45;
// Needs to be 1.
largeArc = 1;
// Change sweep to change orientation of loop.
//sweep = 0;
// Make drx and dry different to get an ellipse
// instead of a circle.
drx = 30;
dry = 20;
// For whatever reason the arc collapses to a point if the beginning
// and ending points of the arc are the same, so kludge it.
x2 = x2 + 1;
y2 = y2 + 1;
}
return "M" + x1 + "," + y1 + "A" + drx + "," + dry + " " + xRotation + "," + largeArc + "," + sweep + " " + x2 + "," + y2;
});
And here is a jsfiddle that demonstrates the whole thing, and a screenshot:
Source: stackoverflow.com
Related Query
- D3 Force Layout Graph - Self linking node
- How to get the same node positions in d3's force layout graph
- D3 JS - Force Graph - Doesn't restart layout after removing Node
- Fix Node Position in D3 Force Directed Layout
- How to make force layout graph in D3.js responsive to screen/browser size
- Add text label to d3 node in Force directed Graph and resize on hover
- Simple graph of nodes and links without using force layout
- D3 force directed graph with drag and drop support to make selected node position fixed when dropped
- D3 force layout - linking nodes by name instead of index
- d3.js: suggested node position in force layout
- D3 force layout fix root node at the center
- Space out nodes evenly around root node in D3 force layout
- Add text label to d3 node in Force layout
- Align Marker on node edges D3 Force Layout
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- Prevent node overlap in force directed graph
- Recentering D3 force layout diagram on node click
- Inserting a line break in D3 force layout node labels
- Place pie charts on nodes of force directed layout graph in D3
- How to display a text when mouseover a node in D3 force layout
- Center force directed layout after tick using root node (return to center)
- D3 force layout graph with nodes positioned in a grid
- How to stop a d3 force graph layout simulation?
- D3 Force Layout : How to force a group of node to stay in a given area
- d3js force layout dynamically adding image or circle for each node
- Creating force layout node labels in d3.js
- how to generate a network graph with edge weight using D3.js force layout
- d3.js Node "jumps back" on fast drag in force layout
- Both single and double click on a node in d3 force directed graph
- Nodes drawn randomly in Force Layout graph
More Query from same tag
- Proper data structure for d3 nodes
- Create the appropriate space for variable axis labels
- Create artificial zoom transform event
- D3.js cannot render my circles from a json file
- Calling D3 Tooltip in context of data visualization in AngularJS
- Populating Nested Arrays with Key value pairs
- D3js and React regarding DOM manipulations
- d3 text-wrap plugin changes coordinates
- d3 transition not working when triggered from mouseover/mouseout handler
- D3/Raphael js draws 1000+ animated circle with slow fps
- OpenSeaDragon with D3 svg-overlay draggable elements
- vega-lite: how to specify date in JSON as separate fields containing year, month, and day numbers?
- Grouped line chart, how to draw lines in a repeating pattern
- Multiple Area Charts with D3.js
- Display geojson on a map using d3.js and d3-tile
- D3.js tick format: show when month changes
- set start point in c3 Donut chart
- D3 scatter plot coming out wrong
- D3.js: How to get the selection index, instead of the data index?
- How do I change the colour of my d3.js Bar Graph bars individually and make a Scale for them?
- Trying to return a maximum date using d3.max
- Incrementing numbers in an array in javascript
- Setting d3.js code to point to my .csv instead of #csv?
- d3.nest() to convert csv to json
- Why is the data displayed using D3.js looks cutoff?
- Treemap highlighting child-nodes D3
- Nested Data reformat
- D3.JS, How can I render multiple objects in the same data binding?
- Can i use d3.js for creating interactive visualizations inside an android app?
- D3js-Get the first and the last decile