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 childPlot: any = {
bar: {
horizontal: false,
distributed: false,
columnWidth: '70%',
borderRadius: 3,
borderRadiusApplication: 'end',
},
};
private childLegend: any = { show: true, position: 'top' };
private yearColors: any = ['#1565C0', '#2E7D32', '#EF6C00', '#6A1B9A'];
private bluePalette: any = ['#1565C0', '#42A5F5', '#90CAF9'];
private greenPalette: any = ['#2E7D32', '#66BB6A', '#A5D6A7'];
private amberPalette: any = ['#EF6C00', '#FB8C00', '#FFB74D'];
private purplePalette: any = ['#6A1B9A', '#AB47BC', '#CE93D8'];
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Total Sales',
data: [
{ x: '2021', y: 480, drilldown: '2021' },
{ x: '2022', y: 530, drilldown: '2022' },
{ x: '2023', y: 610, drilldown: '2023' },
{ x: '2024', y: 705, drilldown: '2024' },
],
},
],
chart: {
type: 'bar',
height: 420,
toolbar: {
show: false,
},
},
colors: this.yearColors,
plotOptions: {
bar: {
horizontal: false,
columnWidth: '55%',
borderRadius: 6,
borderRadiusApplication: 'end',
distributed: true,
},
},
legend: {
show: false,
},
dataLabels: {
enabled: false,
},
title: {
text: 'Annual Sales by Channel',
align: 'left',
},
subtitle: {
text: 'Click a year to drill into its channel breakdown (Online / Retail / Wholesale). Use the breadcrumb to go back.',
align: 'left',
},
yaxis: {
title: {
text: 'Sales ($k)',
},
},
drilldown: {
enabled: true,
breadcrumb: {
show: true,
position: 'top-right',
rootLabel: 'All Years',
separator: ' / ',
},
series: [
{
id: '2021',
name: '2021 by Channel',
plotOptions: this.childPlot,
legend: this.childLegend,
colors: this.bluePalette,
series: [
{
name: 'Online',
data: [
{ x: 'Q1', y: 50 },
{ x: 'Q2', y: 55 },
{ x: 'Q3', y: 55 },
{ x: 'Q4', y: 60 },
],
},
{
name: 'Retail',
data: [
{ x: 'Q1', y: 35 },
{ x: 'Q2', y: 38 },
{ x: 'Q3', y: 40 },
{ x: 'Q4', y: 42 },
],
},
{
name: 'Wholesale',
data: [
{ x: 'Q1', y: 22 },
{ x: 'Q2', y: 25 },
{ x: 'Q3', y: 28 },
{ x: 'Q4', y: 30 },
],
},
],
},
{
id: '2022',
name: '2022 by Channel',
plotOptions: this.childPlot,
legend: this.childLegend,
colors: this.greenPalette,
series: [
{
name: 'Online',
data: [
{ x: 'Q1', y: 55 },
{ x: 'Q2', y: 60 },
{ x: 'Q3', y: 62 },
{ x: 'Q4', y: 68 },
],
},
{
name: 'Retail',
data: [
{ x: 'Q1', y: 40 },
{ x: 'Q2', y: 42 },
{ x: 'Q3', y: 43 },
{ x: 'Q4', y: 45 },
],
},
{
name: 'Wholesale',
data: [
{ x: 'Q1', y: 26 },
{ x: 'Q2', y: 28 },
{ x: 'Q3', y: 30 },
{ x: 'Q4', y: 31 },
],
},
],
},
{
id: '2023',
name: '2023 by Channel',
plotOptions: this.childPlot,
legend: this.childLegend,
colors: this.amberPalette,
series: [
{
name: 'Online',
data: [
{ x: 'Q1', y: 62 },
{ x: 'Q2', y: 68 },
{ x: 'Q3', y: 72 },
{ x: 'Q4', y: 78 },
],
},
{
name: 'Retail',
data: [
{ x: 'Q1', y: 45 },
{ x: 'Q2', y: 48 },
{ x: 'Q3', y: 50 },
{ x: 'Q4', y: 52 },
],
},
{
name: 'Wholesale',
data: [
{ x: 'Q1', y: 32 },
{ x: 'Q2', y: 33 },
{ x: 'Q3', y: 35 },
{ x: 'Q4', y: 35 },
],
},
],
},
{
id: '2024',
name: '2024 by Channel',
plotOptions: this.childPlot,
legend: this.childLegend,
colors: this.purplePalette,
series: [
{
name: 'Online',
data: [
{ x: 'Q1', y: 72 },
{ x: 'Q2', y: 78 },
{ x: 'Q3', y: 82 },
{ x: 'Q4', y: 88 },
],
},
{
name: 'Retail',
data: [
{ x: 'Q1', y: 52 },
{ x: 'Q2', y: 55 },
{ x: 'Q3', y: 56 },
{ x: 'Q4', y: 57 },
],
},
{
name: 'Wholesale',
data: [
{ x: 'Q1', y: 40 },
{ x: 'Q2', y: 41 },
{ x: 'Q3', y: 42 },
{ x: 'Q4', y: 42 },
],
},
],
},
],
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}