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(1357924680);
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 sample = (n: any, mean: any, sd: any): any => {
var out = []
for (var i = 0; i < n; i++) {
out.push(Math.round(this.gauss(mean, sd) * 10) / 10)
}
return out
};
private MONTHS: any = [
['Jan', 4, 3],
['Feb', 6, 3],
['Mar', 11, 3.5],
['Apr', 15, 4],
['May', 20, 4],
['Jun', 24, 4],
['Jul', 27, 3.5],
['Aug', 26, 3.5],
['Sep', 21, 4],
['Oct', 15, 4.5],
['Nov', 9, 3.5],
['Dec', 5, 3],
].map(function (m) {
return { name: m[0], data: this.sample(110, m[1], m[2]) }
});
private kde = (values: any, bw: 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 += 0.4) {
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
};
private COLOR_RAMP: any = ['#2c7bb6', '#abd9e9', '#ffffbf', '#fdae61', '#d7191c'];
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Temperature',
data: this.MONTHS.map(function (m) {
return {
x: m.name,
y: { density: this.kde(m.data, 1.5), points: m.data },
}
}),
},
],
chart: {
type: 'violin',
height: 560,
},
title: {
text: 'Afternoon temperature through the year — dots coloured by value',
},
// muted grey violins so the colour-graded dots are the focus
colors: ['#b8c4cc'],
stroke: { width: 1, colors: ['#90a4ae'] },
fill: { opacity: 0.6 },
legend: { show: false },
yaxis: {
title: { text: 'Temperature (°C)' },
},
plotOptions: {
violin: {
points: {
show: true,
size: 5,
jitter: 0.8,
opacity: 1,
strokeColor: '#fff',
strokeWidth: 1,
// colour each observation by its value along the ramp
colorScale: { colors: this.COLOR_RAMP, min: 0, max: 32 },
},
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}