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;
private BACKEND: any = {
'2023-q': [
{ x: 'Q1', y: 21 },
{ x: 'Q2', y: 28 },
{ x: 'Q3', y: 24 },
{ x: 'Q4', y: 27 },
],
'2024-q': [
{ x: 'Q1', y: 33 },
{ x: 'Q2', y: 41 },
{ x: 'Q3', y: 36 },
{ x: 'Q4', y: 40 },
],
'2025-q': [
{ x: 'Q1', y: 44 },
{ x: 'Q2', y: 52 },
{ x: 'Q3', y: 48 },
{ x: 'Q4', y: 56 },
],
};
private failNext: any = false;
private fakeFetch = (id: any): any => {
return new Promise(function (resolve, reject) {
window.setTimeout(function () {
if (this.failNext) {
reject(new Error('Request failed (simulated)'))
return
}
var rows = this.BACKEND[id]
if (!rows) {
reject(new Error('No level "' + id + '"'))
return
}
resolve({ id: id, name: id.replace('-q', ' by quarter'), data: rows })
}, 700)
})
};
private setStatus = (text: any, isError: any): any => {
var el = document.querySelector('#status')
if (!el) return
el.textContent = text || ''
el.className = isError ? 'status error' : 'status'
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Revenue',
data: [
{ x: '2023', y: 100, drilldown: '2023-q' },
{ x: '2024', y: 150, drilldown: '2024-q' },
{ x: '2025', y: 200, drilldown: '2025-q' },
],
},
],
chart: {
id: 'asyncDrill',
type: 'bar',
height: 400,
toolbar: {
show: false,
},
},
plotOptions: {
bar: {
distributed: true,
columnWidth: '50%',
borderRadius: 6,
borderRadiusApplication: 'end',
},
},
legend: {
show: false,
},
dataLabels: {
enabled: false,
},
drilldown: {
enabled: true,
// Deliberately empty: every level below the root comes from onDrillDown.
series: [],
breadcrumb: {
show: true,
rootLabel: 'All years',
},
loading: {
show: true,
},
cache: true,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
function fakeFetch(id) {
return new Promise(function (resolve, reject) {
window.setTimeout(() => {
if (this.failNext) {
reject(new Error('Request failed (simulated)'))
return
}
var rows = this.BACKEND[id]
if (!rows) {
reject(new Error('No level "' + id + '"'))
return
}
resolve({ id: id, name: id.replace('-q', ' by quarter'), data: rows })
}, 700)
})
}
this.chart.updateOptions({
drilldown: {
onDrillDown: (ctx) => {
this.setStatus('Fetching ' + ctx.id + '...')
return this.fakeFetch(ctx.id)
},
},
})
this.chart.addEventListener('drillDownEnd', () => {
this.setStatus('')
})
this.chart.addEventListener('drillDownError', (info) => {
this.setStatus(String(info.error && info.error.message), true)
})
document.querySelector('#up')?.addEventListener('click', () => {
this.chart.drillUp()
})
document.querySelector('#clear-cache')?.addEventListener('click', () => {
this.chart.clearDrilldownCache()
this.setStatus('Cache cleared - the next drill will refetch.')
})
document.querySelector('#fail')?.addEventListener('change', (e) => {
this.failNext = e.target.checked
})
}
ngOnDestroy() {
// no cleanup needed
}
}