Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Network: force array order when sorting hierarchical levels (almende#…
Browse files Browse the repository at this point in the history
…3576)

* Network: force array order when sorting hierarchical levels

Fixes #340r34.

If coordinates are not available to sort within a hierarchical level, sort to array order instead.

The previous fix on this issue was not good enough to circumvent this quirk in the chromium sorting. This should bury it.

* Added TimSort for sorting in DirectionStrategy

* Added TimSort to LayoutEngine

* Fixed typo
  • Loading branch information
wimrijnders authored and mojoaxel committed Jun 9, 2019
1 parent 07f7482 commit 7ea79ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/network/modules/LayoutEngine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
/**
* There's a mix-up with terms in the code. Following are the formal definitions:
*
Expand Down Expand Up @@ -30,6 +29,8 @@
* The layout is a way to arrange the nodes in the view; this can be done
* on non-hierarchical networks as well. The converse is also possible.
*/
'use strict';
var TimSort = require('timsort');
let util = require('../../util');
var NetworkUtil = require('../NetworkUtil').default;
var {HorizontalStrategy, VerticalStrategy} = require('./components/DirectionStrategy.js');
Expand Down Expand Up @@ -1422,7 +1423,7 @@ class LayoutEngine {
result.push(Number(size));
});

result.sort(function(a, b) {
TimSort.sort(result, function(a, b) {
return b - a;
});

Expand Down
18 changes: 12 additions & 6 deletions lib/network/modules/components/DirectionStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Strategy pattern for usage of direction methods for hierarchical layouts.
*/
var TimSort = require('timsort');


/**
Expand Down Expand Up @@ -87,6 +88,15 @@ class DirectionInterface {
/**
* Sort array of nodes on the unfixed coordinates.
*
* **Note:** chrome has non-stable sorting implementation, which
* has a tendency to change the order of the array items,
* even if the custom sort function returns 0.
*
* For this reason, an external sort implementation is used,
* which has the added benefit of being faster than the standard
* platforms implementation. This has been verified on `node.js`,
* `firefox` and `chrome` (all linux).
*
* @param {Array.<Node>} nodeArray array of nodes to sort
*/
sort(nodeArray) { this.fake_use(nodeArray); this.abstract(); }
Expand Down Expand Up @@ -156,9 +166,7 @@ class VerticalStrategy extends DirectionInterface {

/** @inheritdoc */
sort(nodeArray) {
nodeArray.sort(function(a, b) {
// Test on 'undefined' takes care of divergent behaviour in chrome
if (a.x === undefined || b.x === undefined) return 0; // THIS HAPPENS
TimSort.sort(nodeArray, function(a, b) {
return a.x - b.x;
});
}
Expand Down Expand Up @@ -221,9 +229,7 @@ class HorizontalStrategy extends DirectionInterface {

/** @inheritdoc */
sort(nodeArray) {
nodeArray.sort(function(a, b) {
// Test on 'undefined' takes care of divergent behaviour in chrome
if (a.y === undefined || b.y === undefined) return 0; // THIS HAPPENS
TimSort.sort(nodeArray, function(a, b) {
return a.y - b.y;
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"hammerjs": "^2.0.8",
"keycharm": "^0.2.0",
"moment": "^2.18.1",
"propagating-hammerjs": "^1.4.6"
"propagating-hammerjs": "^1.4.6",
"timsort": "^0.3.0"
},
"devDependencies": {
"async": "^2.5.0",
Expand Down

0 comments on commit 7ea79ea

Please sign in to comment.