Flame Graph in Angular
Using ApexCharts with Angular
This Flame Graph example wires ApexCharts into Angular through the ng-apexcharts component.
Install it with npm install ng-apexcharts apexcharts, then mount the chart with <apx-chart [series]="series" [chart]="chart"></apx-chart>.
This demo uses imperative chart updates. The generated code is a faithful Angular translation: open it in CodeSandbox to run and tweak.
import { Component, AfterViewInit, OnDestroy, ViewChild } from '@angular/core';
import {
ChartComponent,
ApexAxisChartSeries,
ApexNonAxisChartSeries,
ApexChart,
ApexXAxis,
ApexYAxis,
ApexTitleSubtitle,
ApexDataLabels,
ApexStroke,
ApexFill,
ApexLegend,
ApexTooltip,
ApexMarkers,
ApexPlotOptions,
ApexResponsive,
ApexGrid,
ApexAnnotations,
ApexStates,
ApexTheme,
NgApexchartsModule,
} from 'ng-apexcharts';
export type ChartOptions = {
series?: ApexAxisChartSeries | ApexNonAxisChartSeries;
chart?: ApexChart;
xaxis?: ApexXAxis;
yaxis?: ApexYAxis | ApexYAxis[];
title?: ApexTitleSubtitle;
subtitle?: ApexTitleSubtitle;
dataLabels?: ApexDataLabels;
stroke?: ApexStroke;
fill?: ApexFill;
legend?: ApexLegend;
tooltip?: ApexTooltip;
markers?: ApexMarkers;
plotOptions?: ApexPlotOptions;
responsive?: ApexResponsive[];
grid?: ApexGrid;
annotations?: ApexAnnotations;
states?: ApexStates;
theme?: ApexTheme;
colors?: string[];
labels?: any;
};
@Component({
selector: 'app-chart',
standalone: true,
imports: [NgApexchartsModule],
templateUrl: './chart.component.html',
})
export class AppChart implements AfterViewInit, OnDestroy {
@ViewChild('chart') chart!: ChartComponent;
public chartOptions: Partial<ChartOptions> = {
series: [
{
data: [
{
x: 'main',
children: [
{
x: 'http.serve',
children: [
{ x: 'router.match', y: 120 },
{
x: 'handler.report',
children: [
{
x: 'db.query',
children: [
{ x: 'net.read', y: 640 },
{ x: 'row.scan', y: 310 },
],
},
{
x: 'json.encode',
children: [
{ x: 'reflect.walk', y: 280 },
{ x: 'buf.grow', y: 90 },
],
},
],
},
{
x: 'handler.upload',
children: [
{ x: 'io.copy', y: 420 },
{ x: 'hash.sum', y: 180 },
],
},
],
},
{
x: 'runtime.gc',
children: [
{ x: 'mark', y: 520 },
{ x: 'sweep', y: 140 },
],
},
{ x: 'sched.idle', y: 1080 },
],
},
],
},
],
chart: {
type: 'icicle',
height: 320,
},
// Warm hues, the flame-graph convention: the colour carries no meaning, it is
// there to separate neighbouring branches of the call tree.
colors: ['#EF4444', '#F97316', '#F59E0B', '#EAB308'],
legend: {
show: false,
},
title: {
text: 'CPU profile: 4,100 samples',
align: 'left',
},
subtitle: {
text: 'Width is share of samples, height is stack depth. Click a frame to zoom.',
align: 'left',
},
plotOptions: {
icicle: {
// Root at the bottom, stacks growing upward.
direction: 'up',
// Alphabetical siblings hold a frame in the same place across profiles, so
// two runs of the same program can be compared by eye. Ordering by size
// would move every frame and lose exactly that.
sort: 'name',
spacing: 1,
dataLabels: {
// Left, not the centred default: a profiler reader scans the left edge
// of the stack, where a frame lines up with the one that called it.
align: 'left',
minSizeToShow: 44,
style: {
fontSize: '11px',
},
},
},
},
stroke: {
width: 1,
colors: ['#fff'],
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
}
ngOnDestroy() {
// no cleanup needed
}
}