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(24681012);
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 bimodal = (n: any, a: any, b: any): any => {
var out = []
for (var i = 0; i < n; i++) {
var c = this.rng() < a.w ? a : b
out.push(Math.max(0, Math.round(this.gauss(c.mean, c.sd) * 10) / 10))
}
return out
};
private CHANNELS: any = [
{
name: 'Direct',
data: this.bimodal(320, { w: 0.4, mean: 2, sd: 1 }, { mean: 22, sd: 5 }),
},
{
name: 'Search',
data: this.bimodal(320, { w: 0.55, mean: 2, sd: 1 }, { mean: 18, sd: 5 }),
},
{
name: 'Social',
data: this.bimodal(320, { w: 0.7, mean: 2, sd: 1 }, { mean: 14, sd: 4 }),
},
{
name: 'Email',
data: this.bimodal(320, { w: 0.3, mean: 3, sd: 1.5 }, { mean: 26, sd: 6 }),
},
];
private kde = (values: any, bw: any, step: any): any => {
var min = Math.min.apply(null, values) - bw
var max = Math.max.apply(null, values) + bw
var N = values.length
var profile = []
for (var x = min; x <= max; x += step) {
var sum = 0
for (var k = 0; k < N; k++) {
var d = (x - values[k]) / bw
sum += Math.exp(-0.5 * d * d)
}
profile.push([Math.round(x * 100) / 100, sum / (N * bw)])
}
return profile
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Sessions',
data: this.CHANNELS.map(function (c) {
return {
x: c.name,
y: { density: this.kde(c.data, 2.5, 1.5), points: c.data },
}
}),
},
],
chart: {
type: 'violin',
height: 480,
},
title: {
text: 'Session duration by channel',
},
plotOptions: {
bar: {
horizontal: true,
distributed: true,
},
violin: {
normalize: 'group',
points: { show: false }, // density curves only
},
},
colors: ['#0ea5e9', '#8b5cf6', '#f97316', '#10b981'],
stroke: { width: 1, colors: ['#888'] },
legend: { show: false },
xaxis: {
title: { text: 'Session duration (minutes)' },
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}