Skip to content

Commit

Permalink
Merge pull request #100 from treardon17/master
Browse files Browse the repository at this point in the history
Add ability to sort nodes and edges
  • Loading branch information
maxkfranz authored Oct 31, 2022
2 parents 2b338ac + ee5f4a4 commit f6da1f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ var defaults = {
boundingBox: undefined, // constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
transform: function( node, pos ){ return pos; }, // a function that applies a transform to the final node position
ready: function(){}, // on layoutready
sort: undefined, // a sorting function to order the nodes and edges; e.g. function(a, b){ return a.data('weight') - b.data('weight') }
// because cytoscape dagre creates a directed graph, and directed graphs use the node order as a tie breaker when
// defining the topology of a graph, this sort function can help ensure the correct order of the nodes/edges.
// this feature is most useful when adding and removing the same nodes and edges multiple times in a graph.
stop: function(){} // on layoutstop
};
```
Expand Down
4 changes: 4 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ let defaults = {
boundingBox: undefined, // constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
transform: function( node, pos ){ return pos; }, // a function that applies a transform to the final node position
ready: function(){}, // on layoutready
sort: undefined, // a sorting function to order the nodes and edges; e.g. function(a, b){ return a.data('weight') - b.data('weight') }
// because cytoscape dagre creates a directed graph, and directed graphs use the node order as a tie breaker when
// defining the topology of a graph, this sort function can help ensure the correct order of the nodes/edges.
// this feature is most useful when adding and removing the same nodes and edges multiple times in a graph.
stop: function(){} // on layoutstop
};

Expand Down
10 changes: 10 additions & 0 deletions src/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ DagreLayout.prototype.run = function(){

// add nodes to dagre
let nodes = eles.nodes();

if ( isFunction(options.sort) ) {
nodes = nodes.sort( options.sort );
}

for( let i = 0; i < nodes.length; i++ ){
let node = nodes[i];
let nbb = node.layoutDimensions( options );
Expand All @@ -80,6 +85,11 @@ DagreLayout.prototype.run = function(){
let edges = eles.edges().stdFilter(function( edge ){
return !edge.source().isParent() && !edge.target().isParent(); // dagre can't handle edges on compound nodes
});

if ( isFunction(options.sort) ) {
edges = edges.sort( options.sort );
}

for( let i = 0; i < edges.length; i++ ){
let edge = edges[i];

Expand Down

0 comments on commit f6da1f9

Please sign in to comment.