Hierarchy Layout - InfographicsJa/d3 GitHub Wiki

Wiki โ–ธ API Reference โ–ธ Layouts โ–ธ Hierarchy Layout

้šŽๅฑค layout ใฏใ€็›ดๆŽฅๅˆฉ็”จใ—ใพใ›ใ‚“ใŒใ€่ค‡ๆ•ฐใฎ้šŽๅฑค็š„ layout ใฎ้–“ใงๅ…ฑๆœ‰ใ•ใ‚Œใฆใ„ใ‚‹ abstract ใช layout ใงใ™ใ€‚ๅฎŸ่ฃ…ใฏไธ‹่จ˜ใฎใใ‚Œใžใ‚Œใ‚’ๅ‚็…งใใ ใ•ใ„ใ€‚

  • Cluster - ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใ‚’ๆจนๅฝขๅ›ณใธใจใ‚ฏใƒฉใ‚นใ‚ฟๅŒ–ใ—ใพใ™ใ€‚
  • Pack - ๅ†ๅธฐ็š„ใชๅ††ๅ……่ถณใ‚’็”จใ„ใŸ้šŽๅฑค็š„ layout ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚
  • Partition - node tree ใ‚’ ๆ”พๅฐ„็ŠถใพใŸใฏๆฐทๆŸฑ็Šถใซๅ†ๅธฐ็š„ใซๅŒบๅˆ†ใ—ใพใ™ใ€‚
  • Tree - node tree ใ‚’ๆ•ด็„ถใจไธฆในใพใ™ใ€‚
  • Treemap - node tree ใ‚’ๅ†ๅธฐ็š„ใซ็ฉบ้–“ๅŒบๅˆ†ใ—ใฆ่กจ็พใ—ใพใ™ใ€‚

้šŽๅฑค layout ใ ใ‘ใงใฏใชใใ€ bundle layout ใ‚‚ใพใŸ้šŽๅฑคๆ€งใ‚’ๅซใ‚€็ตๅˆใ•ใ›ใ‚‹ใŸใ‚ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚

# d3.layout.hierarchy()

ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ่จญๅฎšใ‚’ใ‚‚ใคๆ–ฐใ—ใ„้šŽๅฑค็š„ layout ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ sort order ใฏ้™้ †ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ data ใ‚ขใ‚ฏใ‚ปใ‚ตใฏๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใŒๆ•ฐๅ€คใฎ value ๅฑžๆ€งใ‚’ๆŒใคใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ‚ใ‚‹ใ“ใจใ‚’ๆƒณๅฎšใ—ใฆใŠใ‚Šใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ children ใ‚ขใ‚ฏใ‚ปใ‚ตใฏๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใŒ children ้…ๅˆ—ใ‚’ๆŒใคใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ‚ใ‚‹ใ“ใจใ‚’ๆƒณๅฎšใ—ใฆใ„ใพใ™ใ€‚

# hierarchy.sort([comparator])

ใ‚‚ใ—ใ€ comparator ใŒๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใ‚Œใฐใ€ ้šฃๆŽฅใ™ใ‚‹ node ใฎไธฆใณ้ †ใ‚’ layout ๆฑบใ‚ใ‚‹ใŸใ‚ใซไฝฟใ† comparator ้–ขๆ•ฐใ‚’ใ‚ปใƒƒใƒˆใ—ใพใ™ใ€‚ใ‚‚ใ—ใ€ comparator ใŒๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ‘ใ‚Œใฐใ€็พๅœจใฎใ‚ฐใƒซใƒผใƒ—ใงใฎไธฆใณๆ›ฟใˆ้ †ๅบใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใฎ้ †ๅบใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€้–ข้€ฃใ—ใŸๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใฎๆ•ฐๅ€ค value ๅฑžๆ€งใซใ‚ˆใฃใฆ้™้ †ใซ้ †ๅบไป˜ใ‘ใ•ใ‚Œใพใ™ใ€‚

function comparator(a, b) {
  return b.value - a.value;
}

The comparator function is invoked for pairs of nodes, being passed the input data for each node. A null comparator disables sorting and uses tree traversal order. Comparator functions may also be implemented using d3.ascending or d3.descending.

# hierarchy.children([accessor])

If accessor is specified, sets the specified children accessor function. If accessor is not specified, returns the current children accessor function, which by default assumes that the input data is an object with a children array:

function children(d) {
  return d.children;
}

Often, it is convenient to load the node hierarchy using d3.json, and represent the input hierarchy as a nested JSON object. For example:

{
 "name": "flare",
 "children": [
  {
   "name": "analytics",
   "children": [
    {
     "name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
      {"name": "MergeEdge", "size": 743}
     ]
    },
    {
     "name": "graph",
     "children": [
      {"name": "BetweennessCentrality", "size": 3534},
      {"name": "LinkDistance", "size": 5731}
     ]
    }
   ]
  }
 ]
}

The children accessor is first invoked for root node in the hierarchy. If the accessor returns null, then the node is assumed to be a leaf node at the layout traversal terminates. Otherwise, the accessor should return an array of data elements representing the child nodes.

# hierarchy.nodes(root)

Runs the hierarchy layout, returning the array of nodes associated with the specified root node. The input argument to the layout is the root node of the hierarchy, and the output return value is an array representing the computed positions of all nodes. Several attributes are populated on each node:

  • parent - the parent node, or null for the root.
  • children - the array of child nodes, or null for leaf nodes.
  • value - the node value, as returned by the value accessor.
  • depth - the depth of the node, starting at 0 for the root.

In addition, most hierarchy layouts also compute x and y positions for nodes; see the implementing class for details.

# hierarchy.links(nodes)

Given the specified array of nodes, such as those returned nodes, returns an array of objects representing the links from parent to child for each node. Leaf nodes will not have any links. Each link is an object with two attributes:

  • source - the parent node (as described above).
  • target - the child node.

This method is useful for retrieving a set of link descriptions suitable for display, often in conjunction with the diagonal shape generator. For example:

svg.selectAll("path")
    .data(partition.links(nodes))
  .enter().append("path")
    .attr("d", d3.svg.diagonal());

# hierarchy.value([value])

If value is specified, sets the value accessor to the specified function. If value is not specified, returns the current value accessor. The default accessor assumes that the input data is an object with a numeric value attribute:

function value(d) {
  return d.value;
}

The value accessor is invoked for each input data element, and must return a number representing the numeric value of the node. This value is used to set the area of each node proportionally to the value.

# hierarchy.revalue(root)

Re-evaluates the values of each node in the specified tree starting at root, without re-sorting or recomputing the child nodes. This method can be used to recompute the values of each node without making any structural changes to the hierarchy. Primarily, it exists to support sticky treemaps.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ