Less confusing intialization of node positions.

Fixes #1561.
This commit is contained in:
Mike Bostock 2013-10-05 10:31:24 -07:00
Родитель 2c93d9c192
Коммит c9e0b67948
3 изменённых файлов: 16 добавлений и 23 удалений

13
d3.js поставляемый
Просмотреть файл

@ -5751,7 +5751,7 @@ d3 = function() {
return force;
};
force.start = function() {
var i, j, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o;
var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o;
for (i = 0; i < n; ++i) {
(o = nodes[i]).index = i;
o.weight = 0;
@ -5777,13 +5777,8 @@ d3 = function() {
charges = [];
if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge;
function position(dimension, size) {
var neighbors = neighbor(i), j = -1, m = neighbors.length, x;
while (++j < m) if (!isNaN(x = neighbors[j][dimension])) return x;
return Math.random() * size;
}
function neighbor() {
if (!neighbors) {
neighbors = [];
neighbors = new Array(n);
for (j = 0; j < n; ++j) {
neighbors[j] = [];
}
@ -5793,7 +5788,9 @@ d3 = function() {
neighbors[o.target.index].push(o.source);
}
}
return neighbors[i];
var candidates = neighbors[i], j = -1, m = candidates.length, x;
while (++j < m) if (!isNaN(x = candidates[j][dimension])) return x;
return Math.random() * size;
}
return force.resume();
};

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

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

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

@ -198,7 +198,6 @@ d3.layout.force = function() {
force.start = function() {
var i,
j,
n = nodes.length,
m = links.length,
w = size[0],
@ -239,20 +238,12 @@ d3.layout.force = function() {
if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i);
else for (i = 0; i < n; ++i) charges[i] = charge;
// initialize node position based on first neighbor
// inherit node position from first neighbor with defined position
// or if no such neighbors, initialize node position randomly
// initialize neighbors lazily to avoid overhead when not needed
function position(dimension, size) {
var neighbors = neighbor(i),
j = -1,
m = neighbors.length,
x;
while (++j < m) if (!isNaN(x = neighbors[j][dimension])) return x;
return Math.random() * size;
}
// initialize neighbors lazily
function neighbor() {
if (!neighbors) {
neighbors = [];
neighbors = new Array(n);
for (j = 0; j < n; ++j) {
neighbors[j] = [];
}
@ -262,7 +253,12 @@ d3.layout.force = function() {
neighbors[o.target.index].push(o.source);
}
}
return neighbors[i];
var candidates = neighbors[i],
j = -1,
m = candidates.length,
x;
while (++j < m) if (!isNaN(x = candidates[j][dimension])) return x;
return Math.random() * size;
}
return force.resume();