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 regions: any = ['North America', 'Europe', 'Asia Pacific', 'Latin America'];
private quarters: any = ['Q1', 'Q2', 'Q3', 'Q4'];
private monthsByQuarter: any = [
['Jan', 'Feb', 'Mar'],
['Apr', 'May', 'Jun'],
['Jul', 'Aug', 'Sep'],
['Oct', 'Nov', 'Dec'],
];
private weeks: any = ['Wk 1', 'Wk 2', 'Wk 3', 'Wk 4'];
private pseudo = (seed: any, lo: any, hi: any): any => {
var x = Math.sin(seed) * 10000
x = x - Math.floor(x)
return Math.round(lo + x * (hi - lo))
};
private clamp = (v: any, lo: any, hi: any): any => {
return Math.max(lo, Math.min(hi, v))
};
private heatPlot: any = {
heatmap: {
radius: 2,
shadeIntensity: 0.5,
colorScale: {
ranges: [
{ from: 0, to: 30, color: '#C8E6C9', name: 'Low' },
{ from: 31, to: 60, color: '#66BB6A', name: 'Medium' },
{ from: 61, to: 80, color: '#FB8C00', name: 'High' },
{ from: 81, to: 100, color: '#E53935', name: 'Peak' },
],
},
},
};
private rootValues: any = this.regions.map(function (region, ri) {
return this.quarters.map(function (q, qi) {
return this.pseudo(ri * 31 + qi * 7 + 3, 20, 95)
})
});
private heatRootSeries: any = this.regions.map(function (region, ri) {
return {
name: region,
data: this.quarters.map(function (q, qi) {
return { x: q, y: this.rootValues[ri][qi], drilldown: 'r' + ri + '-q' + qi }
}),
}
});
private heatChildren: any = [];
public chartOptions: Partial<ChartOptions> = {
series: this.heatRootSeries,
chart: {
type: 'heatmap',
height: 450,
toolbar: {
show: false,
},
},
dataLabels: {
enabled: true,
},
plotOptions: this.heatPlot,
title: {
text: 'Sales Intensity by Region and Quarter',
align: 'left',
},
subtitle: {
text: 'Click any cell to drill into its weekly breakdown (pointer cursor marks drillable cells). Use the breadcrumb to go back.',
align: 'left',
},
drilldown: {
enabled: true,
breadcrumb: {
show: true,
position: 'top-right',
rootLabel: 'All Regions',
separator: ' / ',
},
series: this.heatChildren,
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
this.regions.forEach(function (region, ri) {
this.quarters.forEach(function (q, qi) {
var base = this.rootValues[ri][qi]
var months = this.monthsByQuarter[qi]
// Draw a raw +/-15 wobble for each of the 12 weekly cells, then re-center
// the wobbles so they sum to zero. The weekly cells therefore average back
// to the region+quarter score: the drilldown preserves the headline instead
// of inventing a new total, while individual weeks still vary realistically.
var raw = []
months.forEach(function (m, mi) {
this.weeks.forEach(function (wk, wi) {
raw.push(this.pseudo(ri * 101 + qi * 53 + mi * 17 + wi * 3 + 11, -15, 15))
})
})
var mean =
raw.reduce(function (a, b) {
return a + b
}, 0) / raw.length
var k = 0
this.heatChildren.push({
id: 'r' + ri + '-q' + qi,
name: region + ' / ' + q,
series: months.map(function (m, mi) {
return {
name: m,
data: this.weeks.map(function (wk, wi) {
var y = this.clamp(Math.round(base + raw[k] - mean), 0, 100)
k++
return { x: wk, y: y }
}),
}
}),
})
})
})
}
ngOnDestroy() {
// no cleanup needed
}
}