source: Dev/trunk/d3/src/layout/cluster.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 2.1 KB
Line 
1// Implements a hierarchical layout using the cluster (or dendogram) algorithm.
2d3.layout.cluster = function() {
3  var hierarchy = d3.layout.hierarchy().sort(null).value(null),
4      separation = d3_layout_treeSeparation,
5      size = [1, 1]; // width, height
6
7  function cluster(d, i) {
8    var nodes = hierarchy.call(this, d, i),
9        root = nodes[0],
10        previousNode,
11        x = 0,
12        kx,
13        ky;
14
15    // First walk, computing the initial x & y values.
16    d3_layout_treeVisitAfter(root, function(node) {
17      if (node.children) {
18        node.x = d3_layout_clusterX(node.children);
19        node.y = d3_layout_clusterY(node.children);
20      } else {
21        node.x = previousNode ? x += separation(node, previousNode) : 0;
22        node.y = 0;
23        previousNode = node;
24      }
25    });
26
27    // Compute the left-most, right-most, and depth-most nodes for extents.
28    var left = d3_layout_clusterLeft(root),
29        right = d3_layout_clusterRight(root),
30        x0 = left.x - separation(left, right) / 2,
31        x1 = right.x + separation(right, left) / 2;
32
33    // Second walk, normalizing x & y to the desired size.
34    d3_layout_treeVisitAfter(root, function(node) {
35      node.x = (node.x - x0) / (x1 - x0) * size[0];
36      node.y = (1 - node.y / root.y) * size[1];
37    });
38
39    return nodes;
40  }
41
42  cluster.separation = function(x) {
43    if (!arguments.length) return separation;
44    separation = x;
45    return cluster;
46  };
47
48  cluster.size = function(x) {
49    if (!arguments.length) return size;
50    size = x;
51    return cluster;
52  };
53
54  return d3_layout_hierarchyRebind(cluster, hierarchy);
55};
56
57function d3_layout_clusterY(children) {
58  return 1 + d3.max(children, function(child) {
59    return child.y;
60  });
61}
62
63function d3_layout_clusterX(children) {
64  return children.reduce(function(x, child) {
65    return x + child.x;
66  }, 0) / children.length;
67}
68
69function d3_layout_clusterLeft(node) {
70  var children = node.children;
71  return children ? d3_layout_clusterLeft(children[0]) : node;
72}
73
74function d3_layout_clusterRight(node) {
75  var children = node.children;
76  return children ? d3_layout_clusterRight(children[children.length - 1]) : node;
77}
Note: See TracBrowser for help on using the repository browser.