For PIE Chart:
HTML:
<canvas baseChart width="200" height="200"
[data]="chartData"
[options]="chartOptions"
[type]="chartType">
</canvas>
TS:
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChartConfiguration, ChartData, ChartType } from 'chart.js';
import { BaseChartDirective } from 'ng2-charts';
export class MyChartComponent implements OnInit {
@ViewChild(BaseChartDirective) chart: BaseChartDirective | undefined;
constructor() { }
ngOnInit(): void {}
public chartOptions: ChartConfiguration['options'] = {
responsive: true,
plugins: {
legend: {
display: true,
position: 'top',
},
},
};
public chartData: ChartData<'pie', number[], string | string[]> = {
labels: ['Low', 'Middle', 'High'],
datasets: [{
data: [25, 40, 35],
backgroundColor: ['rgba(0, 160, 0, 1)', 'rgba(240, 160, 0, 1)', 'rgba(220, 0, 0, 1)'],
borderColor: ['rgba(250, 250, 250, 1)', 'rgba(250, 250, 250, 1)', 'rgba(250, 250, 250, 1)'],
hoverBackgroundColor: ['rgba(0, 160, 0, 0.8)', 'rgba(240, 160, 0, 0.8)', 'rgba(220, 0, 0, 0.8)'],
hoverBorderColor: ['rgba(0, 160, 0, 1)', 'rgba(240, 160, 0, 1)', 'rgba(220, 0, 0, 1)'],
}],
};
public chartType: ChartType = 'pie';
}