This commit is contained in:
Mike Bostock 2011-09-17 19:04:02 -07:00
Родитель be57c31753 8d6511a2d0
Коммит 41329688c2
8 изменённых файлов: 138 добавлений и 22 удалений

Просмотреть файл

@ -1030,9 +1030,9 @@ d3.layout.hierarchy = function() {
node = d3_layout_hierarchyInline ? data : {data: data}; node = d3_layout_hierarchyInline ? data : {data: data};
node.depth = depth; node.depth = depth;
nodes.push(node); nodes.push(node);
if (childs) { if (childs && (n = childs.length)) {
var i = -1, var i = -1,
n = childs.length, n,
c = node.children = [], c = node.children = [],
v = 0, v = 0,
j = depth + 1; j = depth + 1;
@ -1054,9 +1054,9 @@ d3.layout.hierarchy = function() {
function revalue(node, depth) { function revalue(node, depth) {
var children = node.children, var children = node.children,
v = 0; v = 0;
if (children) { if (children && (n = children.length)) {
var i = -1, var i = -1,
n = children.length, n,
j = depth + 1; j = depth + 1;
while (++i < n) v += revalue(children[i], j); while (++i < n) v += revalue(children[i], j);
} else if (value) { } else if (value) {
@ -1096,6 +1096,12 @@ d3.layout.hierarchy = function() {
return root; return root;
}; };
// If the new API is used, enabling inlining.
hierarchy.nodes = function(d) {
d3_layout_hierarchyInline = true;
return (hierarchy.nodes = hierarchy)(d);
};
return hierarchy; return hierarchy;
}; };
@ -1354,9 +1360,10 @@ d3.layout.cluster = function() {
// First walk, computing the initial x & y values. // First walk, computing the initial x & y values.
d3_layout_treeVisitAfter(root, function(node) { d3_layout_treeVisitAfter(root, function(node) {
if (node.children) { var children = node.children;
node.x = d3_layout_clusterX(node.children); if (children && children.length) {
node.y = d3_layout_clusterY(node.children); node.x = d3_layout_clusterX(children);
node.y = d3_layout_clusterY(children);
} else { } else {
node.x = previousNode ? x += separation(node, previousNode) : 0; node.x = previousNode ? x += separation(node, previousNode) : 0;
node.y = 0; node.y = 0;
@ -1408,12 +1415,15 @@ function d3_layout_clusterX(children) {
function d3_layout_clusterLeft(node) { function d3_layout_clusterLeft(node) {
var children = node.children; var children = node.children;
return children ? d3_layout_clusterLeft(children[0]) : node; return children && children.length
? d3_layout_clusterLeft(children[0]) : node;
} }
function d3_layout_clusterRight(node) { function d3_layout_clusterRight(node) {
var children = node.children; var children = node.children,
return children ? d3_layout_clusterRight(children[children.length - 1]) : node; n;
return children && (n = children.length)
? d3_layout_clusterRight(children[n - 1]) : node;
} }
// Node-link tree diagram using the Reingold-Tilford "tidy" algorithm // Node-link tree diagram using the Reingold-Tilford "tidy" algorithm
d3.layout.tree = function() { d3.layout.tree = function() {

2
d3.layout.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -14,9 +14,10 @@ d3.layout.cluster = function() {
// First walk, computing the initial x & y values. // First walk, computing the initial x & y values.
d3_layout_treeVisitAfter(root, function(node) { d3_layout_treeVisitAfter(root, function(node) {
if (node.children) { var children = node.children;
node.x = d3_layout_clusterX(node.children); if (children && children.length) {
node.y = d3_layout_clusterY(node.children); node.x = d3_layout_clusterX(children);
node.y = d3_layout_clusterY(children);
} else { } else {
node.x = previousNode ? x += separation(node, previousNode) : 0; node.x = previousNode ? x += separation(node, previousNode) : 0;
node.y = 0; node.y = 0;
@ -68,10 +69,13 @@ function d3_layout_clusterX(children) {
function d3_layout_clusterLeft(node) { function d3_layout_clusterLeft(node) {
var children = node.children; var children = node.children;
return children ? d3_layout_clusterLeft(children[0]) : node; return children && children.length
? d3_layout_clusterLeft(children[0]) : node;
} }
function d3_layout_clusterRight(node) { function d3_layout_clusterRight(node) {
var children = node.children; var children = node.children,
return children ? d3_layout_clusterRight(children[children.length - 1]) : node; n;
return children && (n = children.length)
? d3_layout_clusterRight(children[n - 1]) : node;
} }

Просмотреть файл

@ -10,9 +10,9 @@ d3.layout.hierarchy = function() {
node = d3_layout_hierarchyInline ? data : {data: data}; node = d3_layout_hierarchyInline ? data : {data: data};
node.depth = depth; node.depth = depth;
nodes.push(node); nodes.push(node);
if (childs) { if (childs && (n = childs.length)) {
var i = -1, var i = -1,
n = childs.length, n,
c = node.children = [], c = node.children = [],
v = 0, v = 0,
j = depth + 1; j = depth + 1;
@ -34,9 +34,9 @@ d3.layout.hierarchy = function() {
function revalue(node, depth) { function revalue(node, depth) {
var children = node.children, var children = node.children,
v = 0; v = 0;
if (children) { if (children && (n = children.length)) {
var i = -1, var i = -1,
n = children.length, n,
j = depth + 1; j = depth + 1;
while (++i < n) v += revalue(children[i], j); while (++i < n) v += revalue(children[i], j);
} else if (value) { } else if (value) {
@ -76,6 +76,12 @@ d3.layout.hierarchy = function() {
return root; return root;
}; };
// If the new API is used, enabling inlining.
hierarchy.nodes = function(d) {
d3_layout_hierarchyInline = true;
return (hierarchy.nodes = hierarchy)(d);
};
return hierarchy; return hierarchy;
}; };

Просмотреть файл

@ -0,0 +1,39 @@
require("../env");
require("../../d3");
require("../../d3.layout");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.layout.cluster");
suite.addBatch({
"cluster": {
topic: d3.layout.cluster,
"can handle an empty children array": function(cluster) {
assert.deepEqual(cluster.nodes({value: 1, children: [{value: 1, children: []}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0},
{value: 1, depth: 1, x: 0.25, y: 1},
{value: 1, depth: 1, x: 0.75, y: 1}
]);
},
"can handle zero-valued nodes": function(cluster) {
assert.deepEqual(cluster.nodes({value: 0, children: [{value: 0}, {value: 1}]}).map(layout), [
{value: 0, depth: 0, x: 0.5, y: 0},
{value: 0, depth: 1, x: 0.25, y: 1},
{value: 1, depth: 1, x: 0.75, y: 1}
]);
}
}
});
function layout(node) {
return {
value: node.value,
depth: node.depth,
x: node.x,
y: node.y
};
}
suite.export(module);

Просмотреть файл

@ -0,0 +1,36 @@
require("../env");
require("../../d3");
require("../../d3.layout");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.layout.hierarchy");
suite.addBatch({
"hierarchy": {
topic: d3.layout.hierarchy,
"doesn't overwrite the value of a node that has an empty children array": function(hierarchy) {
var nodes = hierarchy.nodes({value: 1, children: []});
assert.deepEqual(nodes, [
{children: [], depth: 0, value: 1}
]);
hierarchy.revalue(nodes[0]);
assert.deepEqual(nodes, [
{children: [], depth: 0, value: 1}
]);
},
"a valueless node that has an empty children array gets a value of 0": function(hierarchy) {
var nodes = hierarchy.nodes({children: []});
assert.deepEqual(nodes, [
{children: [], depth: 0, value: 0}
]);
hierarchy.revalue(nodes[0]);
assert.deepEqual(nodes, [
{children: [], depth: 0, value: 0}
]);
}
}
});
suite.export(module);

Просмотреть файл

@ -14,7 +14,7 @@ suite.addBatch({
}, },
"ignores zero values": function(partition) { "ignores zero values": function(partition) {
var p = partition().size([3, 3]); var p = partition().size([3, 3]);
assert.deepEqual(p({children: [{value: 1}, {value: 0}, {value: 2}, {children: [{value: 0}, {value: 0}]}]}).map(metadata), [ assert.deepEqual(p.nodes({children: [{value: 1}, {value: 0}, {value: 2}, {children: [{value: 0}, {value: 0}]}]}).map(metadata), [
{x: 0, y: 0, dx: 3, dy: 1}, {x: 0, y: 0, dx: 3, dy: 1},
{x: 2, y: 1, dx: 1, dy: 1}, {x: 2, y: 1, dx: 1, dy: 1},
{x: 3, y: 1, dx: 0, dy: 1}, {x: 3, y: 1, dx: 0, dy: 1},
@ -23,6 +23,17 @@ suite.addBatch({
{x: 3, y: 2, dx: 0, dy: 1}, {x: 3, y: 2, dx: 0, dy: 1},
{x: 3, y: 2, dx: 0, dy: 1} {x: 3, y: 2, dx: 0, dy: 1}
]); ]);
},
"can handle an empty children array": function(partition) {
var p = partition();
assert.deepEqual(p.nodes({children: []}).map(metadata), [
{x: 0, y: 0, dx: 1, dy: 1}
]);
assert.deepEqual(p.nodes({children: [{children: []}, {value: 1}]}).map(metadata), [
{x: 0, y: 0, dx: 1, dy: 0.5},
{x: 1, y: 0.5, dx: 0, dy: 0.5},
{x: 0, y: 0.5, dx: 1, dy: 0.5}
]);
} }
} }
}); });

Просмотреть файл

@ -164,6 +164,16 @@ suite.addBatch({
{"value": 1189} {"value": 1189}
]}).map(layout); ]}).map(layout);
assert.equal(nodes.filter(function(n) { return n.dx < 0 || n.dy < 0; }).length, 0); assert.equal(nodes.filter(function(n) { return n.dx < 0 || n.dy < 0; }).length, 0);
},
"can handle an empty children array": function(treemap) {
assert.deepEqual(treemap().nodes({children: []}).map(layout), [
{x: 0, y: 0, dx: 1, dy: 1}
]);
assert.deepEqual(treemap().nodes({children: [{children: []}, {value: 1}]}).map(layout), [
{x: 0, y: 0, dx: 1, dy: 1},
{x: 0, y: 0, dx: 0, dy: 1},
{x: 0, y: 0, dx: 1, dy: 1}
]);
} }
} }
}); });