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 COUNTRIES: any = [
{ name: 'United States', seed: 5, base: 48000 },
{ name: 'India', seed: 11, base: 30500 },
{ name: 'United Kingdom', seed: 19, base: 22000 },
{ name: 'Germany', seed: 23, base: 18200 },
{ name: 'Brazil', seed: 31, base: 15400 },
{ name: 'France', seed: 37, base: 13100 },
{ name: 'Canada', seed: 43, base: 12000 },
{ name: 'Australia', seed: 47, base: 9600 },
{ name: 'Japan', seed: 59, base: 8100 },
{ name: 'Netherlands', seed: 61, base: 6200 },
{ name: 'Spain', seed: 71, base: 5100 },
{ name: 'Mexico', seed: 79, base: 4300 },
{ name: 'Sweden', seed: 83, base: 3600 },
{ name: 'Poland', seed: 89, base: 3000 },
{ name: 'Singapore', seed: 97, base: 2600 },
{ name: 'South Africa', seed: 101, base: 2200 },
{ name: 'Argentina', seed: 107, base: 1800 },
{ name: 'Ireland', seed: 113, base: 1500 },
{ name: 'Thailand', seed: 127, base: 1200 },
{ name: 'New Zealand', seed: 131, base: 900 },
];
private dailySessions = (seed: any, base: any, spikeAt: any): any => {
var rand = this.mulberry32(seed)
var out = []
var ts = new Date('01 Jan 2025').getTime()
var val = base
for (var i = 0; i < 60; i++) {
val = Math.max(base * 0.5, val + (rand() - 0.5) * base * 0.12)
var v = val
// The anomaly the top-5 chart would never show: a three-day viral spike.
if (spikeAt && i >= spikeAt && i < spikeAt + 3) {
v = val + 11000 * (1 - (i - spikeAt) * 0.3)
}
out.push([ts, Math.round(v)])
ts += 86400000
}
return out
};
private trafficSeries: any = this.COUNTRIES.map(function (c) {
return {
name: 'Sessions',
country: c.name,
data: this.dailySessions(c.seed, c.base, c.name === 'New Zealand' ? 34 : 0),
}
});
public chartOptions: Partial<ChartOptions> = {
series: this.trafficSeries,
chart: {
id: 'trafficTrellis',
type: 'area',
animations: {
enabled: false,
},
},
trellis: {
by: 'country',
minPanelWidth: 240,
panelHeight: 120,
gap: 12,
},
colors: ['#0369A1'],
stroke: {
width: 2,
curve: 'straight',
},
fill: {
type: 'gradient',
gradient: {
opacityFrom: 0.35,
opacityTo: 0.05,
},
},
xaxis: {
type: 'datetime',
},
yaxis: {
labels: {
formatter: (val) => {
return Math.round(val / 1000) + 'k'
},
},
},
dataLabels: {
enabled: false,
},
tooltip: {
x: {
format: 'dd MMM',
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
}
ngOnDestroy() {
// no cleanup needed
}
}