Columns in Angular
Using ApexCharts with Angular
This Columns 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 YEARS: any = ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022'];
private REGIONS: any = ['Americas', 'Europe', 'Asia Pacific'];
private UNITS: any = [
[34, 43, 66, 69, 58, 40, 78, 80],
[21, 26, 29, 32, 47, 46, 50, 96],
[19, 24, 28, 26, 38, 31, 52, 38],
];
private CHANNELS: any = {
online: [
[26, 29, 50, 48, 37, 26, 55, 40],
[14, 17, 21, 20, 23, 34, 38, 70],
[12, 18, 22, 20, 26, 21, 38, 19],
],
store: [
[8, 14, 16, 21, 21, 14, 23, 40],
[7, 9, 8, 12, 24, 12, 12, 26],
[7, 6, 6, 6, 12, 10, 14, 19],
],
};
private seriesFor = (channel: any): any => {
return this.REGIONS.map(function (name, i) {
var s = { name: name, data: this.UNITS[i] }
if (channel) s.highlightData = this.CHANNELS[channel][i]
return s
})
};
private pickChannel = (chart: any, channel: any): any => {
document.querySelectorAll('[data-channel]').forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-channel') === channel)
})
return chart.updateSeries(this.seriesFor(channel))
};
private wireChannels = (chart: any): any => {
document.querySelectorAll('[data-channel]').forEach(function (b) {
b.addEventListener('click', function () {
this.pickChannel(chart, b.getAttribute('data-channel'))
})
})
};
public chartOptions: Partial<ChartOptions> = {
series: this.seriesFor('online'),
chart: {
id: 'units',
type: 'bar',
height: 380,
toolbar: { show: false },
},
colors: ['#16a34a', '#f59e0b', '#2563eb'],
plotOptions: {
bar: { columnWidth: '78%' },
},
dataLabels: { enabled: false },
stroke: { show: true, width: 1, colors: ['transparent'] },
xaxis: { categories: this.YEARS },
yaxis: { title: { text: 'Units (thousands)' } },
highlightFilter: { fadeOpacity: 0.2 },
legend: { position: 'bottom' },
tooltip: { shared: true, intersect: false },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.wireChannels(this.chart)
function pickChannel(chart, channel) {
document.querySelectorAll('[data-channel]').forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-channel') === channel)
})
return this.chart.updateSeries(this.seriesFor(channel))
}
function wireChannels(chart) {
document.querySelectorAll('[data-channel]').forEach(function (b) {
b.addEventListener('click', () => {
this.pickChannel(chart, b.getAttribute('data-channel'))
})
})
}
}
ngOnDestroy() {
// no cleanup needed
}
}