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(98761234);
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 quantile = (sorted: any, q: any): any => {
var pos = (sorted.length - 1) * q
var b = Math.floor(pos)
var r = pos - b
return sorted[b + 1] !== undefined
? sorted[b] + r * (sorted[b + 1] - sorted[b])
: sorted[b]
};
private buildBox = (name: any, n: any, mean: any, sd: any): any => {
var data = []
for (var i = 0; i < n; i++) data.push(Math.round(this.gauss(mean, sd) * 10) / 10)
var s = data.slice().sort(function (a, b) {
return a - b
})
return {
x: name,
y: [
s[0],
this.quantile(s, 0.25),
this.quantile(s, 0.5),
this.quantile(s, 0.75),
s[s.length - 1],
],
points: data,
}
};
private REGIONS: any = [
this.buildBox('North', 45, 68, 8),
this.buildBox('South', 45, 74, 6),
this.buildBox('East', 45, 61, 11),
this.buildBox('West', 45, 70, 7),
];
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Response time',
type: 'boxPlot',
data: this.REGIONS,
},
],
chart: {
type: 'boxPlot',
height: 460,
},
title: {
text: 'Response time distribution by region',
},
plotOptions: {
bar: {
horizontal: true,
barHeight: '50%',
},
boxPlot: {
colors: { upper: '#5b8def', lower: '#a3c0f9' },
// overlay each raw observation as a jittered dot
points: { show: true, size: 3, jitter: 0.6 },
},
},
stroke: { colors: ['#37474f'] },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}