Treemap in Angular
Using ApexCharts with Angular
This Treemap example wires ApexCharts into Angular through the ng-apexcharts component.
Install it with npm install ng-apexcharts apexcharts, then mount the chart with <apx-chart [series]="series" [chart]="chart"></apx-chart>.
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 product = (name: any, total: any, north: any, south: any, west: any): any => {
return {
x: name,
y: total,
region: { north: north, south: south, west: west },
}
};
private DEPARTMENTS: any = [
{
name: 'Apparel',
data: [
{
x: 'Outerwear',
children: [
this.product('Jackets', 96, 52, 18, 26),
this.product('Coats', 64, 41, 9, 14),
],
},
{
x: 'Basics',
children: [
this.product('Tees', 58, 14, 25, 19),
this.product('Socks', 22, 6, 9, 7),
this.product('Hoodies', 40, 21, 8, 11),
],
},
],
},
{
name: 'Electronics',
data: [
{
x: 'Audio',
children: [
this.product('Headphones', 88, 24, 30, 34),
this.product('Speakers', 46, 10, 19, 17),
],
},
{
x: 'Wearables',
children: [
this.product('Watches', 72, 15, 23, 34),
this.product('Trackers', 30, 6, 13, 11),
],
},
],
},
{
name: 'Home',
data: [
{
x: 'Kitchen',
children: [
this.product('Cookware', 54, 13, 27, 14),
this.product('Knives', 26, 7, 11, 8),
],
},
{
x: 'Living',
children: [
this.product('Lamps', 36, 9, 16, 11),
this.product('Rugs', 48, 22, 15, 11),
],
},
],
},
];
private money = (v: any): any => {
return '$' + Math.round(v) + 'k'
};
private pickRegion = (region: any): any => {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-region') === region)
})
var chart = ApexCharts.getChartByID('revenue')
// A pick is an ordinary update; clear() takes every part off again.
if (!region) return chart.highlightFilter.clear()
chart.highlightFilter.set(function (o) {
return o.datum.region[region]
})
};
private wireRegions = (): any => {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.addEventListener('click', function () {
this.pickRegion(b.getAttribute('data-region'))
})
})
};
public chartOptions: Partial<ChartOptions> = {
series: this.DEPARTMENTS.map(function (d) {
return {
name: d.name,
data: d.data.map(function (c) {
return {
x: c.x,
children: c.children.map(function (p) {
return Object.assign({}, p, { highlight: p.region.north })
}),
}
}),
}
}),
chart: {
id: 'revenue',
type: 'treemap',
height: 460,
toolbar: { show: false },
},
colors: ['#0284c7', '#0f766e', '#d97706'],
legend: { show: false },
stroke: { width: 2, colors: ['#fff'] },
dataLabels: {
enabled: true,
style: { fontSize: '12px', fontWeight: 600 },
},
title: { text: 'Revenue by product, $k', style: { fontSize: '13px' } },
tooltip: {
y: { formatter: this.money },
},
plotOptions: {
treemap: {
borderRadius: 3,
zoom: { enabled: true },
parents: {
padding: 3,
header: {
// A header states the region's part of its group against the group.
formatter: (name, o) => {
var h = o.node.highlight
return h && h.value != null
? name + ' ' + this.money(h.value) + ' / ' + this.money(h.total)
: name + ' ' + this.money(o.value)
},
},
},
},
},
highlightFilter: { fadeOpacity: 0.25 },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.wireRegions()
function wireRegions() {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.addEventListener('click', () => {
this.pickRegion(b.getAttribute('data-region'))
})
})
}
}
ngOnDestroy() {
// no cleanup needed
}
}