D3.js庫的node.count()函數用於計算特定節點下的葉子數,並將其作為value屬性附加到對象。如果給定的節點本身是葉節點,則計數為1。
用法:
node.count();
參數:此函數不帶任何參數。
返回值:此函數返回一個對象。
下麵給出的是上麵給出的函數的一些例子。
範例1:以下示例演示了計數根節點的葉子。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" path1tent=
"width=device-width, initial-scale = 1.0" />
<script src="https://d3js.org/d3.v4.min.js">
</script>
</head>
<body>
<script>
// Constructing a tree
var tree = {
name:"rootNode",
children:[
{
name:"child1"
},
{
name:"child2",
children:[
{
name:"grandchild1",
children:[
{ name:"grand_granchild1_1" },
{ name:"grand_granchild1_2" }
]
},
{
name:"grandchild2",
children:[
{ name:"grand_granchild2_1" },
{ name:"grand_granchild2_2" }
]
},
]
}
]
};
var obj = d3.hierarchy(tree);
// Using node.count() Function
var count = obj.count();
console.log("The number of leaves for root"
+ " node are:", count.value);
</script>
</body>
</html>
輸出:
範例2:以下代碼演示了計數任何節點的葉子數。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" path1tent=
"width=device-width, initial-scale = 1.0" />
<script src="https://d3js.org/d3.v4.min.js">
</script>
</head>
<body>
<script>
// Constructing a tree
var tree = {
name:"rootNode", // Root node
children:[
{
name:"child1" // Child of root node
},
{
name:"child2", // Child of root node
children:[
{
// Child of child2
name:"grandchild1",
children:[
// Child of grandchild1
{ name:"grand_granchild1_1" },
// Child of grandchild1
{ name:"grand_granchild1_2" }
]
},
{
name:"grandchild2",
children:[
// Child of grandchild2
{ name:"grand_granchild2_1" },
// Child of grandchild2
{ name:"grand_granchild2_2" }
]
},
]
}
]
};
var obj = d3.hierarchy(tree);
var grandchild2 = obj.children[1].children[1];
// Using node.count() function
var count = grandchild2.count();
console.log("The number of leaves for "
+ "grandchild2 are:", count.value);
</script>
</body>
</html>
輸出:
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js node.count() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。