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 bigSeries = (seed: any, amp: any): any => {
var d = []
var v = seed
for (var i = 0; i < 40; i++) {
v += (Math.random() - 0.5) * amp
d.push({ x: i + 1, y: Math.round(v) })
}
return d
};
private viewsSel: any = document.getElementById('views');
private linkEl: any = document.getElementById('link');
private saveCount: any = 0;
private refreshList = (): any => {
var list = this.chart.perspectives.list()
this.viewsSel.innerHTML =
'<option value="">' +
(list.length ? 'Pick a saved view...' : '(none yet)') +
'</option>'
list.forEach(function (p) {
var o = document.createElement('option')
o.value = p.id
o.textContent = p.name
this.viewsSel.appendChild(o)
})
};
public chartOptions: Partial<ChartOptions> = {
series: [
{ name: 'North', data: this.bigSeries(50, 14) },
{ name: 'South', data: this.bigSeries(70, 10) },
],
chart: {
height: 380,
type: 'line',
id: 'perspectives-demo',
animations: { enabled: true },
toolbar: {
show: true,
tools: { zoom: true, pan: true, reset: true, download: false },
},
zoom: { enabled: true, type: 'x' },
ink: { enabled: true },
contextMenu: { enabled: true },
},
colors: ['#1971c2', '#e0447e'],
stroke: { curve: 'smooth', width: 2 },
dataLabels: { enabled: false },
title: { text: 'Capture, apply, and share the exact view', align: 'left' },
xaxis: { type: 'numeric', title: { text: 'Step' } },
legend: { position: 'top' },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
document.getElementById('save').addEventListener('click', () => {
this.saveCount++
this.chart.perspectives.save('View ' + this.saveCount)
this.refreshList()
})
this.viewsSel.addEventListener('change', () => {
var id = this.viewsSel.value
if (!id) return
var entry = this.chart.perspectives.list().filter(function (p) {
return p.id === id
})[0]
if (entry) this.chart.perspectives.apply(entry.token, { animate: true })
})
document.getElementById('copy').addEventListener('click', () => {
var url = this.chart.perspectives.toURL()
this.linkEl.textContent = url
if (navigator.clipboard)
navigator.clipboard.writeText(url).catch(function () {})
})
document.getElementById('resetZoom').addEventListener('click', () => {
this.chart.resetSeries(true, true)
})
}
ngOnDestroy() {
// no cleanup needed
}
}