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 SOURCES: any = ['Coal', 'Gas', 'Hydro', 'Nuclear', 'Wind', 'Solar', 'Other'];
private SOURCE_COLORS: any = [
'#374151', // Coal
'#94A3B8', // Gas
'#2563EB', // Hydro
'#E11D48', // Nuclear
'#0D9488', // Wind
'#F59E0B', // Solar
'#CBD5E1', // Other
];
private SCENARIOS: any = {
today: {
label: 'Today',
caption:
'A rough picture of the mix today: fossil fuels (coal + gas) still fill well over half the grid.',
values: [35, 23, 15, 9, 8, 6, 4],
},
future: {
label: '2035 (projected)',
caption:
'A cleaner projected mix: coal squares recolour to wind and solar as the renewables band grows.',
values: [18, 20, 16, 10, 18, 14, 4],
},
};
private buttons: any = document.querySelectorAll('#scenario-nav button');
private caption: any = document.getElementById('caption');
private showScenario = (key: any): any => {
this.buttons.forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-scenario') === key)
})
this.caption.textContent = this.SCENARIOS[key].caption
this.chart.updateSeries(this.SCENARIOS[key].values)
};
public chartOptions: Partial<ChartOptions> = {
series: this.SCENARIOS.today.values,
chart: {
id: 'energyWaffle',
type: 'waffle',
height: 420,
animations: {
enabled: true,
speed: 700,
},
},
labels: this.SOURCES,
colors: this.SOURCE_COLORS,
legend: {
position: 'bottom',
},
plotOptions: {
unit: {
grid: {
columns: 10,
total: 100,
},
spacing: 1.25,
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
function showScenario(key) {
this.buttons.forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-scenario') === key)
})
this.caption.textContent = this.SCENARIOS[key].caption
this.chart.updateSeries(this.SCENARIOS[key].values)
}
this.buttons.forEach(function (btn) {
btn.addEventListener('click', () => {
this.showScenario(btn.getAttribute('data-scenario'))
})
})
}
ngOnDestroy() {
// no cleanup needed
}
}