Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
MUR Drupal
d3-library
Commits
1896136d
Commit
1896136d
authored
May 18, 2014
by
Mike Bostock
Browse files
Make scaling non-recursive.
parent
ea7b43e8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
74 deletions
+38
-74
d3.js
d3.js
+13
-28
d3.min.js
d3.min.js
+5
-5
src/layout/tree.js
src/layout/tree.js
+17
-38
test/layout/tree-test.js
test/layout/tree-test.js
+3
-3
No files found.
d3.js
View file @
1896136d
...
...
@@ -6887,11 +6887,17 @@
var
nodes
=
hierarchy
.
call
(
this
,
d
,
i
),
root0
=
nodes
[
0
],
root1
=
wrapTree
(
root0
);
d3_layout_hierarchyVisitAfter
(
root1
,
firstWalk
),
root1
.
parent
.
m
=
-
root1
.
z
;
d3_layout_hierarchyVisitBefore
(
root1
,
secondWalk
);
if
(
nodeSize
)
d3_layout_hierarchyVisitBefore
(
root1
,
sizeNode
);
else
{
var
left
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeLeftmost
),
right
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeRightmost
),
x0
=
left
.
x
-
separation
(
left
,
right
)
/
2
,
x1
=
right
.
x
+
separation
(
right
,
left
)
/
2
,
y1
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeDeepest
).
depth
||
1
;
d3_layout_hierarchyVisitBefore
(
root1
,
function
(
node
)
{
node
.
_
.
x
=
(
node
.
_
.
x
-
x0
)
/
(
x1
-
x0
)
*
size
[
0
];
node
.
_
.
y
=
node
.
_
.
depth
/
y1
*
size
[
1
];
if
(
nodeSize
)
d3_layout_hierarchyVisitBefore
(
root0
,
sizeNode
);
else
{
var
left
=
root0
,
right
=
root0
,
bottom
=
root0
;
d3_layout_hierarchyVisitBefore
(
root0
,
function
(
node
)
{
if
(
node
.
x
<
left
.
x
)
left
=
node
;
if
(
node
.
x
>
right
.
x
)
right
=
node
;
if
(
node
.
depth
>
bottom
.
depth
)
bottom
=
node
;
});
var
tx
=
separation
(
left
,
right
)
/
2
-
left
.
x
,
kx
=
size
[
0
]
/
(
right
.
x
+
separation
(
right
,
left
)
/
2
+
tx
),
ky
=
size
[
1
]
/
(
bottom
.
depth
||
1
);
d3_layout_hierarchyVisitBefore
(
root0
,
function
(
node
)
{
node
.
x
=
(
node
.
x
+
tx
)
*
kx
;
node
.
y
=
node
.
depth
*
ky
;
});
}
return
nodes
;
...
...
@@ -6971,8 +6977,8 @@
return
ancestor
;
}
function
sizeNode
(
node
)
{
node
.
_
.
x
*=
size
[
0
];
node
.
_
.
y
=
node
.
_
.
depth
*
size
[
1
];
node
.
x
*=
size
[
0
];
node
.
y
=
node
.
depth
*
size
[
1
];
}
tree
.
separation
=
function
(
x
)
{
if
(
!
arguments
.
length
)
return
separation
;
...
...
@@ -6994,27 +7000,6 @@
function
d3_layout_treeSeparation
(
a
,
b
)
{
return
a
.
parent
==
b
.
parent
?
1
:
2
;
}
function
d3_layout_treeSearch
(
node
,
compare
)
{
var
children
=
node
.
children
;
if
(
children
&&
(
n
=
children
.
length
))
{
var
child
,
n
,
i
=
-
1
;
while
(
++
i
<
n
)
{
if
(
compare
(
child
=
d3_layout_treeSearch
(
children
[
i
],
compare
),
node
)
>
0
)
{
node
=
child
;
}
}
}
return
node
;
}
function
d3_layout_treeRightmost
(
a
,
b
)
{
return
a
.
x
-
b
.
x
;
}
function
d3_layout_treeLeftmost
(
a
,
b
)
{
return
b
.
x
-
a
.
x
;
}
function
d3_layout_treeDeepest
(
a
,
b
)
{
return
a
.
depth
-
b
.
depth
;
}
function
d3_layout_treeLeft
(
v
)
{
var
children
=
v
.
children
;
return
children
.
length
?
children
[
0
]
:
v
.
t
;
...
...
d3.min.js
View file @
1896136d
This source diff could not be displayed because it is too large. You can
view the blob
instead.
src/layout/tree.js
View file @
1896136d
...
...
@@ -18,19 +18,25 @@ d3.layout.tree = function() {
d3_layout_hierarchyVisitBefore
(
root1
,
secondWalk
);
// If a fixed node size is specified, scale x and y.
if
(
nodeSize
)
d3_layout_hierarchyVisitBefore
(
root
1
,
sizeNode
);
if
(
nodeSize
)
d3_layout_hierarchyVisitBefore
(
root
0
,
sizeNode
);
// If a fixed tree size is specified, scale x and y based on the extent.
// Compute the left-most, right-most, and depth-most nodes for extents.
else
{
var
left
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeLeftmost
),
right
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeRightmost
),
x0
=
left
.
x
-
separation
(
left
,
right
)
/
2
,
x1
=
right
.
x
+
separation
(
right
,
left
)
/
2
,
y1
=
d3_layout_treeSearch
(
root0
,
d3_layout_treeDeepest
).
depth
||
1
;
d3_layout_hierarchyVisitBefore
(
root1
,
function
(
node
)
{
node
.
_
.
x
=
(
node
.
_
.
x
-
x0
)
/
(
x1
-
x0
)
*
size
[
0
];
node
.
_
.
y
=
node
.
_
.
depth
/
y1
*
size
[
1
];
var
left
=
root0
,
right
=
root0
,
bottom
=
root0
;
d3_layout_hierarchyVisitBefore
(
root0
,
function
(
node
)
{
if
(
node
.
x
<
left
.
x
)
left
=
node
;
if
(
node
.
x
>
right
.
x
)
right
=
node
;
if
(
node
.
depth
>
bottom
.
depth
)
bottom
=
node
;
});
var
tx
=
separation
(
left
,
right
)
/
2
-
left
.
x
,
kx
=
size
[
0
]
/
(
right
.
x
+
separation
(
right
,
left
)
/
2
+
tx
),
ky
=
size
[
1
]
/
(
bottom
.
depth
||
1
);
d3_layout_hierarchyVisitBefore
(
root0
,
function
(
node
)
{
node
.
x
=
(
node
.
x
+
tx
)
*
kx
;
node
.
y
=
node
.
depth
*
ky
;
});
}
...
...
@@ -146,8 +152,8 @@ d3.layout.tree = function() {
}
function
sizeNode
(
node
)
{
node
.
_
.
x
*=
size
[
0
];
node
.
_
.
y
=
node
.
_
.
depth
*
size
[
1
];
node
.
x
*=
size
[
0
];
node
.
y
=
node
.
depth
*
size
[
1
];
}
tree
.
separation
=
function
(
x
)
{
...
...
@@ -179,33 +185,6 @@ function d3_layout_treeSeparation(a, b) {
// return (a.parent == b.parent ? 1 : 2) / a.depth;
// }
function
d3_layout_treeSearch
(
node
,
compare
)
{
var
children
=
node
.
children
;
if
(
children
&&
(
n
=
children
.
length
))
{
var
child
,
n
,
i
=
-
1
;
while
(
++
i
<
n
)
{
if
(
compare
(
child
=
d3_layout_treeSearch
(
children
[
i
],
compare
),
node
)
>
0
)
{
node
=
child
;
}
}
}
return
node
;
}
function
d3_layout_treeRightmost
(
a
,
b
)
{
return
a
.
x
-
b
.
x
;
}
function
d3_layout_treeLeftmost
(
a
,
b
)
{
return
b
.
x
-
a
.
x
;
}
function
d3_layout_treeDeepest
(
a
,
b
)
{
return
a
.
depth
-
b
.
depth
;
}
// NEXT LEFT
// This function is used to traverse the left contour of a subtree (or
// subforest). It returns the successor of v on this contour. This successor is
...
...
test/layout/tree-test.js
View file @
1896136d
...
...
@@ -18,9 +18,9 @@ suite.addBatch({
]
}).
map
(
layout
),
[
{
name
:
"
1
"
,
depth
:
0
,
x
:
0.5
,
y
:
0
},
{
name
:
"
1-1
"
,
depth
:
1
,
x
:
1
/
6
,
y
:
1
},
{
name
:
"
1-2
"
,
depth
:
1
,
x
:
3
/
6
,
y
:
1
},
{
name
:
"
1-3
"
,
depth
:
1
,
x
:
5
/
6
,
y
:
1
}
{
name
:
"
1-1
"
,
depth
:
1
,
x
:
0.1666666666666666
6
,
y
:
1
},
{
name
:
"
1-2
"
,
depth
:
1
,
x
:
0.5
,
y
:
1
},
{
name
:
"
1-3
"
,
depth
:
1
,
x
:
0.8333333333333333
,
y
:
1
}
]);
},
"
can handle an empty children array
"
:
function
(
tree
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment