Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to sort nodes and edges #100

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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