-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcalc.js
51 lines (41 loc) · 1.42 KB
/
calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var tarjan = require('strongly-connected-components');
var Lib = require('../../lib');
function circularityPresent(nodeList, sources, targets) {
var nodes = nodeList.map(function() {return [];});
for(var i = 0; i < Math.min(sources.length, targets.length); i++) {
if(sources[i] === targets[i]) {
return true; // self-link which is also a scc of one
}
nodes[sources[i]].push(targets[i]);
}
var scc = tarjan(nodes);
// Tarján's strongly connected components algorithm coded by Mikola Lysenko
// returns at least one non-singular component if there's circularity in the graph
return scc.components.some(function(c) {
return c.length > 1;
});
}
module.exports = function calc(gd, trace) {
if(circularityPresent(trace.node.label, trace.link.source, trace.link.target)) {
Lib.error('Circularity is present in the Sankey data. Removing all nodes and links.');
trace.link.label = [];
trace.link.source = [];
trace.link.target = [];
trace.link.value = [];
trace.link.color = [];
trace.node.label = [];
trace.node.color = [];
}
return [{
link: trace.link,
node: trace.node
}];
};