Module

DataViz.Layout.Hierarchy.Core

Package
purescript-hylograph-layout
Repository
afcondon/purescript-hylograph-layout

DataViz.Layout.Hierarchy.Core

Pure PureScript implementation of D3 hierarchy core functions. Matches D3's algorithms exactly but using pure functional style.

#hierarchy Source

hierarchy :: forall a. a -> (a -> Maybe (Array a)) -> HierarchyNode a

Construct a hierarchy from user data Matches D3's hierarchy() function

Simplified implementation using recursion instead of D3's iterative approach This is more idiomatic for PureScript while producing identical results

#getHeight Source

getHeight :: forall a. HierarchyNode a -> Int

#sum Source

sum :: forall a. HierarchyNode a -> (a -> Number) -> ValuedNode a

Compute aggregate values bottom-up Matches D3's .sum() method

Formula: node.value = value(node.data) + Σ(child.value)

#count Source

count :: forall a. HierarchyNode a -> ValuedNode a

Count descendants Matches D3's .count() method

Formula:

  • Leaf: value = 1
  • Internal: value = Σ(child.value)

#eachBefore Source

eachBefore :: forall a b. (HierarchyNode a -> b) -> HierarchyNode a -> Array b

Pre-order traversal (visit parent before children) Matches D3's .eachBefore() method

Order: root → A → A1 → A2 → B → B1 → B2

#eachAfter Source

eachAfter :: forall a b. (HierarchyNode a -> b) -> HierarchyNode a -> Array b

Post-order traversal (visit children before parent) Matches D3's .eachAfter() method

Order: A1 → A2 → A → B1 → B2 → B → root

#descendants Source

descendants :: forall a. HierarchyNode a -> Array (HierarchyNode a)

Get all descendant nodes (including self) Matches D3's .descendants() method

#leaves Source

leaves :: forall a. HierarchyNode a -> Array (HierarchyNode a)

Get all leaf nodes (nodes with no children) Matches D3's .leaves() method

#sortHierarchy Source

sortHierarchy :: forall a. (HierarchyNode a -> HierarchyNode a -> Ordering) -> HierarchyNode a -> HierarchyNode a

Sort children by comparator Matches D3's .sort() method

Note: This returns a new hierarchy (pure function)