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 deviceColors: any = ['#1565C0', '#2E7D32', '#EF6C00'];
private mobileOsColors: any = ['#0D47A1', '#1976D2', '#64B5F6'];
private iosColors: any = ['#1565C0', '#42A5F5', '#90CAF9'];
private desktopOsColors: any = ['#1B5E20', '#388E3C', '#66BB6A'];
private tabletOsColors: any = ['#E65100', '#FB8C00'];
public chartOptions: Partial<ChartOptions> = {
series: [
{
data: [
{ x: 'Mobile', y: 55, drilldown: 'mobile' },
{ x: 'Desktop', y: 33, drilldown: 'desktop' },
{ x: 'Tablet', y: 12, drilldown: 'tablet' },
],
},
],
chart: {
type: 'donut',
height: 420,
},
colors: this.deviceColors,
legend: {
position: 'bottom',
},
dataLabels: {
enabled: true,
},
plotOptions: {
pie: {
donut: {
size: '60%',
},
},
},
title: {
text: 'Website Traffic by Device',
align: 'left',
},
subtitle: {
text: 'Click a slice to drill into its breakdown. Use the breadcrumb to go back.',
align: 'left',
},
drilldown: {
enabled: true,
breadcrumb: {
show: true,
position: 'top-right',
rootLabel: 'All Devices',
separator: ' / ',
},
series: [
{
id: 'mobile',
name: 'Mobile by OS',
colors: this.mobileOsColors,
data: [
{ x: 'iOS', y: 30, drilldown: 'mobile-ios' },
{ x: 'Android', y: 23 },
{ x: 'Other', y: 2 },
],
},
{
id: 'mobile-ios',
name: 'iOS Versions',
colors: this.iosColors,
data: [
{ x: 'iOS 17', y: 18 },
{ x: 'iOS 16', y: 9 },
{ x: 'iOS 15', y: 3 },
],
},
{
id: 'desktop',
name: 'Desktop by OS',
colors: this.desktopOsColors,
data: [
{ x: 'Windows', y: 20 },
{ x: 'macOS', y: 10 },
{ x: 'Linux', y: 3 },
],
},
{
id: 'tablet',
name: 'Tablet by OS',
colors: this.tabletOsColors,
data: [
{ x: 'iPadOS', y: 8 },
{ x: 'Android', y: 4 },
],
},
],
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}