basant307/AI_Governance_Project
048
1function defaultSeparation(a, b) {2 return a.parent === b.parent ? 1 : 2;3}4 5function meanX(children) {6 return children.reduce(meanXReduce, 0) / children.length;7}8 9function meanXReduce(x, c) {10 return x + c.x;11}12 13function maxY(children) {14 return 1 + children.reduce(maxYReduce, 0);15}16 17function maxYReduce(y, c) {18 return Math.max(y, c.y);19}20 21function leafLeft(node) {22 var children;23 while (children = node.children) node = children[0];24 return node;25}26 27function leafRight(node) {28 var children;29 while (children = node.children) node = children[children.length - 1];30 return node;31}32 33export default function() {34 var separation = defaultSeparation,35 dx = 1,36 dy = 1,37 nodeSize = false;38 39 function cluster(root) {40 var previousNode,41 x = 0;42 43 // First walk, computing the initial x & y values.44 root.eachAfter(function(node) {45 var children = node.children;46 if (children) {47 node.x = meanX(children);48 node.y = maxY(children);49 } else {50 node.x = previousNode ? x += separation(node, previousNode) : 0;51 node.y = 0;52 previousNode = node;53 }54 });55 56 var left = leafLeft(root),57 right = leafRight(root),58 x0 = left.x - separation(left, right) / 2,59 x1 = right.x + separation(right, left) / 2;60 61 // Second walk, normalizing x & y to the desired size.62 return root.eachAfter(nodeSize ? function(node) {63 node.x = (node.x - root.x) * dx;64 node.y = (root.y - node.y) * dy;65 } : function(node) {66 node.x = (node.x - x0) / (x1 - x0) * dx;67 node.y = (1 - (root.y ? node.y / root.y : 1)) * dy;68 });69 }70 71 cluster.separation = function(x) {72 return arguments.length ? (separation = x, cluster) : separation;73 };74 75 cluster.size = function(x) {76 return arguments.length ? (nodeSize = false, dx = +x[0], dy = +x[1], cluster) : (nodeSize ? null : [dx, dy]);77 };78 79 cluster.nodeSize = function(x) {80 return arguments.length ? (nodeSize = true, dx = +x[0], dy = +x[1], cluster) : (nodeSize ? [dx, dy] : null);81 };82 83 return cluster;84}85 