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 T0: any = new Date('01 Jan 2025').getTime();
private heartRate = (seed: any, base: any, tachyFrom: any): any => {
var rand = this.mulberry32(seed)
var out = []
var ts = this.T0
var val = base
for (var i = 0; i < 48; i++) {
val += (rand() - 0.5) * 6
var v = val
// P-07 trends tachycardic until the 30h medication event pulls it back.
if (tachyFrom && i >= tachyFrom && i < 30) v = val + 28
if (tachyFrom && i >= 30) v = val + Math.max(0, 28 - (i - 30) * 3)
out.push([ts, Math.round(v)])
ts += 3600000 // hourly
}
return out
};
private patientSeries: any = [];
public chartOptions: Partial<ChartOptions> = {
series: this.patientSeries,
chart: {
id: 'vitalsTrellis',
type: 'line',
animations: {
enabled: false,
},
},
trellis: {
by: 'patient',
minPanelWidth: 240,
panelHeight: 130,
gap: 12,
tooltip: 'grid',
},
colors: ['#0F766E'],
stroke: {
width: 2,
curve: 'straight',
},
annotations: {
yaxis: [
{
y: 60,
y2: 100,
fillColor: '#22C55E',
opacity: 0.1,
},
],
xaxis: [
{
x: new Date('01 Jan 2025').getTime() + 30 * 3600000,
scope: 'P-07',
borderColor: '#D97706',
strokeDashArray: 4,
label: {
text: 'medication',
orientation: 'horizontal',
style: {
color: '#92400E',
background: '#FEF3C7',
fontSize: '10px',
},
},
},
],
},
xaxis: {
type: 'datetime',
},
yaxis: {
labels: {
formatter: (val) => {
return Math.round(val) + ' bpm'
},
},
},
dataLabels: {
enabled: false,
},
tooltip: {
x: {
format: 'dd MMM HH:mm',
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
for (var p = 1; p <= 12; p++) {
var id = 'P-' + (p < 10 ? '0' + p : p)
this.patientSeries.push({
name: 'Heart rate',
patient: id,
data: this.heartRate(p * 7, 68 + (p % 5) * 4, p === 7 ? 12 : 0),
})
}
}
ngOnDestroy() {
// no cleanup needed
}
}