cluster.js 2.0 KB

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