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 DATASETS: any = {
departments: {
values: [200, 120, 90, 70],
labels: ['Engineering', 'Sales', 'Operations', 'Design'],
colors: ['#008FFB', '#00B8D9', '#36B37E', '#FFAB00'],
layout: 'grouped',
},
seniority: {
values: [60, 420],
labels: ['Leadership', 'Staff'],
colors: ['#FFAB00', '#7B8794'],
layout: 'packed',
},
};
private randomWorkforce = (): any => {
var eng = Math.floor(Math.random() * 110) + 150
var sales = Math.floor(Math.random() * 70) + 90
var ops = Math.floor(Math.random() * 60) + 60
var design = Math.floor(Math.random() * 50) + 45
return [eng, sales, ops, design]
};
private setActive = (id: any): any => {
document
.querySelectorAll('.actions button[data-dataset]')
.forEach(function (btn) {
btn.classList.toggle('active', btn.getAttribute('data-dataset') === id)
})
};
private current: any = 'departments';
public chartOptions: Partial<ChartOptions> = {
series: [200, 120, 90, 70],
chart: {
id: 'unitChart',
type: 'unit',
height: 420,
animations: {
enabled: true,
speed: 900,
},
},
labels: ['Engineering', 'Sales', 'Operations', 'Design'],
colors: ['#008FFB', '#00B8D9', '#36B37E', '#FFAB00'],
plotOptions: {
unit: {
layout: 'grouped',
shape: 'circle',
size: 'auto',
clusterLabels: {
show: true,
},
},
},
legend: {
position: 'bottom',
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
this.setActive(this.current)
document
.querySelectorAll('.actions button[data-dataset]')
.forEach(function (btn) {
btn.addEventListener('click', () => {
var id = btn.getAttribute('data-dataset')
if (id === this.current) return
this.current = id
this.setActive(this.current)
var ds = this.DATASETS[id]
this.chart.updateOptions({
colors: ds.colors,
labels: ds.labels,
plotOptions: { unit: { layout: ds.layout } },
series: ds.values,
})
})
})
document.querySelector('#shuffle')?.addEventListener('click', () => {
if (this.current !== 'departments') return
this.chart.updateSeries(this.randomWorkforce())
})
}
ngOnDestroy() {
// no cleanup needed
}
}