Refactor: Extract getStress

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-19 10:56:30 +00:00
parent 8ed4500280
commit 816a086ebd
6 changed files with 30 additions and 53 deletions

View File

@@ -1,6 +1,7 @@
import constant from "./constant";
import jiggle from "./jiggle";
import {map} from "d3-collection";
import {getStress} from "./stress";
/**
* Extended link force algorithm to include the stress metric for
@@ -101,27 +102,6 @@ export default function(links) {
}
}
/**
* Calculates the stress. Basically, it computes the difference between
* high dimensional distance and real distance. The lower the stress is,
* the better layout.
* @return {number} - stress of the layout.
*/
function getStress() {
var m = links.length,
totalDiffSq = 0,
totalHighDistSq = 0,
link;
for (var i = 0, source, target, realDist, highDist; i < m; i++) {
link = links[i], source = link.source, target = link.target;
realDist = Math.hypot(target.x-source.x, target.y-source.y);
highDist = distances[i];
totalDiffSq += Math.pow(realDist-highDist, 2);
totalHighDistSq += highDist * highDist;
}
return Math.sqrt(totalDiffSq/totalHighDistSq);
}
force.initialize = function(_) {
nodes = _;
initialize();
@@ -148,7 +128,7 @@ export default function(links) {
};
force.stress = function() {
return getStress();
return getStress(nodes, function(s,t){return distance({source: s, target: t});});
}
return force;