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 TREE: any = [
{
data: [
{
x: 'Engineering',
y: 88,
color: '#008FFB',
children: [
{ x: 'Platform', y: 34, color: '#008FFB' },
{ x: 'Product', y: 30, color: '#29A1FC' },
{ x: 'Data', y: 24, color: '#52B3FC' },
],
},
{
x: 'Design',
y: 34,
color: '#00E396',
children: [
{ x: 'Research', y: 12, color: '#00E396' },
{ x: 'Brand', y: 10, color: '#29E7A7' },
{ x: 'Systems', y: 12, color: '#52ECB8' },
],
},
{
x: 'Go to market',
y: 52,
color: '#FEB019',
children: [
{ x: 'Sales', y: 26, color: '#FEB019' },
{ x: 'Marketing', y: 15, color: '#FEBD3E' },
{ x: 'Support', y: 11, color: '#FEC963' },
],
},
],
},
];
private LEAVES: any = [
{
data: this.TREE[0].data.reduce(function (acc, parent) {
return acc.concat(
parent.children.map(function (c) {
return { x: c.x, y: c.y }
}),
)
}, []),
},
];
private LEAF_COLORS: any = this.TREE[0].data.reduce(function (acc, parent) {
return acc.concat(
parent.children.map(function (c) {
return c.color
}),
)
}, []);
private wirePartitionToggle = (chart: any): any => {
var buttons = [].slice.call(document.querySelectorAll('[data-view]'))
function apply(view) {
buttons.forEach(function (b) {
b.className = b.getAttribute('data-view') === view ? 'on' : ''
})
chart.updateOptions({
chart: { type: view === 'sunburst' ? 'sunburst' : 'treemap' },
series: view === 'sunburst' ? this.TREE : this.LEAVES,
})
}
buttons.forEach(function (b) {
b.addEventListener('click', function () {
apply(b.getAttribute('data-view'))
})
})
};
public chartOptions: Partial<ChartOptions> = {
series: this.LEAVES,
chart: {
id: 'partition',
type: 'treemap',
width: 700,
height: 460,
toolbar: {
show: false,
},
animations: {
enabled: true,
chartTypeMorph: {
enabled: true,
speed: 900,
},
},
},
colors: this.LEAF_COLORS,
legend: {
show: false,
},
dataLabels: {
enabled: true,
style: {
fontSize: '12px',
},
},
plotOptions: {
treemap: {
// One palette entry per tile, taken straight from the data above, so a
// tile and the arc it becomes are the same colour. Shading is off because
// it would recolour tiles by value and break that pairing.
distributed: true,
enableShades: false,
},
sunburst: {
innerSize: '22%',
borderRadius: 2,
spacing: 1,
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.wirePartitionToggle(this.chart)
function wirePartitionToggle(chart) {
var buttons = [].slice.call(document.querySelectorAll('[data-view]'))
function apply(view) {
buttons.forEach(function (b) {
b.className = b.getAttribute('data-view') === view ? 'on' : ''
})
this.chart.updateOptions({
chart: { type: view === 'sunburst' ? 'sunburst' : 'treemap' },
series: view === 'sunburst' ? this.TREE : this.LEAVES,
})
}
buttons.forEach(function (b) {
b.addEventListener('click', () => {
apply(b.getAttribute('data-view'))
})
})
}
}
ngOnDestroy() {
// no cleanup needed
}
}