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 REGIONS: any = [
{ name: 'North', seed: 11, base: 58 },
{ name: 'South', seed: 23, base: 42 },
{ name: 'East', seed: 35, base: 74 },
{ name: 'West', seed: 47, base: 90 },
{ name: 'Central', seed: 59, base: 35 },
{ name: 'Northeast', seed: 71, base: 62 },
];
private monthlyRevenue = (seed: any, base: any): any => {
var rand = this.mulberry32(seed)
var out = []
var ts = new Date('01 Jan 2025').getTime()
var val = base
for (var i = 0; i < 12; i++) {
val += (rand() - 0.45) * 9
out.push([ts, Math.round(val * 10) / 10])
ts += 2678400000 // ~1 month
}
return out
};
private trellisSeries: any = this.REGIONS.map(function (r) {
return {
name: 'Revenue',
region: r.name,
data: this.monthlyRevenue(r.seed, r.base),
}
});
public chartOptions: Partial<ChartOptions> = {
series: this.trellisSeries,
chart: {
id: 'revenueTrellis',
type: 'line',
height: 640,
animations: {
enabled: false,
},
},
trellis: {
by: 'region',
minPanelWidth: 260,
gap: 14,
header: {
formatter: (key) => {
return key
},
},
},
colors: ['#0EA5E9', '#94A3B8'],
stroke: {
width: [3, 2],
curve: 'straight',
dashArray: [0, 5],
},
xaxis: {
type: 'datetime',
},
yaxis: {
labels: {
formatter: (val) => {
return '$' + Math.round(val) + 'k'
},
},
},
dataLabels: {
enabled: false,
},
tooltip: {
x: {
format: 'MMM yyyy',
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.trellisSeries.push({
name: 'Target',
data: this.monthlyRevenue(1, 65).map(function (d) {
return [d[0], 65]
}),
})
}
ngOnDestroy() {
// no cleanup needed
}
}