根据指定的层级数据构造一个根节点。指定的数据必须是代表根节点的对象,例如:
{
"name": "Eve",
"children": [
{
"name": "Cain"
},
{
"name": "Seth",
"children": [
{
"name": "Enos"
},
{
"name": "Noam"
}
]
},
{
"name": "Abel"
},
{
"name": "Awan",
"children": [
{
"name": "Enoch"
}
]
},
{
"name": "Azura"
}
]
}
将为每个数据调用指定的子访问器函数,从根节点开始,必须返回一个可迭代数据代表孩子,如有的话。如果子访问器没有指定,默认情况下:
function children(d){
return d.children;
}
如果数据是映射,它将被隐式地转换为【undefined, data】条目,子访问器默认为:
function children(d){
return Array.isArray(d) ? d[1] : null
}
允许你通过d3.group或d3.rollup结果给d3.hierarchy。
返回的结果和每个后代有下列属性:
export function hierarchy<Datum>(data: Datum, children?: (d: Datum) => (Datum[] | null | undefined)): HierarchyNode<Datum>;
export interface HierarchyNode<Datum> {
data: Datum;
readonly depth: number;
readonly height: number;
parent: this | null;
children?: this[];
readonly value?: number;
readonly id?: string;
xxxx funcion
xxx funcion
}
parent: this | null, 中的this代表HierarchyNode
export interface HierarchyLink<Datum> {
source: HierarchyNode<Datum>;
target: HierarchyNode<Datym>;
}
源码这里上下文都没有关于这个数据结构的说明,我猜想模拟链子来连接节点的。HierarchyNode.links(): Array<HierarchyLink
Returns an array of links for this node, where each link is an object that defines source and target properties。
The source of each link is the parent node, and the target is a child node。
为当前节点返回一个链接的数组,每个链接是一个定义了源和目标属性的对象。
源是每个链子的父节点,目标是孩子节点。
export interface ClusterLayout<Datum>{
(root: HierarchyNode<Datum>): HierarchyPointNode<Datum>
xxx function;
xxx function;
}
export interface HierarchyPointNode<Datum> extends HierarchyNode<Datum> {
x: number;
y: number;
links(): Array<HierarchyPointLink<Datum>>;
}
export interface TreemapLayout<Datum> {
(root: HierarchyNode<Datum>): HierarchyRectangularNode<Datum>;
xxx function;
xxx function;
}
export interface HierarchyRectangularNode<Datum> extends HierarchyNode<Datum> {
x0: number;
y0: number;
x1: number;
y1: number;
links(): Array<HierarchyRecangularLink<Datum>>
}
原文地址:https://www.cnblogs.com/xiaoxu-xmy/p/13771461.html
GitHub: https://github.com/lemon-Xu/Learning-d3.-Js
作者: lemon
原文:https://www.cnblogs.com/xiaoxu-xmy/p/13771461.html