The data in a tree
let treeData = {
id:1.name:Data '1'Children: [{id:2.name:Data '2',}... ] }Copy the code
Initialize the tree
const w = 100 // Horizontal width
const h = 100 // Longitudinal height
/** * nodeSize is the amount of space your nodes take up. *separation determines the space between two unification based on whether they are children of the same parent node. * /
let tree = d3.tree().nodeSize(w,h).separation((a,b) = >{return a.parent === b.parent ? 1:2})
// Get data with automatically generated x,y coordinate attributes
let hierarchyData = tree(d3.hierarchy(treeData))
// Flat the data, this can be a flat array, equivalent to a flattened tree structure. Such data is easy to recycle.
let data = hierarchyData.descendants()
Copy the code
conclusion
Still want to see D3 D3 – hierarchy section of official document, www.d3js.org.cn/document/d3…