Reactive, responsive, beautiful charts for Angular based on Chart.js
npx @tessl/cli install tessl/npm-ng2-charts@8.0.0ng2-charts is a reactive, responsive Angular library that provides beautiful chart components based on Chart.js. It offers a single directive that supports 8 different chart types (line, bar, radar, pie, polar area, doughnut, bubble, and scatter) with dynamic theming capabilities, event handling, and flexible data binding options.
ng add ng2-charts or npm install ng2-chartsimport { BaseChartDirective, ThemeService, provideCharts, withDefaultRegisterables, NgChartsConfiguration, NG_CHARTS_CONFIGURATION } from "ng2-charts";
import { ChartConfiguration, ChartData, ChartOptions, ChartType, ChartEvent, Plugin, UpdateMode } from "chart.js";For CommonJS (if supported):
const { BaseChartDirective, ThemeService, provideCharts, withDefaultRegisterables } = require("ng2-charts");
const { ChartConfiguration, ChartData, ChartOptions } = require("chart.js");import { Component } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts';
import { ChartData, ChartOptions, ChartType } from 'chart.js';
@Component({
selector: 'app-chart',
template: `
<canvas
baseChart
[data]="chartData"
[options]="chartOptions"
[type]="chartType"
(chartClick)="onChartClick($event)"
(chartHover)="onChartHover($event)">
</canvas>
`,
standalone: true,
imports: [BaseChartDirective]
})
export class ChartComponent {
chartType: ChartType = 'bar';
chartData: ChartData = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 205, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
]
}]
};
chartOptions: ChartOptions = {
responsive: true,
plugins: {
legend: {
display: true
}
}
};
onChartClick(event: any) {
console.log('Chart clicked:', event);
}
onChartHover(event: any) {
console.log('Chart hovered:', event);
}
}ng2-charts is built around several key components:
provideCharts and withDefaultRegisterablesThe main BaseChartDirective provides the core charting functionality with support for 8 chart types, reactive data binding, and Angular lifecycle integration.
@Directive({
selector: 'canvas[baseChart]',
exportAs: 'base-chart',
standalone: true
})
export class BaseChartDirective<TType extends ChartType = ChartType, TData = DefaultDataPoint<TType>, TLabel = unknown> implements OnDestroy, OnChanges {
@Input() type: ChartConfiguration<TType, TData, TLabel>['type'];
@Input() legend?: boolean;
@Input() data?: ChartConfiguration<TType, TData, TLabel>['data'];
@Input() options: ChartConfiguration<TType, TData, TLabel>['options'];
@Input() plugins: Plugin<TType>[];
@Input() labels?: ChartConfiguration<TType, TData, TLabel>['data']['labels'];
@Input() datasets?: ChartConfiguration<TType, TData, TLabel>['data']['datasets'];
@Output() chartClick: EventEmitter<{event?: ChartEvent; active?: object[]}>;
@Output() chartHover: EventEmitter<{event: ChartEvent; active: object[]}>;
ctx: string;
chart?: Chart<TType, TData, TLabel>;
constructor(element: ElementRef, zone: NgZone, themeService: ThemeService, config?: NgChartsConfiguration);
ngOnChanges(changes: SimpleChanges): void;
ngOnDestroy(): void;
render(): Chart<TType, TData, TLabel>;
update(mode?: UpdateMode): void;
hideDataset(index: number, hidden: boolean): void;
isDatasetHidden(index: number): boolean | undefined;
toBase64Image(): string | undefined;
}Provider-based configuration system for registering Chart.js components and setting default options. Essential for proper Chart.js integration and bundle optimization.
function provideCharts(...configurations: readonly NgChartsConfiguration[]): Provider;
function withDefaultRegisterables(...registerables: ChartComponentLike[]): NgChartsConfiguration;
interface NgChartsConfiguration {
registerables?: readonly ChartComponentLike[];
defaults?: DeepPartial<Defaults>;
}Theme service for runtime color scheme changes and dynamic chart styling. Allows applications to update chart appearance based on user preferences or application state.
@Injectable({
providedIn: 'root'
})
export class ThemeService {
colorschemesOptions: BehaviorSubject<ChartOptions | undefined>;
setColorschemesOptions(options: ChartConfiguration['options']): void;
getColorschemesOptions(): ChartConfiguration['options'];
}These types are imported from Chart.js:
type ChartType = 'line' | 'bar' | 'radar' | 'pie' | 'polarArea' | 'doughnut' | 'bubble' | 'scatter';
type UpdateMode = 'resize' | 'reset' | 'none' | 'hide' | 'show' | 'normal' | 'active';
type DefaultDataPoint<TType extends ChartType> = TType extends 'line' ? number | Point : TType extends 'bar' ? number : TType extends 'bubble' ? BubbleDataPoint : number;
interface ChartConfiguration<TType extends ChartType = ChartType, TData = DefaultDataPoint<TType>, TLabel = unknown> {
type: TType;
data: ChartData<TType, TData, TLabel>;
options?: ChartOptions<TType>;
plugins?: Plugin<TType>[];
}
interface ChartData<TType extends ChartType = ChartType, TData = DefaultDataPoint<TType>, TLabel = unknown> {
labels?: TLabel[];
datasets: ChartDataset<TType, TData>[];
}
interface Plugin<TType extends ChartType = ChartType> {
id: string;
[key: string]: any;
}
interface ChartComponentLike {
id: string;
[key: string]: any;
}
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};
interface Defaults {
[key: string]: any;
}interface ChartEvent {
type: string;
native: Event | null;
x: number | null;
y: number | null;
}interface NgChartsConfiguration {
registerables?: readonly ChartComponentLike[];
defaults?: DeepPartial<Defaults>;
}
const NG_CHARTS_CONFIGURATION: InjectionToken<NgChartsConfiguration>;