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 onboardingMinutes = (seed: any, mean: any, spread: any, n: any): any => {
var rand = this.mulberry32(seed)
var out = []
for (var i = 0; i < n; i++) {
var v = mean + (rand() + rand() + rand() - 1.5) * spread
// A slow tail: some users always wander off mid-setup.
if (rand() < 0.06) v += rand() * 25
out.push(Math.max(2, Math.round(v * 10) / 10))
}
return out
};
private COHORTS: any = [
{ name: 'Jan', seed: 11, mean: 34, spread: 14 },
{ name: 'Feb', seed: 23, mean: 32, spread: 13 },
{ name: 'Mar', seed: 37, mean: 30, spread: 13 },
{ name: 'Apr', seed: 41, mean: 27, spread: 12 },
{ name: 'May', seed: 53, mean: 25, spread: 11 },
{ name: 'Jun', seed: 67, mean: 22, spread: 10 },
{ name: 'Jul', seed: 79, mean: 20, spread: 9 },
{ name: 'Aug', seed: 97, mean: 18, spread: 8 },
];
private cohortSeries: any = this.COHORTS.map(function (c) {
return {
name: 'Onboarding time',
cohort: c.name + ' cohort',
data: this.onboardingMinutes(c.seed, c.mean, c.spread, 240),
}
});
public chartOptions: Partial<ChartOptions> = {
series: this.cohortSeries,
chart: {
id: 'cohortTrellis',
type: 'histogram',
height: 560,
animations: {
enabled: false,
},
},
trellis: {
by: 'cohort',
columns: 4,
gap: 12,
},
colors: ['#0E7490'],
plotOptions: {
histogram: {
bins: 'auto',
},
},
xaxis: {
tickAmount: 5,
labels: {
rotate: 0,
formatter: (val) => {
return Math.round(Number(val)) + 'm'
},
},
},
dataLabels: {
enabled: false,
},
stroke: {
width: 1,
colors: ['#fff'],
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
}
ngOnDestroy() {
// no cleanup needed
}
}