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 = (a: any): any => {
return function () {
a |= 0
a = (a + 0x6d2b79f5) | 0
var t = Math.imul(a ^ (a >>> 15), 1 | a)
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
};
private rng: any = this.mulberry32(20240607);
private gauss = (mean: any, sd: any): any => {
var u = 0,
v = 0
while (u === 0) u = this.rng()
while (v === 0) v = this.rng()
return mean + sd * Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v)
};
private REGIONS: any = [
{ name: 'Frankfurt', mean: 120, sd: 22 },
{ name: 'Mumbai', mean: 182, sd: 30 },
{ name: 'Oregon', mean: 96, sd: 18 },
{ name: 'Singapore', mean: 158, sd: 26 },
{ name: 'Virginia', mean: 82, sd: 16 },
];
private readings = (region: any): any => {
var out = []
for (var i = 0; i < 150; i++) {
out.push(Math.max(20, Math.round(this.gauss(region.mean, region.sd))))
}
return out
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Frankfurt',
data: [{ x: 'Frankfurt', y: this.readings(this.REGIONS[0]) }],
},
{
name: 'Mumbai',
data: [{ x: 'Mumbai', y: this.readings(this.REGIONS[1]) }],
},
{
name: 'Oregon',
data: [{ x: 'Oregon', y: this.readings(this.REGIONS[2]) }],
},
{
name: 'Singapore',
data: [{ x: 'Singapore', y: this.readings(this.REGIONS[3]) }],
},
{
name: 'Virginia',
data: [{ x: 'Virginia', y: this.readings(this.REGIONS[4]) }],
},
],
chart: {
type: 'scatter',
height: 460,
},
title: {
text: 'API response time by region',
align: 'center',
},
plotOptions: {
scatter: {
// Spread the readings in each band horizontally (axis units; 1 = one
// band step). Set `distributed: true` to colour each band differently.
jitter: {
enabled: true,
x: 0.35,
},
},
},
markers: {
size: 3,
strokeWidth: 0,
},
fill: {
opacity: 0.6,
},
legend: { show: false },
xaxis: {
// strip plots read better without the vertical crosshair line
crosshairs: { show: false },
},
yaxis: {
title: { text: 'Response time (ms)' },
labels: {
formatter: (v) => {
return Math.round(v)
},
},
},
tooltip: {
// anchor the tooltip to the hovered band so it stays put instead of
// chasing each individual marker
shared: true,
intersect: false,
y: {
formatter: (v) => {
return Math.round(v) + ' ms'
},
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}