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(20240607);
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 mixture = (n: any, comps: any): any => {
var out = []
for (var i = 0; i < n; i++) {
var r = this.rng(),
acc = 0,
c = comps[0]
for (var k = 0; k < comps.length; k++) {
acc += comps[k].w
if (r <= acc) {
c = comps[k]
break
}
}
out.push(Math.max(5, Math.round(this.gauss(c.mean, c.sd) * 10) / 10))
}
return out
};
private CITIES: any = [
{
name: 'Tokyo',
data: this.mixture(260, [
{ w: 0.62, mean: 42, sd: 5 },
{ w: 0.38, mean: 58, sd: 5 },
]),
},
{
name: 'London',
data: this.mixture(260, [
{ w: 0.7, mean: 48, sd: 7 },
{ w: 0.3, mean: 74, sd: 6 },
]),
},
{
name: 'New York',
data: this.mixture(260, [{ w: 1, mean: 50, sd: 11 }]),
},
{
name: 'Berlin',
data: this.mixture(260, [
{ w: 0.8, mean: 37, sd: 5 },
{ w: 0.2, mean: 54, sd: 5 },
]),
},
];
private kde = (values: any, bw: any): any => {
var min = Math.min.apply(null, values)
var max = Math.max.apply(null, values)
var N = values.length
var profile = []
// Keep the tail short (1 bandwidth) so the violin tapers cleanly without
// a long near-zero spike that would drag the axis toward 0.
for (var x = min - bw; x <= max + bw; x += 1) {
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([x, sum / (N * bw)])
}
return profile
};
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 fiveNum = (values: any): any => {
var s = values.slice().sort(function (a, b) {
return a - b
})
return {
min: s[0],
q1: this.quantile(s, 0.25),
median: this.quantile(s, 0.5),
q3: this.quantile(s, 0.75),
max: s[s.length - 1],
}
};
private BW: any = 2.5;
private LABELS: any = this.CITIES.map(function (c) {
return c.name
});
private COLORS: any = ['#14b8a6', '#6366f1', '#f59e0b', '#ef4444'];
private stats: any = this.CITIES.map(function (c) {
return this.fiveNum(c.data)
});
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Commute',
data: this.CITIES.map(function (c) {
return {
x: c.name,
y: { density: this.kde(c.data, this.BW), points: c.data },
}
}),
},
],
chart: {
type: 'violin',
height: 560,
},
title: {
text: 'Daily commute time by city',
align: 'center',
},
colors: this.COLORS,
plotOptions: {
bar: { distributed: true }, // one colour per city
violin: {
normalize: 'group', // widths track density across cities
// dots in a darker shade of each violin's colour, with a white outline
points: { show: true, size: 3, jitter: 0.6 },
},
},
stroke: { width: 1, colors: ['#888'] },
legend: { show: false },
yaxis: {
labels: {
formatter: (v) => {
return Math.round(v) + ' min'
},
},
},
tooltip: {
shared: false,
custom: (o) => {
var st = this.stats[o.dataPointIndex]
if (!st) return ''
var row = function (label, val) {
return (
'<tr><td style="padding:2px 8px">' +
label +
':</td><td style="padding:2px 8px"><b>' +
val.toFixed(1) +
' min</b></td></tr>'
)
}
return (
'<div class="apexcharts-tooltip-box" style="padding:6px">' +
'<div style="font-weight:600;margin-bottom:2px">' +
this.LABELS[o.dataPointIndex] +
'</div><table>' +
row('Max', st.max) +
row('Q3', st.q3) +
row('Median', st.median) +
row('Q1', st.q1) +
row('Min', st.min) +
'</table></div>'
)
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}