Through a Forecast in Angular
Using ApexCharts with Angular
This Through a Forecast 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>.
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 MONTHS: any = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'];
private PLANS: any = ['Team', 'Personal'];
private SUBSCRIPTIONS: any = [
[120, 132, 128, 145, 150, 162, 170, 178, 186],
[80, 84, 90, 88, 96, 101, 104, 110, 114],
];
private REGIONS: any = {
europe: [
[52, 58, 55, 63, 66, 71, 74, 78, 81],
[30, 33, 35, 34, 38, 40, 41, 43, 45],
],
americas: [
[48, 51, 50, 57, 58, 63, 66, 69, 72],
[36, 37, 40, 39, 42, 44, 46, 49, 50],
],
};
private seriesFor = (region: any): any => {
return this.PLANS.map(function (name, i) {
var s = { name: name, data: this.SUBSCRIPTIONS[i] }
if (region) s.highlightData = this.REGIONS[region][i]
return s
})
};
private pickRegion = (region: any): any => {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-region') === region)
})
return Promise.all(
['plans-stacked', 'plans-line'].map(function (id) {
return ApexCharts.getChartByID(id).updateSeries(this.seriesFor(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.seriesFor('europe'),
chart: {
id: 'plans-stacked',
type: 'bar',
height: 320,
stacked: true,
toolbar: { show: false },
},
colors: ['#0f766e', '#d97706'],
plotOptions: {
bar: {
columnWidth: '55%',
borderRadius: 4,
borderRadiusApplication: 'end',
},
},
forecastDataPoints: {
count: 3,
fillOpacity: 0.5,
dashArray: 4,
strokeWidth: 1,
},
dataLabels: { enabled: false },
xaxis: { categories: this.MONTHS },
yaxis: { title: { text: 'New subscriptions' } },
legend: { position: 'bottom' },
tooltip: { shared: true, intersect: false },
};
public chartOptions2: Partial<ChartOptions> = {
series: this.seriesFor('europe'),
chart: {
id: 'plans-line',
type: 'line',
height: 320,
toolbar: { show: false },
},
colors: ['#0f766e', '#d97706'],
stroke: { curve: 'stepline', width: 3 },
forecastDataPoints: { count: 3, dashArray: 6 },
dataLabels: { enabled: false },
xaxis: { categories: this.MONTHS },
yaxis: { min: 0, title: { text: 'New subscriptions' } },
legend: { position: 'bottom' },
tooltip: { shared: true, intersect: false },
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
this.wireRegions()
function pickRegion(region) {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.classList.toggle('active', b.getAttribute('data-region') === region)
})
return Promise.all(
['plans-stacked', 'plans-line'].map(function (id) {
return ApexCharts.getChartByID(id).updateSeries(this.seriesFor(region))
}),
)
}
function wireRegions() {
document.querySelectorAll('[data-region]').forEach(function (b) {
b.addEventListener('click', () => {
this.pickRegion(b.getAttribute('data-region'))
})
})
}
}
ngOnDestroy() {
// no cleanup needed
}
}