Angular ApexTree
Installation
npm install ngx-apextree apextree
Demo
View Demo Project created using ngx-apextree
Usage
Basic Example
// app.component.ts
import { Component } from '@angular/core';
import { NgxApextreeComponent, NestedNode, TreeOptions } from 'ngx-apextree';
@Component({
selector: 'app-root',
standalone: true,
imports: [NgxApextreeComponent],
template: `
<ngx-apextree
[data]="treeData"
[options]="treeOptions"
(nodeClick)="onNodeClick($event)"
(graphReady)="onGraphReady($event)"
>
</ngx-apextree>
`,
})
export class AppComponent {
treeData: NestedNode = {
id: '1',
name: 'CEO',
children: [
{
id: '2',
name: 'CTO',
children: [
{ id: '3', name: 'Dev Lead' },
{ id: '4', name: 'QA Lead' },
],
},
{
id: '5',
name: 'CFO',
},
],
};
treeOptions: TreeOptions = {
width: 800,
height: 600,
nodeWidth: 150,
nodeHeight: 60,
direction: 'top',
childrenSpacing: 80,
siblingSpacing: 30,
};
onNodeClick(event: any) {
console.log('Node clicked:', event.node);
}
onGraphReady(graph: any) {
console.log('Graph ready:', graph);
}
}
Custom Node Template
Use Angular's ng-template for custom node rendering:
@Component({
selector: 'app-root',
standalone: true,
imports: [NgxApextreeComponent],
template: `
<ngx-apextree [data]="treeData" [options]="treeOptions">
<ng-template #nodeTemplate let-content>
<div class="custom-node">
<img [src]="content.imageURL" alt="" />
<span>{{ content.name }}</span>
</div>
</ng-template>
</ngx-apextree>
`,
styles: [
`
.custom-node {
display: flex;
align-items: center;
gap: 8px;
height: 100%;
padding: 0 10px;
}
.custom-node img {
width: 40px;
height: 40px;
border-radius: 50%;
}
`,
],
})
export class AppComponent {
treeData: NestedNode = {
id: '1',
data: {
name: 'John Doe',
imageURL: 'https://i.pravatar.cc/300?img=68',
},
children: [
{
id: '2',
data: {
name: 'Jane Smith',
imageURL: 'https://i.pravatar.cc/300?img=69',
},
},
],
};
treeOptions: TreeOptions = {
contentKey: 'data',
width: 800,
height: 600,
nodeWidth: 180,
nodeHeight: 60,
};
}
Custom Tooltip Template
You can provide a custom tooltip using the tooltipTemplate option:
@Component({
selector: 'app-root',
standalone: true,
imports: [NgxApextreeComponent],
template: ` <ngx-apextree [data]="treeData" [options]="treeOptions"></ngx-apextree> `,
})
export class AppComponent {
treeData: NestedNode = {
id: '1',
data: {
name: 'John Doe',
role: 'CEO',
email: 'john@company.com',
},
children: [
{
id: '2',
data: {
name: 'Jane Smith',
role: 'CTO',
email: 'jane@company.com',
},
},
],
};
treeOptions: TreeOptions = {
contentKey: 'data',
width: 800,
height: 600,
enableTooltip: true,
tooltipTemplate: (content: any) => {
return `
<div style="padding: 10px;">
<strong>${content.name}</strong>
<p>${content.role}</p>
<p>${content.email}</p>
</div>
`;
},
};
}
Alternatively, use Angular's ng-template for tooltip rendering:
<ngx-apextree [data]="treeData" [options]="{ enableTooltip: true }">
<ng-template #tooltipTemplate let-content>
<div class="custom-tooltip">
<strong>{{ content.name }}</strong>
<p>{{ content.description }}</p>
</div>
</ng-template>
</ngx-apextree>
Graph Methods
Access graph methods through component reference or the emitted graph instance:
@Component({
template: `
<ngx-apextree
#tree
[data]="treeData"
[options]="treeOptions"
(graphReady)="onGraphReady($event)"
>
</ngx-apextree>
<button (click)="changeDirection()">Change Direction</button>
<button (click)="fit()">Fit Screen</button>
`,
})
export class AppComponent {
@ViewChild('tree') tree!: NgxApextreeComponent;
private graph: any;
onGraphReady(graph: any) {
this.graph = graph;
}
changeDirection() {
// using component method
this.tree.changeLayout('left');
// or using graph instance directly
// this.graph.changeLayout('left');
}
fit() {
this.tree.fitScreen();
}
}
License Setup
If you have a commercial license, set it once at app initialization.
Option 1: Angular Provider (Recommended)
Standalone App:
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideApexTreeLicense } from 'ngx-apextree';
export const appConfig: ApplicationConfig = {
providers: [provideApexTreeLicense('your-license-key-here')],
};
Module-based App:
// app.module.ts
import { NgModule } from '@angular/core';
import { provideApexTreeLicense } from 'ngx-apextree';
@NgModule({
providers: [provideApexTreeLicense('your-license-key-here')],
})
export class AppModule {}
Option 2: Static Method
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { setApexTreeLicense } from 'ngx-apextree';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
// set license before bootstrapping
setApexTreeLicense('your-license-key-here');
bootstrapApplication(AppComponent, appConfig);
API
Inputs
| Input | Type | Description |
|---|---|---|
data | NestedNode | Tree data structure |
options | TreeOptions | Configuration options |
Outputs
| Output | Type | Description |
|---|---|---|
nodeClick | NodeClickEvent | Emits when a node is clicked |
graphReady | ApexTreeGraph | Emits after initial render |
graphUpdated | ApexTreeGraph | Emits after data/options update |
Content Templates
| Template | Context | Description |
|---|---|---|
#nodeTemplate | $implicit: content | Custom node HTML |
#tooltipTemplate | $implicit: content | Custom tooltip HTML |
Component Methods
| Method | Parameters | Description |
|---|---|---|
changeLayout | direction: TreeDirection | Change tree direction |
collapse | nodeId: string | Collapse a node |
expand | nodeId: string | Expand a node |
expandAll | - | Expand every node in one reflow |
collapseAll | - | Collapse every node, leaving the root visible |
expandToDepth | depth: number | Show the tree down to a depth (root = 0) |
updateData | data: NestedNode | Diff against a new dataset and spring to it |
focus | nodeId: string | Spotlight a node's lineage and subtree |
clearFocus | - | Remove the spotlight |
setActivePath | nodeIds: string[] | Flow a marching dash along the root-to-node path |
clearActivePath | - | Clear the active-path flow |
toggleCard | nodeId: string | Toggle a node's expanded card |
zoom | factor: number | Zoom by a fraction of the current scale (positive in, negative out) |
centerOnNode | nodeId: string | Center the camera on a node |
fitScreen | - | Fit graph to screen |
refreshTokens | - | Re-read the tree's CSS variables, the family tokens, the registered theme and the Ignite UI theme, and repaint (apextree 2.3.0 or later) |
getGraph | - | Get graph instance for the full API |
render | - | Manually re-render |
A change to the data input reconciles into the live tree instead of rebuilding it, so surviving nodes spring to their new positions while collapse state, selection, focus and expanded cards survive. A change to the options input rebuilds the instance, since options are read at construction. See Live Data Updates.
Theming and Ignite UI
The options input is typed off the core apextree package you have installed, so with apextree 2.3.0 it takes theme, tokens and designSystem as well as every color option. An option you pass beats the --apex-tree-* CSS variables and the --apx-* tokens, so leave out any option a stylesheet should drive. See Theming and Customization.
To follow an Ignite UI theme on the page, import the add-on once, in main.ts or anywhere before the first tree renders. It ships in the core apextree package and needs no extra licence key:
// main.ts
import 'apextree/ignite-ui';
<!-- per tree: designSystem: false ignores the Ignite UI theme, tokens: false ignores every token -->
<ngx-apextree #tree [data]="data" [options]="{ designSystem: false }"></ngx-apextree>
The tree reads its theme when it renders. When the theme changes without an input change, for example after a stylesheet swap for an Ignite UI light/dark switch, a class toggle that sets the variables, or the add-on loading after the first render, call refreshTokens() on the component. Nodes, expanded cards, selection, focus and the active path are kept:
@ViewChild('tree') tree!: NgxApextreeComponent;
themeLink.onload = () => this.tree.refreshTokens();
refreshTokens() needs ngx-apextree 1.3.0 and apextree 2.3.0; on an older core it does nothing. See Using with Ignite UI.