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 gaussianViolin = (mean: any, sd: any, n: any): any => {
var density = []
for (var v = mean - 3 * sd; v <= mean + 3 * sd; v += sd / 6) {
var z = (v - mean) / sd
density.push([v, Math.exp(-0.5 * z * z)])
}
var points = []
for (var k = 0; k < n; k++) {
// Box–Muller transform on two uniform samples
var u1 = Math.random(),
u2 = Math.random()
var g = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2)
points.push(mean + g * sd)
}
return { density: density, points: points }
};
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Measurement',
data: [
{ x: 'Group A', y: this.gaussianViolin(50, 8, 400) },
{ x: 'Group B', y: this.gaussianViolin(62, 5, 600) },
{ x: 'Group C', y: this.gaussianViolin(45, 12, 250) },
{ x: 'Group D', y: this.gaussianViolin(55, 6, 500) },
{ x: 'Group E', y: this.gaussianViolin(48, 10, 350) },
{ x: 'Group F', y: this.gaussianViolin(58, 7, 450) },
{ x: 'Group G', y: this.gaussianViolin(52, 9, 300) },
],
},
],
chart: {
type: 'violin',
height: 420,
},
title: {
text: 'Distribution by group',
},
plotOptions: {
violin: {
bandwidthScale: 1,
points: {
show: false, // density curve only — no jitter overlay
},
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}