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;
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Compact',
data: [
[1.0, 52],
[1.2, 49],
[1.3, 47],
[1.4, 50],
[1.5, 45],
[1.6, 44],
[1.6, 48],
[1.8, 42],
[1.4, 46],
[1.2, 51],
[1.0, 50],
[1.7, 43],
],
},
{
name: 'Sedan',
data: [
[2.0, 38],
[2.2, 35],
[2.4, 34],
[2.5, 32],
[2.6, 33],
[2.8, 29],
[3.0, 27],
[2.3, 36],
[2.1, 37],
[2.7, 30],
[2.9, 28],
[2.4, 31],
],
},
{
name: 'SUV',
data: [
[3.2, 26],
[3.5, 24],
[3.8, 22],
[4.0, 21],
[4.2, 20],
[4.5, 18],
[4.8, 17],
[5.0, 15],
[3.6, 23],
[4.1, 19],
[3.4, 25],
[4.6, 16],
],
},
],
chart: {
height: 350,
type: 'scatter',
zoom: {
enabled: true,
type: 'xy',
},
},
title: {
text: 'Fuel Efficiency vs Engine Size',
},
xaxis: {
tickAmount: 10,
title: {
text: 'Engine Size (L)',
},
labels: {
formatter: (val) => {
return parseFloat(val).toFixed(1)
},
},
},
yaxis: {
tickAmount: 7,
title: {
text: 'Fuel Economy (mpg)',
},
},
tooltip: {
shared: false,
intersect: true,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}