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 DEPTS: any = [
{ name: 'Engineering', base: 610 },
{ name: 'Sales', base: 380 },
{ name: 'Support', base: 290 },
{ name: 'Marketing', base: 170 },
];
private QUARTERS: any = ['Q1', 'Q2', 'Q3', 'Q4'];
private monthlyHours = (seed: any, base: any): any => {
var rand = this.mulberry32(seed)
var out = []
for (var m = 0; m < 3; m++) {
out.push({
x: 'M' + (m + 1),
y: Math.round(base * (0.85 + rand() * 0.3)),
})
}
return out
};
private deptSeries: any = [];
public chartOptions: Partial<ChartOptions> = {
series: this.deptSeries,
chart: {
id: 'deptQuarterTrellis',
type: 'bar',
height: 560,
animations: {
enabled: false,
},
},
trellis: {
row: 'dept',
column: 'quarter',
gap: 12,
},
colors: ['#2563EB'],
plotOptions: {
bar: {
columnWidth: '55%',
borderRadius: 3,
},
},
xaxis: {
type: 'category',
},
yaxis: {
labels: {
formatter: (val) => {
return Math.round(val) + 'h'
},
},
},
dataLabels: {
enabled: false,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.DEPTS.forEach(function (d, di) {
this.QUARTERS.forEach(function (q, qi) {
if (d.name === 'Marketing' && q === 'Q1') return // formed in Q2
this.deptSeries.push({
name: 'Hours',
dept: d.name,
quarter: q,
data: this.monthlyHours(di * 17 + qi * 5 + 3, d.base),
})
})
})
}
ngOnDestroy() {
// no cleanup needed
}
}