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 mulberry32 = (seed: any): any => {
return function () {
seed |= 0
seed = (seed + 0x6d2b79f5) | 0
var t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
};
private channel = (seed: any, base: any, spread: any): any => {
var rand = this.mulberry32(seed)
var series = []
var ts = new Date('01 Jun 2025 GMT').getTime()
var val = base
for (var i = 0; i < 20; i++) {
val += (rand() - 0.5) * spread
val += (base - val) * 0.15
series.push([ts, Math.max(2, Math.round(val))])
ts += 86400000
}
return series
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Organic Search',
data: this.channel(11, 42, 14),
},
{
name: 'Direct',
data: this.channel(23, 26, 9),
},
{
name: 'Social',
data: this.channel(35, 15, 8),
},
],
chart: {
type: 'area',
height: 350,
stacked: true,
events: {
selection: (chart, e) => {
console.log(new Date(e.xaxis.min))
},
},
},
colors: ['#008FFB', '#00E396', '#FEB019'],
dataLabels: {
enabled: false,
},
stroke: {
curve: 'monotoneCubic',
},
title: {
text: 'Website Visits by Channel',
align: 'left',
},
fill: {
type: 'gradient',
gradient: {
opacityFrom: 0.6,
opacityTo: 0.8,
},
},
legend: {
position: 'top',
horizontalAlign: 'left',
},
yaxis: {
labels: {
formatter: (val) => {
return val + 'k'
},
},
},
xaxis: {
type: 'datetime',
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}