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 DEVICES: any = [
{ name: 'Desktop', center: 1.4, spread: 0.3, slowTail: 1.4 },
{ name: 'Laptop', center: 1.9, spread: 0.38, slowTail: 1.8 },
{ name: 'Tablet', center: 2.8, spread: 0.55, slowTail: 2.4 },
{ name: 'Phone (5G)', center: 3.2, spread: 0.62, slowTail: 3.0 },
{ name: 'Phone (3G)', center: 6.1, spread: 1.3, slowTail: 4.5 },
];
private makeSample = (cfg: any, index: any): any => {
var seed = 8191 * (index + 3)
function rand() {
seed = (seed * 16807) % 2147483647
return (seed - 1) / 2147483646
}
var out = []
for (var i = 0; i < 140; i++) {
// Box-Muller, then a few genuinely slow loads: the tail is the point of
// the chart, and it is what the two whisker rules disagree about.
var u1 = Math.max(rand(), 1e-9)
var z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * rand())
var v = cfg.center + z * cfg.spread
if (i % 34 === 0) v = cfg.center + cfg.slowTail * (0.6 + rand())
out.push(Math.round(Math.max(0.2, v) * 100) / 100)
}
return out
};
private RAW: any = this.DEVICES.map(function (cfg, i) {
return { x: cfg.name, points: this.makeSample(cfg, i) }
});
private wireWhiskerControls = (chart: any): any => {
var buttons = [].slice.call(document.querySelectorAll('[data-whiskers]'))
var note = document.querySelector('#note')
function apply(mode) {
buttons.forEach(function (b) {
b.className = b.getAttribute('data-whiskers') === mode ? 'on' : ''
})
chart.updateOptions({
plotOptions: { boxPlot: { whiskers: mode } },
})
if (note) {
note.textContent =
mode === 'tukey'
? 'Whiskers stop at 1.5 x IQR. The dots past the whisker are the outliers.'
: 'Whiskers reach the slowest and fastest load. Nothing is hidden.'
}
}
buttons.forEach(function (b) {
b.addEventListener('click', function () {
apply(b.getAttribute('data-whiskers'))
})
})
apply('minmax')
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Load time',
type: 'boxPlot',
data: this.RAW,
},
],
chart: {
id: 'loadTimes',
type: 'boxPlot',
// Fixed width so the layout cannot shift with the page.
width: 700,
height: 440,
toolbar: {
show: false,
},
},
plotOptions: {
boxPlot: {
colors: {
upper: '#5A87FF',
lower: '#B7BEFF',
},
// Every observation, drawn over its own box. With the 1.5 x IQR rule the
// dots beyond the whisker are exactly the outliers that rule excludes.
points: {
show: true,
size: 2.5,
jitter: 0.45,
opacity: 0.55,
},
},
},
legend: {
show: false,
},
yaxis: {
title: {
text: 'Page load (seconds)',
},
min: 0,
},
xaxis: {
title: {
text: 'Device',
},
},
tooltip: {
shared: false,
intersect: true,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.wireWhiskerControls(this.chart)
function wireWhiskerControls(chart) {
var buttons = [].slice.call(document.querySelectorAll('[data-whiskers]'))
var note = document.querySelector('#note')?
function apply(mode) {
buttons.forEach(function (b) {
b.className = b.getAttribute('data-whiskers') === mode ? 'on' : ''
})
this.chart.updateOptions({
plotOptions: { boxPlot: { whiskers: mode } },
})
if (note) {
note.textContent =
mode === 'tukey'
? 'Whiskers stop at 1.5 x IQR. The dots past the whisker are the outliers.'
: 'Whiskers reach the slowest and fastest load. Nothing is hidden.'
}
}
buttons.forEach(function (b) {
b.addEventListener('click', () => {
apply(b.getAttribute('data-whiskers'))
})
})
apply('minmax')
}
}
ngOnDestroy() {
// no cleanup needed
}
}