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;
public chartOptions: Partial<ChartOptions> = {
series: [
// George Washington
{
name: 'George Washington',
data: [
{
x: 'President',
y: [new Date(1789, 3, 30).getTime(), new Date(1797, 2, 4).getTime()],
},
],
},
// John Adams
{
name: 'John Adams',
data: [
{
x: 'President',
y: [new Date(1797, 2, 4).getTime(), new Date(1801, 2, 4).getTime()],
},
{
x: 'Vice President',
y: [new Date(1789, 3, 21).getTime(), new Date(1797, 2, 4).getTime()],
},
],
},
// Thomas Jefferson
{
name: 'Thomas Jefferson',
data: [
{
x: 'President',
y: [new Date(1801, 2, 4).getTime(), new Date(1809, 2, 4).getTime()],
},
{
x: 'Vice President',
y: [new Date(1797, 2, 4).getTime(), new Date(1801, 2, 4).getTime()],
},
{
x: 'Secretary of State',
y: [
new Date(1790, 2, 22).getTime(),
new Date(1793, 11, 31).getTime(),
],
},
],
},
// Aaron Burr
{
name: 'Aaron Burr',
data: [
{
x: 'Vice President',
y: [new Date(1801, 2, 4).getTime(), new Date(1805, 2, 4).getTime()],
},
],
},
// George Clinton
{
name: 'George Clinton',
data: [
{
x: 'Vice President',
y: [new Date(1805, 2, 4).getTime(), new Date(1812, 3, 20).getTime()],
},
],
},
// John Jay
{
name: 'John Jay',
data: [
{
x: 'Secretary of State',
y: [new Date(1789, 8, 25).getTime(), new Date(1790, 2, 22).getTime()],
},
],
},
// Edmund Randolph
{
name: 'Edmund Randolph',
data: [
{
x: 'Secretary of State',
y: [new Date(1794, 0, 2).getTime(), new Date(1795, 7, 20).getTime()],
},
],
},
// Timothy Pickering
{
name: 'Timothy Pickering',
data: [
{
x: 'Secretary of State',
y: [new Date(1795, 7, 20).getTime(), new Date(1800, 4, 12).getTime()],
},
],
},
// Charles Lee
{
name: 'Charles Lee',
data: [
{
x: 'Secretary of State',
y: [new Date(1800, 4, 13).getTime(), new Date(1800, 5, 5).getTime()],
},
],
},
// John Marshall
{
name: 'John Marshall',
data: [
{
x: 'Secretary of State',
y: [new Date(1800, 5, 13).getTime(), new Date(1801, 2, 4).getTime()],
},
],
},
// Levi Lincoln
{
name: 'Levi Lincoln',
data: [
{
x: 'Secretary of State',
y: [new Date(1801, 2, 5).getTime(), new Date(1801, 4, 1).getTime()],
},
],
},
// James Madison
{
name: 'James Madison',
data: [
{
x: 'Secretary of State',
y: [new Date(1801, 4, 2).getTime(), new Date(1809, 2, 3).getTime()],
},
],
},
],
chart: {
height: 350,
type: 'rangeBar',
},
plotOptions: {
bar: {
horizontal: true,
barHeight: '50%',
rangeBarGroupRows: true,
},
},
colors: [
'#008FFB',
'#00E396',
'#FEB019',
'#FF4560',
'#775DD0',
'#3F51B5',
'#546E7A',
'#D4526E',
'#8D5B4C',
'#F86624',
'#D7263D',
'#1B998B',
'#2E294E',
'#F46036',
'#E2C044',
],
fill: {
type: 'solid',
},
xaxis: {
type: 'datetime',
},
legend: {
position: 'right',
},
tooltip: {
custom: (opts) => {
const fromYear = new Date(opts.y1).getFullYear()
const toYear = new Date(opts.y2).getFullYear()
const w = opts.ctx.w
let ylabel = ''
if (
w.config.series[opts.seriesIndex].data &&
w.config.series[opts.seriesIndex].data[opts.dataPointIndex]
) {
ylabel = w.config.series[opts.seriesIndex].data[opts.dataPointIndex].x
}
let seriesName = w.config.series[opts.seriesIndex].name
? w.config.series[opts.seriesIndex].name
: ''
const color = w.globals.colors[opts.seriesIndex]
return (
'<div class="apexcharts-tooltip-rangebar">' +
'<div> <span class="series-name" style="color: ' +
color +
'">' +
(seriesName ? seriesName : '') +
'</span></div>' +
'<div> <span class="category">' +
ylabel +
' </span> <span class="value start-value">' +
fromYear +
'</span> <span class="separator">-</span> <span class="value end-value">' +
toYear +
'</span></div>' +
'</div>'
)
},
},
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
}
ngOnDestroy() {
// no cleanup needed
}
}