要自定义ng2-chart饼图的图例颜色和点击事件,可以按照以下步骤进行操作:
{{legendItem.label}}
import { Component } from '@angular/core';
@Component({
selector: 'app-pie-chart',
templateUrl: './pie-chart.component.html',
styleUrls: ['./pie-chart.component.css']
})
export class PieChartComponent {
public pieChartLabels: string[] = ['Red', 'Blue', 'Yellow'];
public pieChartData: number[] = [300, 500, 100];
public pieChartType: string = 'pie';
public pieChartLegend: any[] = [
{ label: 'Red', visible: true },
{ label: 'Blue', visible: true },
{ label: 'Yellow', visible: true }
];
public chartColors: any[] = [
{ backgroundColor: 'red', borderColor: 'red' },
{ backgroundColor: 'blue', borderColor: 'blue' },
{ backgroundColor: 'yellow', borderColor: 'yellow' }
];
public chartHovered(event: any): void {
// 鼠标悬停事件
}
public chartClicked(event: any): void {
// 饼图点击事件
}
public toggleVisibility(index: number): void {
this.pieChartLegend[index].visible = !this.pieChartLegend[index].visible;
}
}
这样,你就可以在ng2-chart饼图中自定义图例的颜色和点击事件了。