You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use ApexTree with a commercial license, set your license key before creating any chart instances:
importApexTreefrom'apextree';// set license key before creating any chartsApexTree.setLicense('your-license-key');consttree=newApexTree(document.getElementById('svg-tree'),options);constgraph=tree.render(data);
Tree Options
The layout can be configured by passing a second argument to ApexTree with the properties listed below.
Layout & Canvas
Option
Type
Default
Description
width
number | string
'100%'
Width of the canvas. Accepts a pixel number or CSS percentage string.
height
number | string
'auto'
Height of the canvas. 'auto' sizes to content.
viewPortWidth
number
800
Internal SVG viewport width in pixels.
viewPortHeight
number
600
Internal SVG viewport height in pixels.
direction
'top' | 'bottom' | 'left' | 'right'
'top'
Direction the tree grows from the root node.
contentKey
string
'name'
Key in the data object used as the node display label. Set to 'data' to pass a structured object to nodeTemplate.
siblingSpacing
number
50
Horizontal distance between sibling nodes in pixels.
childrenSpacing
number
50
Vertical distance between a parent node and its children in pixels.
canvasStyle
string
''
Arbitrary CSS injected onto the SVG root container element.
containerClassName
string
'root'
CSS class name for the root SVG container element.
theme
'light' | 'dark' | 'custom'
'light'
Built-in theme preset. 'dark' uses slate backgrounds for dark-mode apps. 'custom' disables built-in CSS variable injection so host-page variables take precedence.
Interaction & Features
Option
Type
Default
Description
highlightOnHover
boolean
true
Highlight the hovered node and its connecting edges.
enableAnimation
boolean
true
Animate node expansion/collapse transitions.
enableExpandCollapse
boolean
true
Show expand/collapse buttons on nodes that have children.
enableExpandCollapseZoom
boolean
true
Re-fit the viewBox to the new tree bounds on collapse/expand. Set to false to keep the viewBox fixed.
enableToolbar
boolean
false
Show the zoom/pan toolbar.
enableSearch
boolean
false
Show a search input in the toolbar. Filters nodes by label, highlights matches, and centers on the first match on Enter.
enableSelection
'single' | 'multi' | false
false
Node selection mode. 'single' allows one selected node at a time. 'multi' allows toggling multiple nodes. Selected nodes get aria-selected="true" and a visible ring.
enableBreadcrumb
boolean
false
Show a breadcrumb trail above the chart. Updates on node click to show the path from root to the selected node.
groupLeafNodes
boolean
false
Stack leaf nodes vertically instead of spreading them horizontally.
groupLeafNodesSpacing
number
10
Spacing between stacked leaf nodes in pixels.
onNodeClick
(node: unknown) => void
undefined
Callback fired when the user clicks a node. Receives the raw node data object.
Node Styling
Option
Type
Default
Description
nodeWidth
number
50
Width of each node in pixels.
nodeHeight
number
30
Height of each node in pixels.
nodeTemplate
(content: unknown) => string
built-in
Custom function returning an HTML string rendered inside each node. Receives the value at contentKey.
When contentKey is set to 'data' and the data object contains any of imageURL, title, subtitle, badge, or accentColor, the built-in template automatically renders a structured org-chart card with avatar, name, title, subtitle, optional badge, and optional accent stripe — no custom nodeTemplate needed.
The tree.render(data) call returns a graph instance with the following public methods:
Layout & View
Method
Description
changeLayout(direction)
Switch the tree direction dynamically ('top', 'bottom', 'left', 'right').
fitScreen()
Re-fit the viewBox to show all visible nodes.
centerOnNode(nodeId)
Pan and zoom to center a specific node in the viewport.
Expand & Collapse
Method
Description
collapse(nodeId)
Programmatically collapse a node.
expand(nodeId)
Programmatically expand a node.
Selection
Method
Description
getSelection()
Returns an array of currently selected node IDs.
setSelection(ids)
Programmatically set selected nodes by ID array.
clearSelection()
Clear all selections.
onSelectionChange(listener)
Register a callback fired whenever the selection changes. Pass null to remove.
Search
Method
Description
findNodesByQuery(query)
Returns an array of node IDs whose labels match the query string.
setSearchHighlight(matchIds)
Highlight specific nodes as search results. Pass an empty array to clear.
Data & Nodes
Method
Description
construct(data)
Replace the tree data and re-render.
getNodeMap()
Returns a map of all node IDs to their node objects.
getRootNodeId()
Returns the ID of the root node.
getNodeLabel(nodeId)
Returns the display label for a node.
Breadcrumb
Method
Description
setBreadcrumbHandler(handler)
Register a callback for breadcrumb segment clicks. Pass null to remove.
Export
Method
Description
exportToSvg()
Export the current tree as an SVG file download.
Example
consttree=newApexTree(document.getElementById('svg-tree'),options);constgraph=tree.render(data);// Change layout directiongraph.changeLayout('left');// Collapse a node programmaticallygraph.collapse('node-2');// Listen for selection changesgraph.onSelectionChange((ids)=>{console.log('Selected:',ids);});// Search and highlightconstmatches=graph.findNodesByQuery('engineer');graph.setSearchHighlight(matches);graph.centerOnNode(matches[0]);