组件模板及相关功能:
<div>
<ngx-charts-pie-chart
[scheme]="colorScheme"
[results]="chart.data"
(select)="onSelect($event)">
</ngx-charts-pie-chart>
</div>
<div *ngIf="legend" class="chart-legend">
<ngx-charts-legend
[data]="chart.legendData"
[colors]="chart.colors"
(labelClick)="onLabelClick($event, chart.data)">
</ngx-charts-legend>
</div>
onLabelClick(event: any, data: any) {
let result = data.find((obj: any) => {
return obj.extra.displayName === event;
})
this.onSelect(result);
}
onSelect(event: any) {
console.log(event);
}
我的数据源示例,我从 API 获取数据,然后设置这些图表属性:
chart: Chart =
{
title: 'Some Title',
subtitle: 'A descriptive subtitle',
data: this.chartData,
legendData: [],
colors: new ColorHelper('cool', 'ordinal', [], null),
type: 'pie'
}
chartData = [];
this.myService.getChartData(body).subscribe(
result => {
let data = result.data;
for (let d in data) {
let name = d;
let val = data[d];
let newDataPoint: NewDataPoint = {
'name': name,
'value': val,
'extra': {
'displayName': name,
'displayValue': val
}
}
this.chartData.push(newDataPoint);
}
chart.data = [...this.chartData];
chart.legendData = chart.data.map(d => d['extra']['displayName']);
chart.colors = new ColorHelper('neons', 'ordinal', chart.legendData, null);
}
);