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 countsForAge = (minAge: any): any => {
var frac = (90 - minAge) / 45 // 1 at age 45, 0 at age 90
var total = Math.round(180 + 760 * frac)
var femaleShare = 0.2 + 0.16 * frac
var female = Math.round(total * femaleShare)
return { female: female, male: total - female, total: total }
};
private readoutText = (minAge: any): any => {
var c = this.countsForAge(minAge)
var pct = ((c.female / c.total) * 100).toFixed(1)
return (
c.total.toLocaleString() +
' people aged ' +
minAge +
'+, ' +
c.female.toLocaleString() +
' female (' +
pct +
'%)'
)
};
private ageInput: any = document.querySelector('#age');
private readout: any = document.querySelector('#readout');
public chartOptions: Partial<ChartOptions> = {
series: [338, 602],
chart: {
id: 'popChart',
type: 'unit',
height: 460,
animations: {
enabled: true,
speed: 700,
},
},
labels: ['Female', 'Male'],
colors: ['#EE5A8F', '#1AA5A0'],
legend: {
position: 'bottom',
},
plotOptions: {
unit: {
layout: 'packed',
sortByGroup: true,
spacing: 1.1,
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
this.ageInput.addEventListener('input', () => {
var minAge = parseInt(this.ageInput.value, 10)
this.readout.textContent = this.readoutText(minAge)
var c = this.countsForAge(minAge)
this.chart.updateSeries([c.female, c.male])
})
}
ngOnDestroy() {
// no cleanup needed
}
}