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 TICKINTERVAL: any = 1000;
private XAXISRANGE: any = 29 * this.TICKINTERVAL;
private lastX: any = new Date().getTime() - this.XAXISRANGE;
private lastY: any = 50;
private data: any = [];
private nextValue = (): any => {
this.lastY = Math.max(5, Math.min(95, this.lastY + (Math.random() - 0.5) * 20))
return Math.round(this.lastY)
};
private intervalRuns: any = 0;
private interval: any;
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Signal',
data: this.data.slice(),
},
],
chart: {
id: 'realtime-streaming',
height: 350,
type: 'line',
streaming: {
enabled: true,
},
animations: {
dynamicAnimation: {
speed: 1000,
},
},
toolbar: {
show: false,
},
zoom: {
enabled: false,
},
},
dataLabels: {
enabled: false,
},
stroke: {
curve: 'smooth',
},
title: {
text: 'Streaming (bounded memory + linear scroll)',
align: 'left',
},
markers: {
size: 0,
},
xaxis: {
type: 'datetime',
range: this.XAXISRANGE,
},
yaxis: {
min: 0,
max: 100,
},
legend: {
show: false,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
while (this.data.length < 30) {
this.data.push({ x: this.lastX, y: this.nextValue() })
this.lastX += this.TICKINTERVAL
}
this.interval = window.setInterval(() => {
this.intervalRuns++
this.lastX += this.TICKINTERVAL
this.chart.appendData([
{
data: [{ x: this.lastX, y: this.nextValue() }],
},
])
if (this.intervalRuns === 2 && (window as any).isATest === true) {
clearInterval(this.interval)
}
}, this.TICKINTERVAL)
}
ngOnDestroy() {
if (this.interval) clearInterval(this.interval);
}
}