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 randomSeries = (): any => {
var d = []
var v = 45 + Math.random() * 25
for (var i = 0; i < 45; i++) {
v += (Math.random() - 0.5) * 22
if (v < 8) v = 8
if (v > 100) v = 100
d.push([i + 1, Math.round(v)])
}
return d
};
private undoBtn: any = document.getElementById('undo');
private redoBtn: any = document.getElementById('redo');
private readout: any = document.getElementById('readout');
private syncButtons = (state: any): any => {
if (!state) state = this.chart.history.state()
this.undoBtn.disabled = !state.canUndo
this.redoBtn.disabled = !state.canRedo
this.readout.innerHTML =
'history: <b>' +
(state.index + 1) +
' / ' +
state.length +
'</b> checkpoints'
};
public chartOptions: Partial<ChartOptions> = {
series: [{ name: 'Value', data: this.randomSeries() }],
chart: {
height: 400,
type: 'line',
id: 'rewind-demo',
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { enabled: true },
// Toolbar on so zoom / pan / reset (and the measure tool) are one click away.
toolbar: { show: true, autoSelected: 'zoom' },
zoom: { enabled: true, type: 'x' },
history: { enabled: true, maxDepth: 50, coalesceMs: 250, keyboard: true },
// Right-click actions: drop a note, a dashed marker line, or seed the ruler.
contextMenu: {
enabled: true,
line: { text: 'Marker', strokeDashArray: 5, color: '#0EA5E9' },
items: ['annotate', 'xline', 'yline', 'measure'],
},
// Measure ruler + editable / draggable annotations. Both commit checkpoints,
// so Undo / Redo steps through them alongside data and zoom changes.
measure: { enabled: true },
ink: { enabled: true, snap: true },
},
colors: ['#0EA5E9'],
stroke: { width: 2.5, curve: 'smooth' },
markers: { size: 0, hover: { size: 5 } },
dataLabels: { enabled: false },
title: {
text: 'Undo / redo over data, zoom, annotations, and measurements',
align: 'left',
},
xaxis: { type: 'numeric', title: { text: 'Session' }, tickAmount: 10 },
yaxis: { decimalsInFloat: 0 },
grid: { borderColor: '#eceff5' },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.chart.addEventListener('historyChange', (c, state) => {
this.syncButtons(state && state.length != null ? state : undefined)
})
this.undoBtn.addEventListener('click', () => {
this.chart.history.undo()
})
this.redoBtn.addEventListener('click', () => {
this.chart.history.redo()
})
document.getElementById('randomize').addEventListener('click', () => {
this.chart.updateSeries([{ name: 'Value', data: this.randomSeries() }])
})
document.getElementById('reset').addEventListener('click', () => {
this.chart.history.clear()
})
setTimeout(this.syncButtons, 300)
}
ngOnDestroy() {
// no cleanup needed
}
}